# Variable precedence in a group hierarchy

**URL:** <https://forum.ansible.com/t/variable-precedence-in-a-group-hierarchy/18005>\
**Category:** Ansible Project\
**Created:** [April 17, 2015, 3:31pm UTC](https://forum.ansible.com/t/variable-precedence-in-a-group-hierarchy/18005 "2015-04-17T15:31:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandra.kosmalla](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@sandra.kosmalla](https://forum.ansible.com/u/sandra.kosmalla)\
**Post date:** [April 17, 2015, 3:31pm UTC](https://forum.ansible.com/t/variable-precedence-in-a-group-hierarchy/18005/1 "2015-04-17T15:31:47Z")

</div>

Hello list,

I need to understand following variable precedence problem.  
I have an inventory file with following group hierarchy:

`

# inventory file

host1  
host2

[group1]  
host1  
host2

[group11:children]  
group1

[group12:children]  
group1  
`

In the group\_var files of group _group11_ and _group12,_ a variable _var_ is defined with different value:

`

# content of group\_vars/group11

var: “group11”

`

`

`

# content of group\_vars/group12

var: “group12”

`

`

Furthermore, I have two playbooks that evaluate the variable _var_ . One playbook is for the host group _group11_ and the second playbook is for _group12_

`

`  
#content of playbook site-group11.yml

`

- hosts: group11  
tasks:
- debug: var=var

`  
#content of playbook site-group12.yml

`

- hosts: group12  
tasks:
- debug: var=var  
`

When I run both playbooks, I get the following output from both:

`  
\< TASK: debug var=var \>

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [April 17, 2015, 3:42pm UTC](https://forum.ansible.com/t/variable-precedence-in-a-group-hierarchy/18005/2 "2015-04-17T15:42:33Z")

</div>

​yes, it's a feature, not a bug 🙂  
Io just answered the same question on  
[https://github.com/ansible/ansible/issues/10755](https://github.com/ansible/ansible/issues/10755), so copy pasting my answer:

This is a feature and not a bug, and has been asked several times yet here  
and on the list. You are trying to use a way to define things that just  
isn't the way ansible dpes things.

Ansible only uses variables on the host level. The inventory only returns  
variables after they are calculated/inherited down top that host level.

When you target a play to a particular group, that gets calculated down to  
a list of hosts. Whether you call groupA or groupB here, it results in the  
same host. And that hosts only has one particular value for myvar, which  
happens to be groupB because thats the last one bing parsed.

To quote the docs:

Remember: Child groups override parent groups, and hosts always override  
their groups.

​

&nbsp;&nbsp;&nbsp;&nbsp;Serge

---

<div class="post-metadata">

**Author:** ![sandra.kosmalla](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@sandra.kosmalla](https://forum.ansible.com/u/sandra.kosmalla)\
**Post date:** [April 17, 2015, 3:55pm UTC](https://forum.ansible.com/t/variable-precedence-in-a-group-hierarchy/18005/3 "2015-04-17T15:55:06Z")

</div>

Serge, thanks for the fast clarification.
