本文介绍了Ansible:将嵌套组与vars一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个情况,我们有3层盒子,在每一层中,我们应用不同的变量设置(例如缓存目录所在的位置),但是有很多默认值.我还需要基于每个节点进行覆盖,这通常是通过主机本身的清单变量来完成的.我不确定什么是组织主机的最佳方法,以便优先顺序有效.

I have a situation where we have 3 tiers of boxes, in each tier we apply different variables settings (like where the cache dir is), but there are a bunch of defaults. I also need to override on a per node basis, which is usually done via inventory vars on the host itself. I am not sure what is the best way to organize the hosts so that the precedence works in my favor.

这是我尝试过的不同方法.在每种情况下,我在清单文件中都有这样的条目:

Here are the different things I have tried. In each case I have entries in the inventory file like this:

[bots-fancy]
fancy-1

[bots-super-fancy]
super-1

[bots-magical]
magic-1
magic-2 provider=aws

起初,我每个人都有一长串变量定义.我也有不同的group_var/bots/[bots-magical | bots-super-fancy | bots-fancy].yaml文件.这很快变得站不住脚.

At first, I had each of them with a long string of variable definitions. I also had different group_var/bots/[bots-magical | bots-super-fancy | bots-fancy].yaml files. This quickly became untenable.

尝试1:使用剧本变量在剧本中,我有这样的内容:

attempt 1: with playbook variablesIn the playbook I had something like this:

---
hosts:
  - bots
vars_files:
  - "group_vars/bots/defaults.yml"
  - "group_vars/bots/{{ groups_names[0] }}.yml"
roles:
  - somethign

这有效(尽管确实很脆弱),但它不允许我按每个主机进行覆盖.我不得不偶尔在节点上设置不同的东西,而不是在整个组中.

this worked (though yes brittle) but it wouldn't let me override on a per host basis. I had to set things different on nodes occasionally, but not on the whole group.

尝试2:对每一个使用group_vars

attempt 2: using group_vars for each

我添加了

[bots:children]
bots-fancy
bots-super-fancy
bots-magical

到hosts文件.从剧本中删除任何vars_files,并为每个组创建group_vars.我将默认/共享设置添加到了group_vars/bots.yaml.当我运行剧本时,它只会加载看起来似乎是bots group_vars的文件.理想情况下,我希望它加载bots,然后用bots-fancy覆盖它.最后是主机文件中的值.

to the hosts file. Removed any vars_files from the playbook and created group_vars for each group. I added the default/shared settings to group_vars/bots.yaml. When I'd run the playbook, it would only load the bots group_vars it seemed. Ideally, I want it to load the bots and then override it with the bots-fancy. And then finally the values from the hosts file.

我不确定组织这些小组的最佳方法,因此任何输入都将非常有帮助!

I am not sure the best way to structure these groups, so any input would be very helpful!

推荐答案

不确定您的问题是什么.您应该可以:

Not sure what is your problem. You should be fine with:

主机:

[bots-a]
bot1

[bots-b]
bot2

[bots:children]
bots-a
bots-b

目录:

./group_vars/bots.yml
./group_vars/bots-a.yml
./group_vars/bots-b.yml

Ansible中有一个组深度的概念(至少在最近的版本中).在此示例中,主机bot2的组变量将按以下顺序填充:

There is a concept of group depth in Ansible (at least in recent versions). In this example, group variables for host bot2 will be populated in the following order:

深度0:组allall.yml(此处遗漏,忽略)
深度1:组botsbots.yml
深度2:组bots-bbots-b.yml

depth 0: group all, all.yml (missing here, ignoring)
depth 1: group bots, bots.yml
depth 2: group bots-b, bots-b.yml

您可以在此处查看详细信息和处理顺序在源代码中.

因此,如果您在bots.yml中定义默认值,并在bots-b.yml中定义特定值,则应该达到预期的效果.

So if you define defaults in bots.yml and specific values in bots-b.yml, you should achieve what you expect.

这篇关于Ansible:将嵌套组与vars一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 00:05