I've run into the same thing and am curious if there is known better way to do it.
In my case, I've separated my playbooks and site.yml includes them all.
The problem is that if all hosts from play1 fail, then play2 never executes.
cat stuff-pass.yml
---
- name: expected to pass
hosts: localhost
tasks:
- name: show stuff
debug: msg="this should pass"
cat stuff-fail.yml
---
- name: expected to fail
hosts: should_fail
connection: local
gather_facts: no
tasks:
- name: This should fail
debug: msg="This should fail"
failed_when: true
If the pass runs first, they all run:
cat stuff.yml
---
- include: stuff-pass.yml
- include: stuff-fail.yml
ansible-playbook -i hosts stuff.yml
PLAY [expected to pass] ********************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [expected to pass] ********************************************************
ok: [localhost] => {
"msg": "this should pass"
}
PLAY [expected to fail] ********************************************************
TASK [This should fail] ********************************************************
fatal: [testhost1]: FAILED! => {
"changed": false,
"failed": true,
"failed_when_result": true,
"msg": "This should fail"
}
NO MORE HOSTS LEFT *************************************************************
to retry, use: --limit @stuff.retry
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
testhost1 : ok=0 changed=0 unreachable=0 failed=1
If the pass runs second, they don't:
cat stuff.yml
---
- include: stuff-fail.yml
- include: stuff-pass.yml
ansible-playbook -i hosts stuff.yml
PLAY [expected to fail] ********************************************************
TASK [This should fail] ********************************************************
fatal: [testhost1]: FAILED! => {
"changed": false,
"failed": true,
"failed_when_result": true,
"msg": "This should fail"
}
NO MORE HOSTS LEFT *************************************************************
to retry, use: --limit @stuff.retry
PLAY RECAP *********************************************************************
testhost1 : ok=0 changed=0 unreachable=0 failed=1