Ansible:使用带有 vars 的嵌套组 [英] Ansible: using nested groups with vars

查看:20
本文介绍了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 variables In 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

到主机文件.从剧本中删除了所有 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:

depth 0: group all, all.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

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

You can see details and processing order here in the source code.

因此,如果您在 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 的嵌套组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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