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

1,202 views
Skip to first unread message

Larry Fast

unread,
Mar 31, 2015, 1:07:12 AM3/31/15
to ansible...@googlegroups.com
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 }}
}




Giovanni Tirloni

unread,
Mar 31, 2015, 8:53:02 AM3/31/15
to ansible...@googlegroups.com
On Mon, 30 Mar 2015 22:07 -0700, Larry Fast <lfas...@gmail.com> wrote:
> 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?

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

Larry Fast

unread,
Apr 1, 2015, 5:37:38 PM4/1/15
to ansible...@googlegroups.com
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.


Brian Coca

unread,
Apr 1, 2015, 7:56:52 PM4/1/15
to ansible...@googlegroups.com
you can just have a play that acts on localhost and uses that task, it
will run independent of previous play failures.



--
Brian Coca

Larry Fast

unread,
Apr 2, 2015, 12:54:19 PM4/2/15
to ansible...@googlegroups.com
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

Reply all
Reply to author
Forward
0 new messages