I have two plays (1 and 2) in my playbook. First play play1 has two tasks (A/B). If task A fails, I need task B also be executed and then playbook exits. In otherwords, play 2 will be skipped. So I used block/always method. It works fine when the host is single host. But when I specify multiple hosts to plays, play2 still got executed. Although play2 was only executed against one host, I expect the playbook to exit before play2.
I tried to add any_errors_fatal to task A, however it doesn't work.
# single host playbook
name: Test Block 1
hosts: pltB
gather_facts: no
tasks:
block:
- command: "/usr/bin/hostname1"
register: hostname_res
any_errors_fatal: true
always:
- debug: msg="from always block 1"
name: Test Block 2
hosts: pltB
gather_facts: no
tasks:
block:
- debug: msg="result is {{ hostname_res.stdout }} "
always:
- debug: msg="from always block 2" ...
output of single host
ansible-playbook test.yml -i ../inventory/serverhosts
PLAY [Test Block 1] **************************************************************************************
TASK [command] *************************************************************************************** fatal: [192.168.111.25]: FAILED! => {"changed": false, "cmd": "/usr/bin/hostname1", "msg": "[Errno 2] No such file or directory", "rc": 2}
TASK [debug] ************************************************************************************* ok: [192.168.111.25] => { "msg": "from always block 1" } to retry, use: --limit @/home/playbooks/test.retry
PLAY RECAP ************************************************************************************* 192.168.111.25 : ok=1 changed=0 unreachable=0 failed=1
multiple servers in hosts
name: Test Block 1
hosts: pltB,pltA
gather_facts: no
tasks:
block:
- command: "/usr/bin/hostname1"
register: hostname_res
any_errors_fatal: true
always:
- debug: msg="from always block 1"
name: Test Block 2
hosts: pltB,pltA
gather_facts: no
tasks:
block:
- debug: msg="result is {{ hostname_res.stdout }} "
always:
- debug: msg="from always block 2" ...
output of multiple servers
PLAY [Test Block 1] ***********************************************************************************
TASK [command] *************************************************************************************** fatal: [192.168.111.25]: FAILED! => {"changed": false, "cmd": "/usr/bin/hostname1", "msg": "[Errno 2] No such file or directory", "rc": 2} changed: [192.168.111.24]
TASK [debug] *************************************************************************************** ok: [192.168.111.25] => { "msg": "from always block 1" } ok: [192.168.111.24] => { "msg": "from always block 1" }
PLAY [Test Block 2] *********************************************************************************
TASK [debug] *************************************************************************************** ok: [192.168.111.24] => { "msg": "result is plt001 " }
TASK [debug] ************************************************************************************** ok: [192.168.111.24] => { "msg": "from always block 2" } to retry, use: --limit @/home/playbooks/test.retry
PLAY RECAP ******************************************************************************************************* 192.168.111.24 : ok=4 changed=1 unreachable=0 failed=0 192.168.111.25 : ok=1 changed=0 unreachable=0 failed=1