Template Conditionals

'Hope this is not a stupid question but …

I have an existing inventory and playbook associated with it. The inventory has entries like this:


[s1]
host1
[s2]
host2
...

[foo:children]
s8
s9
 

The foo playbook has templatized config files … and there’s the rub. I want the foo playbook to work on s8 and s9, BUT, I want different strings to be substituted when the template gets processed, depending on whether I am on s8 or s9.

I know this is doable, I just don’t know how, or how to do it cleanly …

Ideas? (And thanks).

group_vars/s8.yml
group_vars/s9.yml

https://docs.ansible.com/ansible/intro_inventory.html#splitting-out-host-and-group-specific-data
https://docs.ansible.com/ansible/playbooks_best_practices.html#group-and-host-variables

Kai already mentioned host_vars. If you rather want to do stuff in the
template, then do something like this:

- template:
    src: "foobar"
    dest: "/some/path"
  when: 'inventory_hostname == "s8" or inventory_hostname == "s9"'

In the template:

########### snip #############

{% if inventory_hostname == "s8" %}
Here is stuff for s8.
{% elif inventory_hostname == "s9" %}
Here is stuff for s9.
{% endif %}

########### snip #############

Johannes

s8 and s9 is groups, not hosts since they are in [foo:children] so this will not work.

Sorry, my bad. Then use this:

{% if inventory_hostname in groups["s8"] or ...

Johannes