# include\_vars and hash-behaviour

**URL:** <https://forum.ansible.com/t/include-vars-and-hash-behaviour/18194>\
**Category:** Ansible Project\
**Created:** [May 8, 2015, 6:39pm UTC](https://forum.ansible.com/t/include-vars-and-hash-behaviour/18194 "2015-05-08T18:39:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michail\_Alexakis](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Michail\_Alexakis](https://forum.ansible.com/u/Michail_Alexakis)\
**Post date:** [May 8, 2015, 6:39pm UTC](https://forum.ansible.com/t/include-vars-and-hash-behaviour/18194/1 "2015-05-08T18:39:14Z")

</div>

Hello list,

I have encountered some not expected results when i tried to include (with include\_vars) multiple var files in the beginning of a play.

I normally use the “merge” hash behavior (ansible.cfg) because it seems more natural to me: i usually have highly nested variable dicts  
and i wish to add or override just parts of them based on the group/host hierarchy.

Now, i tried to include some variables in the beginning of a play (at “pre\_tasks” phase, but i don’t thing it’s relevant), to override some  
parts of existing dicts with piece of information regarding other present hosts (e.g. IPs for standby servers). It didn’t succeed.

Consider the following example:

```auto
# vars file: play_vars/baz-a.yml
baz:
a: 1

```

```auto
# vars file: play_vars/baz-b.yml
baz:
b: 2

```

```auto
---
# playbook: play.yml
- hosts: all

pre_tasks:
- include_vars: play_vars/baz-a.yml
- include_vars: play_vars/baz-b.yml

tasks:
- debug: var=baz

```

When playing play.yml, i get: “baz”: {“b”: 2} while expecting “baz”: {“a”:1, “b”:2}.  
Is this normal? Note, this is happening under hash\_behavior=merge.

---

<div class="post-metadata">

**Author:** ![Michail\_Alexakis](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Michail\_Alexakis](https://forum.ansible.com/u/Michail_Alexakis)\
**Post date:** [May 12, 2015, 5:56pm UTC](https://forum.ansible.com/t/include-vars-and-hash-behaviour/18194/2 "2015-05-12T17:56:52Z")

</div>

Hello,

any answer to this question?

---

<div class="post-metadata">

**Author:** ![Adam\_Hamsik](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@Adam\_Hamsik](https://forum.ansible.com/u/Adam_Hamsik)\
**Post date:** [May 13, 2015, 4:41pm UTC](https://forum.ansible.com/t/include-vars-and-hash-behaviour/18194/3 "2015-05-13T16:41:11Z")

</div>

Have a look at hash\_behaviour in ansible.cfg and set it to merge. But be aware that it’s usually sign of bad design.

---

<div class="post-metadata">

**Author:** ![Michail\_Alexakis](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Michail\_Alexakis](https://forum.ansible.com/u/Michail_Alexakis)\
**Post date:** [May 13, 2015, 5:35pm UTC](https://forum.ansible.com/t/include-vars-and-hash-behaviour/18194/4 "2015-05-13T17:35:07Z")

</div>

Hello Adam,

I allready have set it to “merge”, actually that’s the reason i posted this question (because it doesn’t seem to merge).  
Have you tried the example above? I tested it under ansible==1.8.4. Maybe i am missing something obvious.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 13, 2015, 5:41pm UTC](https://forum.ansible.com/t/include-vars-and-hash-behaviour/18194/5 "2015-05-13T17:41:29Z")

</div>

there was a but in 1.8 with hash\_merge, have you tried in 1.9?

---

<div class="post-metadata">

**Author:** ![Michail\_Alexakis](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Michail\_Alexakis](https://forum.ansible.com/u/Michail_Alexakis)\
**Post date:** [May 13, 2015, 6:23pm UTC](https://forum.ansible.com/t/include-vars-and-hash-behaviour/18194/6 "2015-05-13T18:23:17Z")

</div>

Hello Brian,

I just tested the above with a freshly installed ansible==1.9.1, and the problem seems to be still here.  
I played the example posted above accompanied with a simple ansible.cfg:

```auto
[defaults]
hash_behaviour = merge

```

Cant you reproduce it?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 13, 2015, 7:22pm UTC](https://forum.ansible.com/t/include-vars-and-hash-behaviour/18194/7 "2015-05-13T19:22:43Z")

</div>

So I looked and include\_vars works by returning ansible\_facts which  
bypasses, on purpose, hash merging as facts should overwrite  
themselves. The way this currently CAN work ... its not clear how we  
can use the merge,I'll add notes to the docs that include\_vars always  
overwrites.

---

<div class="post-metadata">

**Author:** ![Michail\_Alexakis](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Michail\_Alexakis](https://forum.ansible.com/u/Michail_Alexakis)\
**Post date:** [May 13, 2015, 7:40pm UTC](https://forum.ansible.com/t/include-vars-and-hash-behaviour/18194/8 "2015-05-13T19:40:57Z")

</div>

Ok, i understand, seems that include\_vars works like set\_fact in this place.

Thanks, Brian
