Inventory variables

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

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.