# Inventory variables

**URL:** <https://forum.ansible.com/t/inventory-variables/14640>\
**Category:** Ansible Project\
**Created:** [February 24, 2014, 2:06pm UTC](https://forum.ansible.com/t/inventory-variables/14640 "2014-02-24T14:06:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stuart\_Golightly](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Stuart\_Golightly](https://forum.ansible.com/u/Stuart_Golightly)\
**Post date:** [February 24, 2014, 2:06pm UTC](https://forum.ansible.com/t/inventory-variables/14640/1 "2014-02-24T14:06:48Z")

</div>

Afternoon all

I’ve got a bunch of servers, each running two instances of an application, which results in a mapping like this:

serverA inst-1

serverA inst-2

serverB inst-3

serverB inst-4

serverC inst-5

serverC inst-6

I was hoping to use inventory vars to do some work on the instances, so I thought of using an inventory file looking like this:

serverA inst\_name=inst-1

serverA inst\_name=inst-2

serverB inst\_name=inst-3

serverB inst\_name=inst-4

serverC inst\_name=inst-5

serverC inst\_name=inst-6

Looking through the variable documentation, this idea seems doomed to failure, as the second var definition appears to overwrite the first, meaning that only “inst-2”, “inst-4” and “inst-6” get processed. Running this inventory through the debug module gives that result…

[sgolightly@XXX ansible]$ ansible -i ./inventories/var-test-inventory.hosts all -m debug -a “var={{inst\_name}}”

serverC | success \>\> {

“inst-6”: “{{ inst-6 }}”

}

serverA | success \>\> {

“inst-2”: “{{ inst-2 }}”

}

serverB | success \>\> {

“inst-4”: “{{ inst-4 }}”

}

[sgolightly@XXX ansible]$

Apart from creating two inventory files – one for odd numbered instances and one for even – has anyone got any bright ideas how this kind of situation can be handled. Or is two inventory files the easiest and best way?

The instance names are used in directory names, config files and the like, so the names have to stay.

Stuart

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 24, 2014, 7:25pm UTC](https://forum.ansible.com/t/inventory-variables/14640/2 "2014-02-24T19:25:26Z")

</div>

Read the docs about “group\_vars/” and “host\_vars/” directories.

This is a better way to organize variables than embedding them in the file for nearly all cases.
