# Best way of writting inventory file

**URL:** <https://forum.ansible.com/t/best-way-of-writting-inventory-file/22642>\
**Category:** Ansible Project\
**Created:** [September 26, 2016, 8:22am UTC](https://forum.ansible.com/t/best-way-of-writting-inventory-file/22642 "2016-09-26T08:22:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [September 26, 2016, 8:22am UTC](https://forum.ansible.com/t/best-way-of-writting-inventory-file/22642/1 "2016-09-26T08:22:25Z")

</div>

Hello,

I have an inventory file to install for one user many ways, it looks like this:

[USER1\_DEV1]  
PARAM=“[app1’,‘1.0.1’,‘1’]”

[USER1\_DEV2]  
PARAM=“[‘app1’,‘1.1.0’,‘5’]”

[USER1\_REC1]  
PARAM=“[‘app1’,‘1.0.4’,‘2’]”

[USER1\_REC2]  
PARAM=“[‘app1’,‘1.1.0’,‘6’]”

[USER1\_PREREC1]  
PARAM=“[‘app1’,‘1.0.3’,‘3’]”

[USER1\_PREREC2]  
PARAM=“[‘app1’,‘1.0.3’,‘7’]”

[USER1\_DEV:children]  
USER1\_DEV1  
USER1\_DEV2

[USER1\_PREREC:children]  
USER1\_PREREC1  
USER1\_PREREC2

[USER1\_REC:children]  
USER1\_REC1  
USER1\_REC2

[user1:children]  
USER1\_DEV  
USER1\_REC  
USER1\_PREREC

[linux1:children]  
user[1:10]  
produser[70:72]

[user1:vars]  
ansible\_user=user1

[linux1:vars]  
ansible\_host=[linux1.fr.com](http://linux1.fr.com)

[webservers:children]  
linux1

So when running the ansible-playbook command, I do use the -l option to decide what to deploy (-l USER1\_REC in example).  
The issue is, that I have 10 users and 10 produsers, all working the same way so I would have to duplicate all these lines again and again like:

[user2:children]  
USER2\_DEV  
USER2\_REC  
USER2\_PREREC

etc …  
but only the first lines are different:

[USER1\_DEV1]  
PARAM=“[app1’,‘1.0.1’,‘1’]”

[USER2\_DEV1]  
PARAM=“[app1’,‘2.0.1’,‘1’]”

[USER1\_DEV2]  
PARAM=“[app1’,‘1.0.1’,‘1’]”

[USER2\_DEV2]  
PARAM=“[app1’,‘1.1.1’,‘1’]”

Is there a simple way to proceed for not writing all these lines ? Shall I try to use a dynamic inventory file , could someone give me an example ?  
Regards

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [September 26, 2016, 1:08pm UTC](https://forum.ansible.com/t/best-way-of-writting-inventory-file/22642/2 "2016-09-26T13:08:29Z")

</div>

Have a look at using separate group\_vars files for common variables you can apply to more than one machine - see [http://docs.ansible.com/ansible/intro\_inventory.html#splitting-out-host-and-group-specific-data](http://docs.ansible.com/ansible/intro_inventory.html#splitting-out-host-and-group-specific-data)  
Also, consider having different inventory files. I have a different inventory for development, testing and each production environment.

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 26, 2016, 1:10pm UTC](https://forum.ansible.com/t/best-way-of-writting-inventory-file/22642/3 "2016-09-26T13:10:17Z")

</div>

Using group/host\_vars is a good idea, even so, this looks incorrect:

[USER1\_DEV1]  
PARAM=“[app1’,‘1.0.1’,‘1’]”

It should be:

[USER1\_DEV1:vars]

PARAM=“[app1’,‘1.0.1’,‘1’]”

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [September 26, 2016, 2:41pm UTC](https://forum.ansible.com/t/best-way-of-writting-inventory-file/22642/4 "2016-09-26T14:41:19Z")

</div>

Thank you,

We might use many inventory files, depending on DEV, REC and PROD environments.  
I did correct the inventory file, instead of setting [USER1\_DEV1:vars], I did add an alias:

[USER1\_DEV1]  
USER1\_DEV1 PARAM=“[app1’,‘1.0.1’,‘1’]”

so when running the ansible-playbook to list the different hosts, the list contains only the more detailed ones USER1\_DEV1, USER2\_DEV1 …

Regards
