# Vars question, bug or by design?

**URL:** <https://forum.ansible.com/t/vars-question-bug-or-by-design/12465>\
**Category:** Ansible Project\
**Created:** [March 7, 2013, 3:13am UTC](https://forum.ansible.com/t/vars-question-bug-or-by-design/12465 "2013-03-07T03:13:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Hoffman](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@Chris\_Hoffman](https://forum.ansible.com/u/Chris_Hoffman)\
**Post date:** [March 7, 2013, 3:13am UTC](https://forum.ansible.com/t/vars-question-bug-or-by-design/12465/1 "2013-03-07T03:13:48Z")

</div>

I have been using nested vars in my group variables and ran into an issue when trying to nest variables in my playbooks. I have my ansible.cfg set to merge hashes (hash\_behavior=merge). This works great for group and host vars but when I put include a vars hash in my playbooks, it overwrites the whole hash with the variables in the playbook even when none of the hash keys are shared with the group/host vars. Is this expected behavior?

I dug into the code a bit and realized it would be a bit tricky to get this to work as I would expect it. Before I dig any further, I just want to make sure this is not expected behavior.

Thanks,  
Chris

---

<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:** [March 7, 2013, 4:49am UTC](https://forum.ansible.com/t/vars-question-bug-or-by-design/12465/2 "2013-03-07T04:49:46Z")

</div>

hash merging is pretty new in Ansible, I'd say it's reasonable to  
assume the behavior for that was completely undefined, but it seems  
more logical if everything merged in that case.

It might be useful to isolate the 'will-it-blend' behavior to a  
function in utils that checks the config setting, rather than having  
this in more than one place.

---

<div class="post-metadata">

**Author:** ![Michel\_Blanc1](https://avatars.discourse-cdn.com/v4/letter/m/fbc32d/32.png) [@Michel\_Blanc1](https://forum.ansible.com/u/Michel_Blanc1)\
**Post date:** [March 7, 2013, 7:57am UTC](https://forum.ansible.com/t/vars-question-bug-or-by-design/12465/3 "2013-03-07T07:57:29Z")

</div>

> > I have been using nested vars in my group variables and ran into an issue  
> > when trying to nest variables in my playbooks. I have my ansible.cfg set to  
> > merge hashes (hash\_behavior=merge). This works great for group and host  
> > vars but when I put include a vars hash in my playbooks, it overwrites the  
> > whole hash with the variables in the playbook even when none of the hash  
> > keys are shared with the group/host vars. Is this expected behavior?

> hash merging is pretty new in Ansible, I'd say it's reasonable to  
> assume the behavior for that was completely undefined, but it seems  
> more logical if everything merged in that case.

I'm the guy to slap for this. Christopher, could you file a ticket ?

Yes, it defitively should merge hashes in a logical order, which is  
described here  
[http://ansible.cc/docs/playbooks2.html?highlight=precedence#understanding-variable-precedence](http://ansible.cc/docs/playbooks2.html?highlight=precedence#understanding-variable-precedence)  
(the later overrides the former). Here is how I understand things.

Please correct me when I'm wrong :

1/ Any variables specified with �extra-vars (-e) on the ansible-playbook  
command line.

I don't think complex vars can be defined here (?)

2/ Variables loaded from YAML files mentioned in �vars\_files� in a playbook.

I suppose complex vars can be defined here.  
If this is the first source of complex vars, the merge only happens  
after this step.

3/ facts, whether built in or custom, or variables assigned from the  
�register� keyword.

Should merge over previous vars.

4/ variables passed to parameterized task include statements

I don't think parameters to tasks can be complex (?).

5/ �vars� as defined in the playbook.

Should merge over previous vars.

6/ Host variables from inventory.

As far as the discussion went, Michael doesn't want complex vars to be  
defined at that level. However, I have a patch here, so if this sounds  
reasonable in the end, I can push that. Since this would be new, it  
probably can't harm existing pbooks. It seems reasonable to me to make  
things behave consistently here too.

7/ Group variables from inventory in inheritance order

This is the only place where merging works for now.

Am I getting it right here ?

BTW I don't understand why 2/ and 5/ are so far away, since they are two  
different ways of defining the same thing (one "modular", one "inline").

> > when I put include a vars hash in my playbooks, it overwrites the  
> > whole hash with the variables in the playbook even when none of the hash  
> > keys are shared with the group/host vars. Is this expected behavior?

You mean var hash in the playbook (5) overrides var hash defined in  
group/host (7) vars ? It doesn't seem to comply with the order defined  
in the docs.

> It might be useful to isolate the 'will-it-blend' behavior to a  
> function in utils that checks the config setting, rather than having  
> this in more than one place.

Sure. I didn't update the docs on this either (there's only a blurb in  
example/ansible.cfg) and this is definitively missing. I can take a look  
at this once the issue is settled.

M

---

<div class="post-metadata">

**Author:** ![Chris\_Hoffman](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@Chris\_Hoffman](https://forum.ansible.com/u/Chris_Hoffman)\
**Post date:** [March 7, 2013, 2:08pm UTC](https://forum.ansible.com/t/vars-question-bug-or-by-design/12465/4 "2013-03-07T14:08:10Z")

</div>

> You mean var hash in the playbook (5) overrides var hash defined in  
> group/host (7) vars ? It doesn’t seem to comply with the order defined  
> in the docs.

I may have the order of which overwrites which incorrect since I have already worked around this issue. As for precedence, I think I have seen it suggested to not rely on the precedence and it not being a best practice, so I am not using it. All my hash keys are mutually exclusive between my group/host vars and playbook vars, they are just defined at a different level in my hierarchy.

> I’m the guy to slap for this. Christopher, could you file a ticket ?

No slaps necessary, I will file a ticket.

---

<div class="post-metadata">

**Author:** ![Chris\_Hoffman](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@Chris\_Hoffman](https://forum.ansible.com/u/Chris_Hoffman)\
**Post date:** [March 7, 2013, 2:11pm UTC](https://forum.ansible.com/t/vars-question-bug-or-by-design/12465/5 "2013-03-07T14:11:36Z")

</div>

Issue created [https://github.com/ansible/ansible/issues/2329](https://github.com/ansible/ansible/issues/2329)
