Hi

On 05.08.19 18:58, Mohtashim S wrote:
> Ansible any_errors_fatal module does not work as expected. Below is my 
> ansible playbook which is expected to terminate execution as soon as it 
> encounters the first error.

your expectation is wrong. Ansible would execute any task following on
any host. E.g:

---
- hosts: all
  any_errors_fatal: True
  tasks:
    - debug:
        msg: 'Pseudo Error'
      failed_when: inventory_hostname == 'abc'

    - debug:
        msg: 'This runs nowhere'

Execution:

PLAY [all] ***********************************
TASK [Gathering Facts] ***********************
ok: [abc]
ok: [def]

TASK [debug] *********************************
fatal: [abc]: FAILED! => {
    "msg": "Pseudo Error"
}
ok: [def] => {
    "msg": "Pseudo Error"
}

NO MORE HOSTS LEFT ***************************

PLAY RECAP ***********************************
abc : ok=1    changed=0    unreachable=0    failed=1    skipped=0
rescued=0    ignored=0
def : ok=2    changed=0    unreachable=0    failed=0    skipped=0
rescued=0    ignored=0

---

You could try something like this:

---
- hosts: localhost
  vars:
    dirs:
      - /tmp/foo
      - /tmp/foo/bar
      - /tmp/foo/baz/bar
      - /tmp/foo/bar/baz
      - /tmp/foo/abc

  tasks:
    - command: "mkdir {{ item }}"
      args:
        warn: no
      register: cmd_result
      with_items:
        - "{{ dirs }}"
      when:
        - not cmd_result is defined or not cmd_result['failed']

Execution:

PLAY [localhost] *****************************

TASK [Gathering Facts] ***********************
ok: [localhost]

TASK [command] *******************************
changed: [localhost] => (item=/tmp/foo)
changed: [localhost] => (item=/tmp/foo/bar)
failed: [localhost] (item=/tmp/foo/baz/bar) => {"ansible_loop_var":
"item", "changed": true, "cmd": ["mkdir", "/tmp/foo/baz/bar"], "delta":
"0:00:00.001926", "end": "2019-08-05 19:30:32.876289", "item":
"/tmp/foo/baz/bar", "msg": "non-zero return code", "rc": 1, "start":
"2019-08-05 19:30:32.874363", "stderr": "mkdir: cannot create directory
‘/tmp/foo/baz/bar’: No such file or directory", "stderr_lines": ["mkdir:
cannot create directory ‘/tmp/foo/baz/bar’: No such file or directory"],
"stdout": "", "stdout_lines": []}
skipping: [localhost] => (item=/tmp/foo/bar/baz)
skipping: [localhost] => (item=/tmp/foo/abc)

PLAY RECAP ************************************
localhost : ok=1    changed=0    unreachable=0    failed=1    skipped=0
   rescued=0    ignored=0


Sebastian

-- 
Sebastian Meyer
Linux Consultant & Trainer
Mail: me...@b1-systems.de

B1 Systems GmbH
Osterfeldstraße 7 / 85088 Vohburg / http://www.b1-systems.de
GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537


-- Deutsche OpenStack Tage 2019 -- 10% Rabatt auf den Ticketpreis ----
------------------------ https://openstack-tage.de (Code DOST-B1) ----

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d2dcd344-046f-4920-42fd-63aaaf2de29d%40b1-systems.de.

Reply via email to