# How can I have a Task always executed on localhost at the end of a Playbook even if the hosts all fail?

**URL:** <https://forum.ansible.com/t/how-can-i-have-a-task-always-executed-on-localhost-at-the-end-of-a-playbook-even-if-the-hosts-all-fail/17877>\
**Category:** Ansible Project\
**Created:** [March 31, 2015, 5:07am UTC](https://forum.ansible.com/t/how-can-i-have-a-task-always-executed-on-localhost-at-the-end-of-a-playbook-even-if-the-hosts-all-fail/17877 "2015-03-31T05:07:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Larry\_Fast](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Larry\_Fast](https://forum.ansible.com/u/Larry_Fast)\
**Post date:** [March 31, 2015, 5:07am UTC](https://forum.ansible.com/t/how-can-i-have-a-task-always-executed-on-localhost-at-the-end-of-a-playbook-even-if-the-hosts-all-fail/17877/1 "2015-03-31T05:07:12Z")

</div>

I have a diagnostic task that dumps the Ansible dictionaries at the end of a run. However if the hosts fail the Playbook stops and my task doesn’t get executed. But of course that’s when it’s most important. How can I arrange that this task always runs after everything else?

For what it’s worth, here’s my dump\_vars code. The real goal is to have a data dump at the end of each Playbook. Another means to this end would also be a valuable solution.

… Other Playbook code …

# Last Play

- hosts: localhost  
gather\_facts: no  
tasks:

- name: dump all vars  
local\_action: template src=dump\_vars.j2 dest=./dump\_vars.out  
tags: [testing1]  
ignore\_errors: yes  
changed\_when: False

cat dump\_vars.j2

{  
“aaa\_export\_info1”: “Export of all Ansible 1.x Dicts as a single JSON dict”,  
“aaa\_export\_info2”: “This file combines the dicts into a single JSON record”,  
“aaa\_export\_info3”: “Note that indentation is a bit off”,

“vars”: {{ vars | remove\_sensitive\_data | to\_nice\_json }},  
“environment”: {{ environment | remove\_sensitive\_data | to\_nice\_json }},  
“group\_names”: {{ group\_names | remove\_sensitive\_data | to\_nice\_json }},  
“groups”: {{ groups | remove\_sensitive\_data | to\_nice\_json }},  
“hostvars”: {{ hostvars | remove\_sensitive\_data | to\_nice\_json }}  
}

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [March 31, 2015, 12:52pm UTC](https://forum.ansible.com/t/how-can-i-have-a-task-always-executed-on-localhost-at-the-end-of-a-playbook-even-if-the-hosts-all-fail/17877/2 "2015-03-31T12:52:53Z")

</div>

You can have a handler for that and start ansible with  
'--force-handlers'.

v2 is coming with try/except/finally [1]

[1] -  
[http://www.slideshare.net/jimi-c/whats-new-in-v2-ansiblefest-london-2015/10](http://www.slideshare.net/jimi-c/whats-new-in-v2-ansiblefest-london-2015/10)

Giovanni

---

<div class="post-metadata">

**Author:** ![Larry\_Fast](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Larry\_Fast](https://forum.ansible.com/u/Larry_Fast)\
**Post date:** [April 1, 2015, 9:37pm UTC](https://forum.ansible.com/t/how-can-i-have-a-task-always-executed-on-localhost-at-the-end-of-a-playbook-even-if-the-hosts-all-fail/17877/3 "2015-04-01T21:37:37Z")

</div>

I want to be sure I understand the scope of the stop-on-failure behavior. Is this correct: If a single Task fails for all selected servers, execution of the whole Playbook stops. The exception is that all handlers will run IF force-handlers is true.

For clarity this implies that a Playbook managing 100 servers will stop if any Task fails for all-selected-servers even if that Task only applies to perhaps a single server.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 1, 2015, 11:56pm UTC](https://forum.ansible.com/t/how-can-i-have-a-task-always-executed-on-localhost-at-the-end-of-a-playbook-even-if-the-hosts-all-fail/17877/4 "2015-04-01T23:56:48Z")

</div>

you can just have a play that acts on localhost and uses that task, it  
will run independent of previous play failures.

---

<div class="post-metadata">

**Author:** ![Larry\_Fast](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Larry\_Fast](https://forum.ansible.com/u/Larry_Fast)\
**Post date:** [April 2, 2015, 4:54pm UTC](https://forum.ansible.com/t/how-can-i-have-a-task-always-executed-on-localhost-at-the-end-of-a-playbook-even-if-the-hosts-all-fail/17877/5 "2015-04-02T16:54:19Z")

</div>

Thanks Brian,  
I think I tried that. The following cut down example stops after the first Play fails for all selected hosts. It did not execute the localhosts Play. I’m all ears if you’re suggesting something different or can show me what I’ve done wrong.

- hosts: some\_hosts  
tasks:

- fail: always fail to test the operation  
when: true

- hosts: localhost  
dump\_vars
