Running command "when"

41 views
Skip to first unread message

Dimitri Yioulos

unread,
Dec 22, 2014, 2:07:36 PM12/22/14
to ansible...@googlegroups.com
Hi, all.

I'm an Ansible newbie trying to create a playbook which will: 1) update all packages on a CentOS 6 box via yum, and 2) reboot the server if the kernel has been updated.  Here's my palybook:

---

- hosts: all
  tasks:
    - name: yum update
      yum: name=* state=latest

    - name: Check for reboot hint.
      shell: if [ `rpm -qa --last|grep kernel-2.6| cut -d'-' -f2- | awk '{print $1}' | head -n 1` != `uname -r` ];  then echo "reboot"; else echo "no"; fi
      ignore_errors: true
      register: reboot_hint
    - name: Rebooting ...
      command: shutdown -r now "Yum kernel update applied"
      when: reboot_hint.stdout.find('reboot') == 'reboot'
      async: 0
      poll: 0
      ignore_errors: true
      register: rebooting

(the last "register" is meaningless for the time being)

The "Check for reboot hint" works, as tested against machines requiring a reboot and those not, via a test bash script.  However, no matter wheat I put in my "when" conditional in the playbook, it fails with "error while evaluating conditional".

Help would be greatly appreciated.

Dimitri

Dimitri Yioulos

unread,
Dec 23, 2014, 12:31:23 PM12/23/14
to ansible...@googlegroups.com

With some help from IRC channel, the fix was really simple.  And, I was able to streamline, too.  Here's the working copy of my playbook:


---

- hosts: all
  tasks:
    - name: yum update
      yum: name=* state=latest
    - name: Check for reboot hint.
      shell: if [ `rpm -qa --last|grep kernel-2.6| cut -d'-' -f2- | awk '{print $1}' | head -n 1` != `uname -r` ];  then echo "reboot"; else echo "no"; fi
      register: reboot_hint
      always_run: yes

    - name: Rebooting ...
      command: shutdown -r now "Yum kernel update applied"
      when: reboot_hint.stdout.find('reboot') != -1
      register: rebooting
Reply all
Reply to author
Forward
0 new messages