Error Handling in Playbook has strange behaviour

61 views
Skip to first unread message

Christian Rusa

unread,
May 19, 2014, 1:56:20 AM5/19/14
to ansible...@googlegroups.com
Hi,

I wrote a playbook to update a group of ubuntu servers and reboot them
if required.

If I run it against one host using the -l option it works as expected,
running against the whole group (3 hosts) it gives some strange
behaviour. The "add to reboot_hosts" task is only executed (and skipped)
for the second host in the group. The host which requires a reboot is
not rebooted. I think I overlooked something.

Best Regards
Christian Rusa


The playbook looks like this:

- hosts: rusa
user: "{{ ssh_user }}"
sudo: yes
tasks:
- name: perform dist-upgrade
apt: upgrade=dist update_cache=yes
- name: check if reboot is required
command: ls /var/run/reboot-required
register: reboot_required
ignore_errors: True
- name: add to reboot_hosts
add_host: name={{ inventory_hostname }} groups=reboot_hosts
when: reboot_required|success

- hosts: reboot_hosts
user: "{{ ssh_user }}"
sudo: yes
gather_facts: no
tasks:
- name: reboot
command: reboot


The output looks like this:

ansible-playbook -v update.yml

PLAY [rusa]
*******************************************************************

GATHERING FACTS
***************************************************************
ok: [host1.example.com]
ok: [host3.example.com]
ok: [host2.example.com]

