# Question regarding host group variables for intersections of groups

**URL:** <https://forum.ansible.com/t/question-regarding-host-group-variables-for-intersections-of-groups/12280>\
**Category:** Ansible Project\
**Created:** [February 1, 2013, 9:58pm UTC](https://forum.ansible.com/t/question-regarding-host-group-variables-for-intersections-of-groups/12280 "2013-02-01T21:58:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arne\_Jacobs](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Arne\_Jacobs](https://forum.ansible.com/u/Arne_Jacobs)\
**Post date:** [February 1, 2013, 9:58pm UTC](https://forum.ansible.com/t/question-regarding-host-group-variables-for-intersections-of-groups/12280/1 "2013-02-01T21:58:06Z")

</div>

If a host is in multiple groups, which have different values for the same variable key, what value takes precedence for that host? Is it based on the order in the inventory? I couldn’t find anything in the documentation.

Also, do variables from the inventory take precedence over variables in plays or vice versa?

And one other thing: regarding playbook includes, the documentation states:  
“(In addition to the explicitly passed in parameters, all variables from the vars section are also available for use here as well.)”

Does this mean that all variables defined in a play A are visible within plays included by A? And if yes, do local variables of the includes plays take precedence over the variables in A?

I probably could test all this, but I would feel more comfortable if it was clearly defined somewhere.

I’m asking these questions because I like to think about host groups as a class hierarchy, which makes me think that variables defined in child groups (aka subclass), e.g., would take precedence over those in their parent (aka superclass). And with overlapping groups, you get something like multiple inheritance, where you want to be cautious with variables common to multiple superclasses…

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 1, 2013, 10:01pm UTC](https://forum.ansible.com/t/question-regarding-host-group-variables-for-intersections-of-groups/12280/2 "2013-02-01T22:01:32Z")

</div>

> If a host is in multiple groups, which have different values for the same  
> variable key, what value takes precedence for that host? Is it based on the  
> order in the inventory? I couldn't find anything in the documentation.

Most specific groups (groups of the deepest depth) win if there are subgroups.

Behavior for peer level groups is undefined but presumably  
alphabetical. You shouldn't rely on it.

> Also, do variables from the inventory take precedence over variables in  
> plays or vice versa?

Should be covered here:  
[http://ansible.cc/docs/playbooks2.html#understanding-variable-precedence](http://ansible.cc/docs/playbooks2.html#understanding-variable-precedence)

> And one other thing: regarding playbook includes, the documentation states:  
> "(In addition to the explicitly passed in parameters, all variables from the  
> vars section are also available for use here as well.)"
> 
> Does this mean that all variables defined in a play A are visible within  
> plays included by A? And if yes, do local variables of the includes plays  
> take precedence over the variables in A?

plays don't actually include plays

playbooks include plays

playbooks are just lists of plays and don't have variables at all.

> I'm asking these questions because I like to think about host groups as a  
> class hierarchy, which makes me think that variables defined in child groups  
> (aka subclass), e.g., would take precedence over those in their parent (aka  
> superclass). And with overlapping groups, you get something like multiple  
> inheritance, where you want to be cautious with variables common to multiple  
> superclasses...

this happens.

---

<div class="post-metadata">

**Author:** ![Arne\_Jacobs](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Arne\_Jacobs](https://forum.ansible.com/u/Arne_Jacobs)\
**Post date:** [February 1, 2013, 11:25pm UTC](https://forum.ansible.com/t/question-regarding-host-group-variables-for-intersections-of-groups/12280/3 "2013-02-01T23:25:01Z")

</div>

> Most specific groups (groups of the deepest depth) win if there are subgroups.

Ah great, so this is just like you would expect thinking about it as a class hierarchy…

> > Also, do variables from the inventory take precedence over variables in  
> > plays or vice versa?
> 
> Should be covered here:  
> [http://ansible.cc/docs/playbooks2.html#understanding-variable-precedence](http://ansible.cc/docs/playbooks2.html#understanding-variable-precedence)

Ok, I assume, “6. Group variables from inventory, in order of least specific group to most specific” actually means “in order of most specific to least specific”, as in that list, priority is in descending order, right?

> plays don’t actually include plays
> 
> playbooks include plays
> 
> playbooks are just lists of plays and don’t have variables at all.

Ah thanks, didn’t get the difference between plays and playbooks and also not between include tasks and included playbooks…

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 2, 2013, 4:20am UTC](https://forum.ansible.com/t/question-regarding-host-group-variables-for-intersections-of-groups/12280/4 "2013-02-02T04:20:13Z")

</div>

> > Most specific groups (groups of the deepest depth) win if there are  
> > subgroups.
> 
> Ah great, so this is just like you would expect thinking about it as a class  
> hierarchy...
> 
> > \>  
> > \> Also, do variables from the inventory take precedence over variables in  
> > \> plays or vice versa?  
> > \>
> > 
> > Should be covered here:  
> > [http://ansible.cc/docs/playbooks2.html#understanding-variable-precedence](http://ansible.cc/docs/playbooks2.html#understanding-variable-precedence)
> 
> Ok, I assume, "6. Group variables from inventory, in order of least specific  
> group to most specific" actually means "in order of most specific to least  
> specific", as in that list, priority is in descending order, right?

It means the child-most groups have greater priority in overriding  
than parent groups (like US vs NC vs Raleigh, vars set in Raleigh have  
more priority than NC). This has confused others so I need to  
rephrase this part to be more explicit for sure.
