use command output to trigger a new task

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

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.