Force Playbook to fail on any task failure

I needed a playbook to fail complete when any task failed. You would think “any_errors_fatal: True” would do this, but it doesn’t. It marks the hosts as failed and ends the play, but the recap doesn’t say anything about it. Instead it prints out the normal output even though the playbook never completes for any node. It’s extremely deceptive.

As a result, I opened up a ticket with the Ansible devs (, but they claimed this was a documentation bug.

I complained about that in the user group (!topic/ansible-devel/MT-bZgISWrs), but I was ignored.

Finally, I decided I needed a workaround. Here it is. I lifted portions of this code in various places, but I can’t recall them all. I apologize if I lifted something from you.

The idea is to catch any failure and set a global variable (using add_host). Then all the nodes can test that global failure and fail as necessary. Here is the code:

- hosts: all
  gather_facts: no
  become: False

  - name: Use block to catch errors
      - name: Generate a random failue
        shell: 'echo $(( $RANDOM % 10 ))'
        register: random
        failed_when: random.stdout|int == 2

      - debug:
          msg: 'This node PASSED'

      - debug:
          msg: 'This node FAILED'

      - name: "Create a dummy host to hold a failure flag"
          name: "BAD_HOST"

  - name: Force EVERYTHING to fail
    fail: msg="One host failed"
    when: hostvars['BAD_HOST'] is defined

EDIT: It looks the Ansible devs are going to implement my suggestion in a future release of Ansible.

Joel Griffiths

Back to top