# inventory name as variable

**URL:** <https://forum.ansible.com/t/inventory-name-as-variable/13299>\
**Category:** Ansible Project\
**Created:** [August 14, 2013, 8:57am UTC](https://forum.ansible.com/t/inventory-name-as-variable/13299 "2013-08-14T08:57:04Z")\
**Posts on this page:** 8\
**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:** [August 14, 2013, 8:57am UTC](https://forum.ansible.com/t/inventory-name-as-variable/13299/1 "2013-08-14T08:57:04Z")

</div>

Hi,

I created two inventories: "test" and "production".  
test includes groups like "webserver-test", as production are "webserver-prod".

A configuration file differs from test to production.

I created files like files/test-conf.file and files/prod-conf.file and want to use the test-\* files for inventory test and prod-\* for production.  
Can I check for the inventory name a host is in or is there a better way to achieve what I want?

Marc

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 14, 2013, 11:14am UTC](https://forum.ansible.com/t/inventory-name-as-variable/13299/2 "2013-08-14T11:14:52Z")

</div>

The best thing to do is to use groups and put your hosts in different groups.

when: inventory\_hostname in groups.webservers

Or even better, target with the play:

hosts: production:&webservers

This says target webservers and machines that are in production.

This is also equivalent to

hosts: webservers

with --limit production

on the CLI

In 1.3, there is also a variable “inventory\_file” that will tell you the path to the file

however you don’t really want to do that unless you need to know the path, paths shouldn’t be significant.

---

<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:** [August 14, 2013, 1:40pm UTC](https://forum.ansible.com/t/inventory-name-as-variable/13299/3 "2013-08-14T13:40:58Z")

</div>

Michael,

Michael DeHaan schrieb (14.08.2013 13:14 Uhr):

> The best thing to do is to use groups and put your hosts in different groups.

I like this one:

> when: inventory\_hostname in groups.webservers

This playbook fails with  
"TASK: [copy linux-init.ldif test]

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 14, 2013, 5:52pm UTC](https://forum.ansible.com/t/inventory-name-as-variable/13299/4 "2013-08-14T17:52:18Z")

</div>

Sounds like you don’t have a group named webservers and should use another name.

---

<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:** [August 15, 2013, 6:12am UTC](https://forum.ansible.com/t/inventory-name-as-variable/13299/5 "2013-08-15T06:12:50Z")

</div>

Michael,

Michael DeHaan schrieb (14.08.2013 19:52 Uhr):

> Sounds like you don't have a group named webservers and should use another name.

my group is "lsm-test" which works in hosts:

> &nbsp;&nbsp;&nbsp;&nbsp;"---  
> &nbsp;&nbsp;&nbsp;&nbsp;- hosts:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- lsm-test  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- lsm-prod  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: no
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: stop slapd  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;service: name=ldap state=stopped  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: slapd\_is\_stopped
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: copy linux-init.ldif test  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src=files/test-linux-init.ldif  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest=/etc/openldap/linux-init.\_\_ldif  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: inventory\_hostname in groups.lsm-test  
> &nbsp;&nbsp;&nbsp;&nbsp;..."

Marc

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 15, 2013, 12:30pm UTC](https://forum.ansible.com/t/inventory-name-as-variable/13299/6 "2013-08-15T12:30:47Z")

</div>

And you referenced “webservers” above, which is the source of the error then 🙂

---

<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:** [August 15, 2013, 12:53pm UTC](https://forum.ansible.com/t/inventory-name-as-variable/13299/7 "2013-08-15T12:53:44Z")

</div>

Michael DeHaan schrieb (15.08.2013 14:30 Uhr):

> And you referenced "webservers" above, which is the source of the error then 🙂

this was only a cite from your mail, an example. That may caused the misunderstanding.  
My playbook is below the error message in the same mail. There is no "webservers" in there.

Marc

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 15, 2013, 1:17pm UTC](https://forum.ansible.com/t/inventory-name-as-variable/13299/8 "2013-08-15T13:17:05Z")

</div>

Ok, please make sure there is a bug filed in github and we’ll take a look!
