Hi,
I have a playbook that uses a meta task to end the play for hosts not meeting a certain condition. Further along the playbook I have a run_once task that uses the ansible_play_hosts variable to provide a list of the hosts in the current play to a command.
My issue is that although I end the play for the hosts not meeting the required condition, they persist in the ansible_play_hosts variable - I guess this is expected since they are not failed and therefore still technically part of the play even though they will not have any further tasks executed against them. Is there any way of getting or creating a list of the hosts that remain active for the remaining tasks?
Here is a test playbook demonstrating that the play has ended for a particular host but remains in the play_hosts variable;
------ test_meta.yml ---------
---
- hosts: all
tasks:
- name: Ending the play for hosts not meeting the required condition
meta: end_host
when:
- inventory_hostname == "test01"
- block:
- debug:
msg: "Host {{ inventory_hostname }}"
delegate_to: localhost
- debug:
msg: "Host {{ item }} still in play_hosts"
with_items: "{{ ansible_play_hosts }}"
delegate_to: localhost
run_once: true
rescue:
- debug:
msg: "Failed host - {{ inventory_hostname }} - {{ ansible_failed_result.msg }}"
delegate_to: localhost
------- eof ---------
The playbook run;
PLAY [all] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [test01]
ok: [test02]
ok: [test03]
TASK [debug] *******************************************************************************************************
ok: [test02 -> localhost] => {
"msg": "Host test02"
}
ok: [test03 -> localhost] => {
"msg": "Host test03"
}
TASK [debug] *******************************************************************************************************
ok: [test02 -> localhost] => (item=test01) => {
"msg": "Host test01 still in play_hosts"
}
ok: [test02 -> localhost] => (item=test02) => {
"msg": "Host test02 still in play_hosts"
}
ok: [test02 -> localhost] => (item=test03) => {
"msg": "Host test03 still in play_hosts"
}
PLAY RECAP *********************************************************************************************************
test03 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test01 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test02 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Can anyone suggest how I get a list of hosts actually still active (i.e. still having tasks executed against them)?
ansible-2.8.4 and 2.8.5