# Templete statement that can qualify mulitiple inventory\_hostname

**URL:** <https://forum.ansible.com/t/templete-statement-that-can-qualify-mulitiple-inventory-hostname/26269>\
**Category:** Ansible Project\
**Created:** [December 12, 2017, 10:56pm UTC](https://forum.ansible.com/t/templete-statement-that-can-qualify-mulitiple-inventory-hostname/26269 "2017-12-12T22:56:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Johnston](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@Mike\_Johnston](https://forum.ansible.com/u/Mike_Johnston)\
**Post date:** [December 12, 2017, 10:56pm UTC](https://forum.ansible.com/t/templete-statement-that-can-qualify-mulitiple-inventory-hostname/26269/1 "2017-12-12T22:56:38Z")

</div>

I’m trying to figure out how to pass statement that will match multiple hostnames instead of just one. I know how to do it with a group in the hosts file, but I need it to be something I set in the defaults part of the role. I’m sure sure the answer is really easy but I’m just getting used to Jinja. Here’s my example…

hosts file:  
[group1]  
system1  
system2  
system3

roles/fubar/templates/mytest.j2

{% if inventory\_hostname == “server1” %}  
This is a test  
{% endif %}

This works… but what if I want this line on server1 and server3? Preferably something a role assigned in defaults/main.yml file like:

ImportantServers: [‘server1’ , ‘server3’]

{% if inventory\_hostname in groups[‘ImportantServers’] %}  
This is a test  
{% endif %}

That doesn’t work however. I think it’s because the groups is referring to the hosts file and not looking at the defaults. If someone could tell me what I’m doing wrong and not shame me too badly that would be great.

---

<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:** [December 13, 2017, 5:42am UTC](https://forum.ansible.com/t/templete-statement-that-can-qualify-mulitiple-inventory-hostname/26269/2 "2017-12-13T05:42:34Z")

</div>

Since ImportantServers is in defaults it's a variable and not a group, so just lose the groups.  
Groups is something you define in inventory or with the module group\_by and add\_host.

{% if inventory\_hostname in ImportantServers %}  
This is a test  
{% endif %}

---

<div class="post-metadata">

**Author:** ![Mike\_Johnston](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@Mike\_Johnston](https://forum.ansible.com/u/Mike_Johnston)\
**Post date:** [December 13, 2017, 7:13pm UTC](https://forum.ansible.com/t/templete-statement-that-can-qualify-mulitiple-inventory-hostname/26269/3 "2017-12-13T19:13:35Z")

</div>

That was it! I knew it would be simple…just not that simple. Thank you Stian. 🙂
