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

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 }}
}

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

Giovanni

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.

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

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