# Error Handling in Playbook has strange behaviour

**URL:** <https://forum.ansible.com/t/error-handling-in-playbook-has-strange-behaviour/15266>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [May 19, 2014, 5:56am UTC](https://forum.ansible.com/t/error-handling-in-playbook-has-strange-behaviour/15266 "2014-05-19T05:56:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Rusa](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@Christian\_Rusa](https://forum.ansible.com/u/Christian_Rusa)\
**Post date:** [May 19, 2014, 5:56am UTC](https://forum.ansible.com/t/error-handling-in-playbook-has-strange-behaviour/15266/1 "2014-05-19T05:56:20Z")

</div>

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  
&nbsp;&nbsp;&nbsp;user: "{{ ssh\_user }}"  
&nbsp;&nbsp;&nbsp;sudo: yes  
&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;- name: perform dist-upgrade  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;apt: upgrade=dist update\_cache=yes  
&nbsp;&nbsp;&nbsp;- name: check if reboot is required  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: ls /var/run/reboot-required  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: reboot\_required  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: True  
&nbsp;&nbsp;&nbsp;- name: add to reboot\_hosts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add\_host: name={{ inventory\_hostname }} groups=reboot\_hosts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: reboot\_required|success

- hosts: reboot\_hosts  
&nbsp;&nbsp;&nbsp;user: "{{ ssh\_user }}"  
&nbsp;&nbsp;&nbsp;sudo: yes  
&nbsp;&nbsp;&nbsp;gather\_facts: no  
&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;- name: reboot  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: reboot

The output looks like this:

ansible-playbook -v update.yml

PLAY [rusa]

---

<div class="post-metadata">

**Author:** ![Christian\_Rusa](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@Christian\_Rusa](https://forum.ansible.com/u/Christian_Rusa)\
**Post date:** [May 19, 2014, 6:35am UTC](https://forum.ansible.com/t/error-handling-in-playbook-has-strange-behaviour/15266/2 "2014-05-19T06:35:49Z")

</div>

I forgot: ansible version 1.7 cloned from git last week.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 21, 2014, 9:46pm UTC](https://forum.ansible.com/t/error-handling-in-playbook-has-strange-behaviour/15266/3 "2014-05-21T21:46:24Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [May 22, 2014, 1:30am UTC](https://forum.ansible.com/t/error-handling-in-playbook-has-strange-behaviour/15266/4 "2014-05-22T01:30:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Christian\_Rusa](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@Christian\_Rusa](https://forum.ansible.com/u/Christian_Rusa)\
**Post date:** [May 22, 2014, 5:22am UTC](https://forum.ansible.com/t/error-handling-in-playbook-has-strange-behaviour/15266/5 "2014-05-22T05:22:49Z")

</div>

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](http://host3.example.com)]  
ok: [[host2.example.com](http://host2.example.com)]  
ok: [[host1.example.com](http://host1.example.com)]

TASK: [stat /tmp/dummy] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [[host3.example.com](http://host3.example.com)] =\> {“changed”: false, “stat”: {“exists”: false}}  
ok: [[host2.example.com](http://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](http://host1.example.com)] =\> {“changed”: false, “stat”: {“exists”: false}}

TASK: [add to reboot\_hosts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [[host1.example.com](http://host1.example.com)]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
[host2.example.com](http://host2.example.com) : ok=2 changed=0 unreachable=0 failed=0  
[host1.example.com](http://host1.example.com) : ok=2 changed=0 unreachable=0 failed=0  
[host3.example.com](http://host3.example.com) : ok=2 changed=0 unreachable=0 failed=0

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [May 22, 2014, 1:22pm UTC](https://forum.ansible.com/t/error-handling-in-playbook-has-strange-behaviour/15266/6 "2014-05-22T13:22:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 23, 2014, 12:23pm UTC](https://forum.ansible.com/t/error-handling-in-playbook-has-strange-behaviour/15266/7 "2014-05-23T12:23:09Z")

</div>

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.
