如何在 Ansible 的不同库存之间共享 group_vars? [英] How to share group_vars between different inventories in Ansible?

查看:34
本文介绍了如何在 Ansible 的不同库存之间共享 group_vars?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ansible 最佳实践文档建议分开库存:

<前>库存/生产/hosts.ini # 生产服务器的清单文件group_vars/group1 # 在这里我们将变量分配给特定的组组2#""主机变量/hostname1 # 如果系统需要特定的变量,把它们放在这里主机名2#""分期/hosts.ini # 暂存环境的清单文件group_vars/group1 # 在这里我们将变量分配给特定的组组2#""主机变量/stagehost1 # 如果系统需要特定的变量,把它们放在这里stagehost2 #""

我的暂存环境和生产环境的结构相同.我在两个环境中都有相同的组.事实证明,对于相同的组,我也有相同的 group_vars.这意味着我想消除冗余.

有没有办法在不同的库存之间共享一些 group_vars?

作为一种解决方法,我开始将共享的 group_vars 放入角色中.

my_var:我的组:- { var1: 1, var2: 2 }

这使得通过将主机组与定义的变量相交来迭代一些变量成为可能:

with_items: "{{group_names | intersect(my_var.keys())}}"

但这有点难以理解,我认为角色不应该对组有任何了解.

我想将大部分库存分开,但以一种易于理解的方式分享一些 group_vars.是否可以将全局 group_vars 与库存特定的 group_vars 合并?

解决方案

我放弃了遵循 Ansible 建议的想法.现在一年后,我确信 Ansible 的建议对我的要求没有用.相反,我认为重要的是在不同阶段之间尽可能多地分享.

现在我将所有库存放在同一个目录中:

production.ini参考文件

并且我会注意每个清单都定义了一个组,其中包含具有舞台名称的所有主机.

文件 production.ini 有组 production:

[生产:儿童]all_production_hosts

并且文件reference.ini有组reference:

[参考:儿童]all_reference_hosts

我只有一个 group_vars 目录,我在其中为每个暂存组定义了一个文件:

group_vars/production.ymlgroup_vars/reference.yml

并且每个文件定义了一个 stage 变量.文件 production.yml 定义了这个:

---阶段:生产

文件 reference.yml 定义了:

---阶段:参考

这使得在生产和参考之间共享所有其他内容成为可能.但是楼主完全不一样.通过使用正确的清单,剧本可以在生产或参考主机上运行:

ansible-playbook -i production.ini site.ymlansible-playbook -i reference.ini site.yml

如果 site.yml 或角色在生产和参考环境中的行为有必要略有不同,他们可以使用使用 stage 变量的条件.但我尽量避免这种情况.因为最好将所有差异移动到暂存文件 production.ymlreference.yml 中的等效定义中.

例如,如果 group_vars/all.yml 定义了一些用户:

用户:- 爱丽丝- 鲍勃- 马洛里

我想在两种环境中创建用户,但我想从生产环境中排除 mallory,我可以定义一个名为 effective_users 的新组.在 reference.yml 中,它与 users 列表相同:

 Effective_users: >-{{ 用户 }}

但是在production.yml中我可以排除mallory:

 Effective_users: >-{{ 用户 |差异(['马洛里'])}}

剧本或角色不需要区分这两个阶段,他们可以简单地使用组effective_users.只需选择清单,该组就会自动包含正确的用户列表.

The Ansible best practices documentation recommends to separate inventories:

inventories/
   production/
      hosts.ini           # inventory file for production servers
      group_vars/
         group1           # here we assign variables to particular groups
         group2           # ""
      host_vars/
         hostname1        # if systems need specific variables, put them here
         hostname2        # ""

   staging/
      hosts.ini           # inventory file for staging environment
      group_vars/
         group1           # here we assign variables to particular groups
         group2           # ""
      host_vars/
         stagehost1       # if systems need specific variables, put them here
         stagehost2       # ""

My staging and production environments are structured in the same way. I have in both environments the same groups. And it turns out that I have also the same group_vars for the same groups. This means redundancy I would like to wipe out.

Is there a way to share some group_vars between different inventories?

As a work-around I started to put shared group_vars into the roles.

my_var:
  my_group:
    - { var1: 1, var2: 2 }

This makes it possible to iterate over some vars by intersecting the groups of a host with the defined var:

with_items: "{{group_names | intersect(my_var.keys())}}"

But this is a bit complicate to understand and I think roles should not know anything about groups.

I would like to separate most of the inventories but share some of the group_vars in an easy to understand way. Is it possible to merge global group_vars with inventory specific group_vars?

解决方案

I scrapped the idea of following Ansible's recommendation. Now one year later, I am convinced that Ansible's recommendation is not useful for my requirements. Instead I think it is important to share as much as possible among different stages.

Now I put all inventories in the same directory:

production.ini
reference.ini

And I take care that each inventory defines a group including all hosts with the name of the stage.

The file production.ini has the group production:

[production:children]
all_production_hosts

And the file reference.ini has the group reference:

[reference:children]
all_reference_hosts

I have just one group_vars directory in which I define a file for every staging group:

group_vars/production.yml
group_vars/reference.yml

And each file defines a stage variable. The file production.yml defines this:

---
stage: production

And the file reference.yml defines that:

---
stage: reference

This makes it possible to share everything else between production and reference. But the hosts are completely different. By using the right inventory the playbook runs either on production or on reference hosts:

ansible-playbook -i production.ini site.yml
ansible-playbook -i reference.ini site.yml

If it is necessary for the site.yml or the roles to behave slightly different in the production and reference environment, they can use conditions using the stage variable. But I try to avoid even that. Because it is better to move all differences into equivalent definitions in the staging files production.yml and reference.yml.

For example, if the group_vars/all.yml defines some users:

users:
  - alice
  - bob
  - mallory

And I want to create the users in both environments, but I want to exclude mallory from the production environment, I can define a new group called effective_users. In the reference.yml it is identical to the users list:

effective_users: >-
  {{ users }}

But in the production.yml I can exclude mallory:

effective_users: >-
  {{ users | difference(['mallory']) }}

The playbook or the roles do not need to distinguish between the two stages, they can simply use the group effective_users. The group contains automatically the right list of users simply by selecting the inventory.

这篇关于如何在 Ansible 的不同库存之间共享 group_vars?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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