TASK: [perform dist-upgrade]
**************************************************
ok: [host3.example.com] => {"changed": false, "msg": "Reading package
lists...\nBuilding dependency tree...\nReading state information...\n0
upgraded, 0 newly installed, 0 to remove and 0 not upgraded.\n",
"stderr": "", "stdout": "Reading package lists...\nBuilding dependency
tree...\nReading state information...\n0 upgraded, 0 newly installed, 0
to remove and 0 not upgraded.\n"}
ok: [host2.example.com] => {"changed": false, "msg": "Reading package
lists...\nBuilding dependency tree...\nReading state information...\n0
upgraded, 0 newly installed, 0 to remove and 0 not upgraded.\n",
"stderr": "", "stdout": "Reading package lists...\nBuilding dependency
tree...\nReading state information...\n0 upgraded, 0 newly installed, 0
to remove and 0 not upgraded.\n"}
ok: [host1.example.com] => {"changed": false, "msg": "Reading package
lists...\nBuilding dependency tree...\nReading state information...\n0
upgraded, 0 newly installed, 0 to remove and 0 not upgraded.\n",
"stderr": "", "stdout": "Reading package lists...\nBuilding dependency
tree...\nReading state information...\n0 upgraded, 0 newly installed, 0
to remove and 0 not upgraded.\n"}

TASK: [check if reboot is required]
*******************************************
failed: [host3.example.com] => {"changed": true, "cmd": ["ls",
"/var/run/reboot-required"], "delta": "0:00:00.010204", "end":
"2014-05-19 07:36:20.335245", "rc": 2, "start": "2014-05-19
07:36:20.325041"}
stderr: ls: cannot access /var/run/reboot-required: No such file or
directory
...ignoring
failed: [host2.example.com] => {"changed": true, "cmd": ["ls",
"/var/run/reboot-required"], "delta": "0:00:00.010280", "end":
"2014-05-19 07:36:20.552200", "rc": 2, "start": "2014-05-19
07:36:20.541920"}
stderr: ls: cannot access /var/run/reboot-required: No such file or
directory
...ignoring
changed: [host1.example.com] => {"changed": true, "cmd": ["ls",
"/var/run/reboot-required"], "delta": "0:00:00.008175", "end":
"2014-05-19 07:36:20.607526", "rc": 0, "start": "2014-05-19
07:36:20.599351", "stderr": "", "stdout": "/var/run/reboot-required"}

TASK: [add to reboot_hosts]
***************************************************
skipping: [host2.example.com]

PLAY [reboot_hosts]
***********************************************************
skipping: no hosts matched

PLAY RECAP
********************************************************************
host1.example.com : ok=3 changed=1 unreachable=0
failed=0
host2.example.com : ok=3 changed=1 unreachable=0
failed=0
host3.example.com : ok=3 changed=1 unreachable=0 failed=0

Christian Rusa

unread,
May 19, 2014, 2:35:49 AM5/19/14
to ansible...@googlegroups.com
I forgot: ansible version 1.7 cloned from git last week.

Michael DeHaan

unread,
May 21, 2014, 5:46:24 PM5/21/14
to ansible...@googlegroups.com
Easier way to do this would be to use the "stat" module, use with "-v" to see what it returns and skip the "ignore_errors" logic.

Still, this doesn't look right to me.

Can you please file a github ticket with this information so we can investigate and make sure we have test coverage if we can reproduce this behavior?

Thanks!


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5379A645.8020902%40toscom.at.

For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
May 21, 2014, 9:30:13 PM5/21/14
to ansible...@googlegroups.com

Doesn't add host only run once per host loop? I think you need a with_items clause, I would just make it a handler off of stat for the file

Brian Coca

Christian Rusa

unread,
May 22, 2014, 1:22:49 AM5/22/14
to ansible...@googlegroups.com
There always is a smarter modules that I don't know :-)
Thank you for the hint with the stat module.

Unfortunately I have the same problem with the stat Module, so I will file a github ticket.
It seems that the task "add to reboot_hosts" is only executed for the first host in the ansible hosts file.

I wrote a shorter version for testing.


- hosts: rusa
  user: "{{ ssh_user }}"
  sudo: yes
  tasks:
  - name: stat /tmp/dummy
    stat: path=/tmp/dummy
    register: dummy


  - name: add to reboot_hosts
    add_host: name="{{ inventory_hostname }}" groups=reboot_hosts
    when: dummy.stat.exists == true



ansible-playbook bug.yml -v


PLAY [rusa] *******************************************************************

GATHERING FACTS ***************************************************************
ok: [host3.example.com]
ok: [host2.example.com]
ok: [host1.example.com]

TASK: [stat /tmp/dummy] *******************************************************
ok: [host3.example.com] => {"changed": false, "stat": {"exists": false}}
ok: [host2.example.com] => {"changed": false, "stat": {"atime": 1400734828.0029335, "ctime": 1400560489.169877, "dev": 64769, "exists": true, "gid": 0, "inode": 5769287, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "md5": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "mtime": 1400560489.169877, "nlink": 1, "pw_name": "root", "rgrp": true, "roth": true, "rusr": true, "size": 0, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}}
ok: [host1.example.com] => {"changed": false, "stat": {"exists": false}}


TASK: [add to reboot_hosts] ***************************************************
skipping: [host1.example.com]

PLAY RECAP ********************************************************************
host2.example.com               : ok=2    changed=0    unreachable=0    failed=0  
host1.example.com                : ok=2    changed=0    unreachable=0    failed=0  
host3.example.com      : ok=2    changed=0    unreachable=0    failed=0  
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CA%2BnsWgywEEKp5xjhUA11pPrn05HnqbtB0TbLtTPHwzBuWi6CHg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

-- 
toscom - the open source company

Christian Rusa
Breiteneckergasse 32                    1230 Wien
Mobil: 0699 10205595                    Fax: 01 9249633
www.toscom.at                           christi...@toscom.at 

Brian Coca

unread,
May 22, 2014, 9:22:27 AM5/22/14
to ansible...@googlegroups.com
this is by design, add_hosts has  BYPASS_HOST_LOOP = True, which means it executes only 1 time per play, you need to use with_items with it. Your current play cannot work.​

instead of making reboot_hosts into a play just create a reboot handler

- handlers:
   - name: reboot
     command: reboot


and call it from the first play (notify: reboot) when testing for the existence of /var/run/reboot-required

     

Michael DeHaan

unread,
May 23, 2014, 8:23:12 AM5/23/14
to ansible...@googlegroups.com
Right, there's an open ticket to make sure the add_host module has a very clear example of what to do and what not to do.

Basically it should only be used in the "ec2 provisioning" type plays where it's adding some temporarily created hosts.

If you want to dynamically add hosts to new groups, "group_by" is the one to use.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages