use command output to trigger a new task

102 views
Skip to first unread message

Peter Zoltan Keresztes

unread,
Mar 20, 2016, 2:37:22 PM3/20/16
to Ansible Project
Hello,

I am trying to remove unused kernel packages when the command output is larger than 1

the tasks looks like this:

- name: Check if kernel upgrade is available

  command: dpkg -l linux-image* | grep ii | wc -l 2>&1

  register: kernelinstalled


- name: Delete unused linux kernels

  command: apt-get remove $(dpkg -l linux-image* | grep ii | grep -v $(uname -r) | awk '{print $2}')

  when: kernelinstalled.stdout >= 2


however when I run a check of the playbook I get the following failure:

TASK [nginx : Delete unused linux kernels] *************************************

fatal: [10.160.125.74]: FAILED! => {"failed": true, "msg": "The conditional check 'kernelinstalled.stdout >= 2' failed. The error was: error while evaluating conditional (kernelinstalled.stdout >= 2): 'dict object' has no attribute 'stdout'\n\nThe error appears to have been in '/etc/ansible/roles/nginx/tasks/main.yml': line 20, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n#\n- name: Delete unused linux kernels\n  ^ here\n"}


Any idea how can I make that work?


Regards,

Peter

matthew...@learnosity.com

unread,
Mar 21, 2016, 6:30:27 AM3/21/16
to Ansible Project
Have you run it in debug to see what the kernalinstalled object looks like?

- debug: var=kernalinstalled

Also, I do believe the 2 needs to be like '2' as its actually a string not a integer thats returned. I think if you do something like kernalinstalled.stdout|int > 2 that may also work as well. 
Reply all
Reply to author
Forward
0 new messages