# Inheritance of variables .. host\_vars, group\_vars .. a question

**URL:** <https://forum.ansible.com/t/inheritance-of-variables-host-vars-group-vars-a-question/19589>\
**Category:** Ansible Project\
**Created:** [October 22, 2015, 3:15pm UTC](https://forum.ansible.com/t/inheritance-of-variables-host-vars-group-vars-a-question/19589 "2015-10-22T15:15:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcus\_Franke](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@Marcus\_Franke](https://forum.ansible.com/u/Marcus_Franke)\
**Post date:** [October 22, 2015, 3:15pm UTC](https://forum.ansible.com/t/inheritance-of-variables-host-vars-group-vars-a-question/19589/1 "2015-10-22T15:15:16Z")

</div>

Hello,

I’ve got a big problem with host and group variables in my playbook and Im not  
sure if I am just plain stupid or if I am running into a bug or am abusing  
ansible in some kind 😉

Some background, I use ansible 1.9.4 installed from brew on OS X.

I have a group of currently two servers and I would like to configure mariadb  
service. As I have lots of parameters for mariadb I tried to split them into  
parameters for both servers and put them into a group variable file and those  
exclusive for the single instance are in the host\_vars config file:

#\> grep -A 3 mariadb group\_vars/api.yaml  
mariadb:  
version: 10.0  
query\_cache\_size: 67108864  
log\_bin\_dir: mariadb-binlogs

#\> grep -A 2 mariadb host\_vars/api02.yaml  
mariadb:  
server\_id: 102  
is\_master: false

What I expected to happen, was that both arrays will be merged when I run the  
playbook, but instead my playbook bailes out with an missing variable error.  
First I thought ansible would miss the group of my server, but variables from  
the all.yaml group file are being loaded.

Using “debug: var=mariadb” I verified the content of the mariadb array to this:  
TASK: [mariadb | debug var=mariadb] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [api02] =\> {  
“var”: {  
“mariadb”: {  
“is\_master”: false,  
“server\_id”: 102  
}  
}  
}

So, as I learned, the mariadb array from the group\_vars file got replaced by  
the one from the host\_vars file. I guess my expectation that those arrays would  
be merged into one large array is plain wrong or did I trigger a bug?

Any ideas on how to to achieve the merge behaviour? It would save me a lot of  
typing and variable duplication if the arrays would be merged instead of being  
replaced… with two database servers in the group its ok, but if I have to  
configure larger groups with for example a dozen servers I would have to copy  
over the whole array.

Greets,  
Marcus

---

<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:** [October 22, 2015, 3:21pm UTC](https://forum.ansible.com/t/inheritance-of-variables-host-vars-group-vars-a-question/19589/2 "2015-10-22T15:21:50Z")

</div>

this is the default behaviour, ansible overwrites vars when there is a  
conflict, the most specific one or last loaded wins.

in ansible.cfg the 'hash\_behaviour' setting can change this, switch  
the value from the default 'replace' to 'merge'.

---

<div class="post-metadata">

**Author:** ![Marcus\_Franke](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@Marcus\_Franke](https://forum.ansible.com/u/Marcus_Franke)\
**Post date:** [October 22, 2015, 3:57pm UTC](https://forum.ansible.com/t/inheritance-of-variables-host-vars-group-vars-a-question/19589/3 "2015-10-22T15:57:38Z")

</div>

Thanks for the quick answer, this is great to know!

Sorry for sending the mail three times, my mail client reported an error when sending.

Greetings,  
Marcus
