gpt4 book ai didi

Ansible group_vars 文件匹配多个组

转载 作者:行者123 更新时间:2023-12-04 02:43:50 31 4
gpt4 key购买 nike

这可能是一个奇怪的问题,我确信我已经看到了这个问题,但忽略了将其添加为书签。

我的主机存在于多个组中:

[group1]
host1

[group2]
host2

[group3]
host1
host3

我想为这些主机设置组变量文件(这是简单明显的部分):

group_vars/group1.yml
group_vars/group2.yml
group_vars/group3.yml

,并使用以下模式对存在的主机的变量进行分组:

group1:&group3

这可以用 group_vars 文件来完成吗?示例:

group_vars/group1:&group3.yml

我确定我见过具有这种模式样式的文件名,但我不知道如何让它工作。

最佳答案

(编辑:这个答案大部分仍然有效,但下面有更新)

documentation 的摘录

Note:

Group loading follows parent/child relationships. Groups of the same ‘parent/child’ level are then merged following alphabetical order. This last one can be superceeded by the user via ansible_group_priority, which defaults to 1 for all groups. This variable, ansible_group_priority, can only be set in the inventory source and not in group_vars/ as the variable is used in the loading of group_vars/.

因此,基于此,您几乎可以做的就是:

  • 将 group3 作为 group1 和 group2 的父组。为 group3 定义的变量将自动用于 group1 和 group2:

    [group1]
    host1

    [group2]
    host2

    [group3:children]
    group1
    group2
  • 在任何地方定义变量并依赖于它们的加载顺序,或者使用 ansible_group_priority 在您的库存源中修复此顺序。

现在如果你想根据你在游戏中的目标组使用不同的变量值,那就算了:无论你的目标是什么,ansible 都会从你的 list /剧本中加载所有组的所有变量.

我建议你深入阅读 explanations regarding where to place a variable


(更新)

自从我上面的回答以来,情况发生了一些变化。虽然结论并没有真正改变,而且你仍然无法根据你游戏中的 hosts 目标来决定获取哪个变量值,但 ansible 引入了一些灵 active 让你定义一个 ansible_group_priority 以在库存解析时覆盖默认的 ASCII 顺序。

请注意,此变量只能在 list 源中设置,不能在group_vars 文件中设置

现在在文档的新部分对此进行了解释:How variables are merged .相关摘录:

The order/precedence is (from lowest to highest):

  • all group (because it is the ‘parent’ of all other groups)
  • parent group
  • child group
  • host

By default Ansible merges groups at the same parent/child level in ASCII order, 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.

You can change this behavior by setting 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.

关于Ansible group_vars 文件匹配多个组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58060653/

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