# Wrong group\_var getting read?

**URL:** <https://forum.ansible.com/t/wrong-group-var-getting-read/19611>\
**Category:** Ansible Project\
**Tags:** galaxy-ng\
**Created:** [October 25, 2015, 10:47pm UTC](https://forum.ansible.com/t/wrong-group-var-getting-read/19611 "2015-10-25T22:47:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francisco\_Reyes](https://avatars.discourse-cdn.com/v4/letter/f/b77776/32.png) [@Francisco\_Reyes](https://forum.ansible.com/u/Francisco_Reyes)\
**Post date:** [October 25, 2015, 10:47pm UTC](https://forum.ansible.com/t/wrong-group-var-getting-read/19611/1 "2015-10-25T22:47:09Z")

</div>

Puzzled with this one.

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/

Any ideas or suggestions? is this a bug?

---

<div class="post-metadata">

**Author:** ![esco\_real](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@esco\_real](https://forum.ansible.com/u/esco_real)\
**Post date:** [October 26, 2015, 9:20am UTC](https://forum.ansible.com/t/wrong-group-var-getting-read/19611/2 "2015-10-26T09:20:05Z")

</div>

This is an expected behavior. The “hosts” entry in the playbook is just for limitation. The vars were build before that. To test what vars a host get:

ansible -m debug -a “var=hostvars” MachineA

---

<div class="post-metadata">

**Author:** ![Francisco\_Reyes](https://avatars.discourse-cdn.com/v4/letter/f/b77776/32.png) [@Francisco\_Reyes](https://forum.ansible.com/u/Francisco_Reyes)\
**Post date:** [October 27, 2015, 3:07am UTC](https://forum.ansible.com/t/wrong-group-var-getting-read/19611/3 "2015-10-27T03:07:10Z")

</div>

> This is an expected behavior.

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.

---

<div class="post-metadata">

**Author:** ![esco\_real](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@esco\_real](https://forum.ansible.com/u/esco_real)\
**Post date:** [October 27, 2015, 12:46pm UTC](https://forum.ansible.com/t/wrong-group-var-getting-read/19611/4 "2015-10-27T12:46:07Z")

</div>

> 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.

The data model for inventory:  
[http://docs.ansible.com/ansible/developing\_inventory.html](http://docs.ansible.com/ansible/developing_inventory.html)

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.
