Ansible group vars priority

自古美人都是妖i 提交于 2020-01-23 05:10:41

问题


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.


回答1:


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.




回答2:


I would say Ansible's behavior in this case is unspecified.




回答3:


It looks like the following issue, from the Ansible git repository, may have addressed the behaviour you describe: Ansible group_vars from inventory don't support repeated hosts

Looking at the code changes from the associated commit, it looks like now groups should be sorted by depth then by name (alphabetically).

It's worth noting that this behaviour will likely vary depending on what version of Ansible you are running, as these changes were only merged into the stable-2.2 branch of the Ansible repository.



来源:https://stackoverflow.com/questions/38120793/ansible-group-vars-priority

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!