# group name or inventory file name in tasks or jinja

**URL:** <https://forum.ansible.com/t/group-name-or-inventory-file-name-in-tasks-or-jinja/17702>\
**Category:** Ansible Project\
**Created:** [March 9, 2015, 5:18pm UTC](https://forum.ansible.com/t/group-name-or-inventory-file-name-in-tasks-or-jinja/17702 "2015-03-09T17:18:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Patermann](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Marc\_Patermann](https://forum.ansible.com/u/Marc_Patermann)\
**Post date:** [March 9, 2015, 5:18pm UTC](https://forum.ansible.com/t/group-name-or-inventory-file-name-in-tasks-or-jinja/17702/1 "2015-03-09T17:18:26Z")

</div>

Hi,

I have two inventories, each for test an production. And I call ansible(-playbook) explicitly with one of these, to not mix things up. 🙂  
(Things like IP configuration differ from test and production.)  
The inventory has group names ending in "-test" or "-prod" too, on which I could check too.

# test:  
[webserver-test]  
foo1  
server2

# production  
[webservers-prod]  
foo2  
server1

How can I check in the task and in jinja templates, if the host is in test or production?

I asked something related in 2013 and got "when: inventory\_hostname in groups.webservers-test" as an answer. But this results in a fatal  
"error while evaluating conditional: inventory\_hostname in groups.webservers-test"

I could set this as a group:var for all, like:  
# test  
[all:vars]  
env="test"

Is this the best way to do this?

Marc

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [March 9, 2015, 8:48pm UTC](https://forum.ansible.com/t/group-name-or-inventory-file-name-in-tasks-or-jinja/17702/2 "2015-03-09T20:48:14Z")

</div>

That works fine for me - [all:vars] in an inventory, but I use the  
same group names  
across all inventories for consistency.

I think this could bite me in the future with dynamic inventories etc.  
that may make it  
a little harder to do that, but for now it works great.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [March 9, 2015, 9:35pm UTC](https://forum.ansible.com/t/group-name-or-inventory-file-name-in-tasks-or-jinja/17702/3 "2015-03-09T21:35:08Z")

</div>

​You inventory has webserver-test, not webserver\*s\*-test :)​

But next thing would be, variable names cannot have dashes, so you would  
need to rewrite you conditional using a string:

&nbsp;&nbsp;&nbsp;when: inventory\_hostname in groups["webservers-test"]

---

<div class="post-metadata">

**Author:** ![Marc\_Patermann](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Marc\_Patermann](https://forum.ansible.com/u/Marc_Patermann)\
**Post date:** [March 10, 2015, 5:17pm UTC](https://forum.ansible.com/t/group-name-or-inventory-file-name-in-tasks-or-jinja/17702/4 "2015-03-10T17:17:43Z")

</div>

Serge,

Serge van Ginderachter schrieb (09.03.2015 22:35 Uhr):

> &nbsp;&nbsp;&nbsp;&nbsp;# test:  
> &nbsp;&nbsp;&nbsp;&nbsp;[\*webserver-tes\*t]  
> &nbsp;&nbsp;&nbsp;&nbsp;foo1  
> &nbsp;&nbsp;&nbsp;&nbsp;server2
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# production  
> &nbsp;&nbsp;&nbsp;&nbsp;[webservers-prod]  
> &nbsp;&nbsp;&nbsp;&nbsp;foo2  
> &nbsp;&nbsp;&nbsp;&nbsp;server1
> 
> &nbsp;&nbsp;&nbsp;&nbsp;How can I check in the task and in jinja templates, if the host is  
> &nbsp;&nbsp;&nbsp;&nbsp;in test or production?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;I asked something related in 2013 and got  
> &nbsp;&nbsp;&nbsp;&nbsp;​​  
> &nbsp;&nbsp;&nbsp;&nbsp;"when: inventory\_hostname in groups.webserve\*rs\*-test" as an answer.  
> &nbsp;&nbsp;&nbsp;&nbsp;But this results in a fatal  
> &nbsp;&nbsp;&nbsp;&nbsp;"error while evaluating conditional: inventory\_hostname in  
> &nbsp;&nbsp;&nbsp;&nbsp;groups.webservers-test"
> 
> ​You inventory has webserver-test, not webserver\*s\*-test :)​

this have not been real world names, as you can imagine. 🙂  
It war an error in the mail, of course.

> But next thing would be, variable names cannot have dashes, so you would need to rewrite you conditional using a string:
> 
> &nbsp;&nbsp;&nbsp;when: inventory\_hostname in groups["webservers-test"]

OK, thank you.

I like the [all:vars] way better now, I think.

Marc
