Ansible组vars优先级 [英] Ansible group vars priority

查看:514
本文介绍了Ansible组vars优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我在group_vars中有3个文件:

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 文档说:

Ansible documentation says:

在任何部分中,重新定义var都将覆盖前一个实例.如果多个组具有相同的变量,则最后一个加载者获胜.如果您在剧本的vars:部分中定义了两次变量,则第二个变量将获胜.

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.

是否表示加载顺序为字母顺序,并且abc.yml具有最低优先级,而xyz.yml具有最高优先级,还是取决于hosts中的组顺序?

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?

什么是加载顺序?

有趣的是,更改hosts中的组顺序也会改变结果,但方式不可预测.

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

我尝试使用以下所有组合运行ansible-playbook my_var.yml -c local(仅返回变量值):

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.

推荐答案

现在,有关此行为的清晰文档非常清晰...

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

合并具有相同父/子级别的组时,将按字母顺序进行操作,并且最后加载的组将覆盖先前的组.例如,a_group将与b_group合并,并且匹配的b_group变量将覆盖a_group中的变量.

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.

从Ansible 2.4版开始,用户可以使用组变量ansible_group_priority更改同一级别的组的合并顺序(在解决父/子顺序之后).数字越大,合并的时间越晚,优先级越高.如果未设置,则此变量默认为1.例如:

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

在此示例中,如果两个组具有相同的优先级,则结果通常为testvar == b,但是由于我们给a_group更高的优先级,结果将为testvar == a.

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.

这篇关于Ansible组vars优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