extract the variables, select true items and evaluate the test is any, e.g.
- set_fact:
semaphore: "{{ ansible_play_hosts_all|
map('extract', hostvars, 'new_node')|
select is any }}"
run_once: true
gives true if any variable new_node is true and gives false otherwise
semaphore: true
Then use the variable semaphore in the conditions e.g.
when: semaphore|bool
Filter select explained
Quoting from select
"If no test is specified, each object will be evaluated as a boolean."
Given the inventory
shell> cat hosts
host1.example.net
host2.example.net new_node=False
host3.example.net new_node=True
The task extracts the variables
- debug:
msg: "{{ ansible_play_hosts_all|
map('extract', hostvars, 'new_node')|
list }}"
msg: '[Undefined, False, True]'
We don't have to care about Undefined because select() evaluates Undefined to False
- debug:
msg: "{{ ansible_play_hosts_all|
map('extract', hostvars, 'new_node')|
select|list }}"
msg:
- true
You can test the evaluation of Undefined to False separately, e.g.
- debug:
msg: "{{ ansible_play_hosts_all|
map('extract', hostvars, 'new_node')|
map('bool')|
list }}"
gives
msg:
- false
- false
- true
If no variable is defined
shell> cat hosts
host1.example.net
host2.example.net
host3.example.net
select returns an empty list
msg: '[Undefined, Undefined, Undefined]'
msg: []