Search code examples
variablesansibleredefinition

Ansible group vars priority


Let's say I have 3 files in group_vars:

abc.yml
all.yml
xyz.yml

And the same variable defined in them:

- my_var: abc
- my_var: all
- my_var: xyz

Ansible documentation says:

Within any section, redefining a var will overwrite the previous instance. If multiple groups have the same variable, the last one loaded wins. If you define a variable twice in a play’s vars: section, the 2nd one wins.

Does it mean that load order is alphabetical one and abc.yml has the lowest priority, whereas xyz.yml the highest, or it depends on order of groups in hosts?

What is the loading order?


It is interesting, that changing the order of groups in hosts changes results as well, but in unpredictable way.

I tried running ansible-playbook my_var.yml -c local (that only returns variable value) with all the combinations of:

[all]
localhost

[xyz]
localhost

[abc]
localhost

but I still cannot figure out how it works.


Solution

  • The ansible documentation is quite clear on this behaviour now...

    https://docs.ansible.com/ansible/2.6/user_guide/intro_inventory.html#how-variables-are-merged

    When groups of the same parent/child level are merged, it is done alphabetically, and the last group loaded overwrites the previous groups. For example, an a_group will be merged with b_group and b_group vars that match will overwrite the ones in a_group.

    Starting in Ansible version 2.4, users can use the group variable ansible_group_priority to change the merge order for groups of the same level (after the parent/child order is resolved). The larger the number, the later it will be merged, giving it higher priority. This variable defaults to 1 if not set. For example:

    a_group:
        testvar: a
        ansible_group_priority: 10
    b_group
        testvar: b
    

    In this example, if both groups have the same priority, the result would normally have been testvar == b, but since we are giving the a_group a higher priority the result will be testvar == a.