If the user passes perform_action
parameter as any of these telnetcurlnslookuptracerouteget_ip_address
then i want the play to run on localhost
else it should run on remotehosts
ansible-playbook test.yml -e perform_action='nslookup'
- name: "Play 1"
hosts: localhost
tasks:
- set_fact:
final_delegate: "{{ 'localhost' if perform_action in 'telnetcurlnslookuptracerouteget_ip_address' else 'remotehosts' }} "
- debug:
msg: "Play needs to run on {{ final_delegate }}"
- name: "Play 2"
hosts: "{{ final_delegate }}"
tasks:
- debug:
msg: "Im running on {{ inventory_hostname }}"
Output:
The Play needs to run on localhost
However, Play 2 fails with the below error:
ERROR! The field 'hosts' has an invalid value, which includes an undefined variable. The error was: 'final_delegate' is undefined
Can this condition be set with the play as i m doing or is it possible only by putting a condition on -e
parameter?