I have an Ansible role that I want to execute on particular hosts based on a certain conditions.
I want to populate the hosts
from Ansible Tower survey. Here's my playbook:
- name: HTTP Response Deploy Automation
hosts: "{% if geo == 'LHR' %}'dblhr002' {% elif geo == 'SJC' %}'dbsjc003' {% endif %}"
gather_facts: true
roles:
- http-response-deploy
I'm getting the following error when choosing LHR:
[WARNING]: Could not match supplied host pattern, ignoring: 'dblhr002'
To note that, it doesn't work when I choose to omit the quotes around the hostnames.
TLDR; need to achieve the conditionals from Ansible as below:
if geo == "LHR":
hosts: dblhr002
if geo == "SJC":
hosts: dbsjc003
Best Answer
What you have provided works fine, as long as
dblhr002
is listed in inventory. Host patterns only match existing hosts, they do not add new hosts to the inventory.If you need to dynamically add a host, use the
add_host
task in a separate play.