Inheritance of variables .. host_vars, group_vars .. a question

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 :wink:

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

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'.

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