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

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

问题描述

Ansible最佳做法文档建议来分隔库存:

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       # ""

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

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.

是否可以在不同的清单之间共享一些group_vars?

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

作为一种变通办法,我开始将共享的group_vars放入角色.

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

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

这使得可以通过将主机的组与定义的var相交来遍历某些var:

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.

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

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?

推荐答案

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

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.

文件production.ini具有组production:

[production:children]
all_production_hosts

文件reference.ini具有组reference:

[reference:children]
all_reference_hosts

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

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

每个文件都定义一个stage变量.文件production.yml定义了以下内容:

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

---
stage: production

文件reference.yml定义了以下内容:

---
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

如果site.yml或角色在生产环境和参考环境中的行为必须有所不同,则可以使用stage变量使用条件.但是我尽力避免这种情况.因为最好将所有差异移到登台文件production.ymlreference.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.

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

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

users:
  - alice
  - bob
  - mallory

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

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 }}

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

But in the production.yml I can exclude mallory:

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

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

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天全站免登陆