# Template Conditionals

**URL:** <https://forum.ansible.com/t/template-conditionals/23506>\
**Category:** Ansible Project\
**Created:** [January 19, 2017, 5:47pm UTC](https://forum.ansible.com/t/template-conditionals/23506 "2017-01-19T17:47:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Horse\_Radish](https://avatars.discourse-cdn.com/v4/letter/h/f9ae1b/32.png) [@Horse\_Radish](https://forum.ansible.com/u/Horse_Radish)\
**Post date:** [January 19, 2017, 5:47pm UTC](https://forum.ansible.com/t/template-conditionals/23506/1 "2017-01-19T17:47:52Z")

</div>

'Hope this is not a stupid question but …

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

```auto

[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).

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 19, 2017, 6:43pm UTC](https://forum.ansible.com/t/template-conditionals/23506/2 "2017-01-19T18:43:31Z")

</div>

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/intro_inventory.html#splitting-out-host-and-group-specific-data)  
[https://docs.ansible.com/ansible/playbooks\_best\_practices.html#group-and-host-variables](https://docs.ansible.com/ansible/playbooks_best_practices.html#group-and-host-variables)

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 20, 2017, 9:02am UTC](https://forum.ansible.com/t/template-conditionals/23506/3 "2017-01-20T09:02:03Z")

</div>

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

- template:  
&nbsp;&nbsp;&nbsp;&nbsp;src: "foobar"  
&nbsp;&nbsp;&nbsp;&nbsp;dest: "/some/path"  
&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 20, 2017, 3:50pm UTC](https://forum.ansible.com/t/template-conditionals/23506/4 "2017-01-20T15:50:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 20, 2017, 4:22pm UTC](https://forum.ansible.com/t/template-conditionals/23506/5 "2017-01-20T16:22:48Z")

</div>

Sorry, my bad. Then use this:

{% if inventory\_hostname in groups["s8"] or ...

Johannes
