gpt4 book ai didi

variables - Ansible 组变量优先级

转载 作者:行者123 更新时间:2023-12-02 15:14:32 24 4
gpt4 key购买 nike

假设我在 group_vars 中有 3 个文件:

abc.yml
all.yml
xyz.yml

并在其中定义相同的变量:

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

Ansible documentation说:

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 中的组顺序也会改变结果,但以不可预测的方式。

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

[all]
localhost

[xyz]
localhost

[abc]
localhost

但我仍然不明白它是如何工作的。

最佳答案

ansible 文档现在对这种行为非常清楚......

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.

关于variables - Ansible 组变量优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38120793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com