In my inventory I have
[group1]
… some machines
MachineA
[group2]
… some machines
MachineA
[group3]
… some machines
MachineA
So, I have one machine in all 3 groups.
In group_vars I have
group1
group2
group3
a variable defined in both group2 and group3 is picking up the value from group3 when I run a playbook using group2
For example the playbooks is
hosts: group2
remote_user: root
roles:
role: dochang.account ← galaxy role to setup users
If I have group_vars/group3 file in the group_vars folder that is the value that gets picked up when I run the playbook above that is for group2. If I move the group_vars/group3 file out, the correct value is used from group_vars/group2
.
ansible --version
ansible 1.9.4 (stable-1.9 4472889632) last updated 2015/10/25 18:37:06 (GMT -400)
lib/ansible/modules/core: (detached HEAD dccef8f89a) last updated 2015/08/29 23:14:50 (GMT -400)
lib/ansible/modules/extras: (detached HEAD 25595b4ecc) last updated 2015/08/29 23:14:52 (GMT -400)
configured module search path = /home/fran/ansible/library:/usr/share/ansible/
I would have never expected it. I expected that if a playbook did a group from the host file that only the matching file from group_vars/GroupName would be read.
ansible -m debug -a “var=hostvars” MachineA
Thanks. That was very helpful.
What I ended up doing to achieve what I want is to use ansible_ssh_host to fake unique names for the same machine depending on what group it is.
I would have never expected it. I expected that if a playbook did a group from the host file that only the matching file from group_vars/GroupName would be read.
Ok, at first the whole inventory topic in Ansible was weird for me, too. Especially when everything is YAML and only for the inventory there is an INI-file. And if you look at your example you can’t really map this data model to the internal (JSON) model.
So you have unique hosts with vars (which can be key/value, list, dictionaries etc.) and unique groups with vars. On the other hand you can assign hosts and groups to other groups.
What I ended up doing to achieve what I want is to use ansible_ssh_host to fake unique names for the same machine depending on what group it is.
You could also use a DNS alias. Depends on your infrastructure. We separate our servers in physical, virtual and alias.