在 Ansible 中为组放置公共变量的位置 [英] Where to put common variables for groups in Ansible

查看:21
本文介绍了在 Ansible 中为组放置公共变量的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些脚本可以帮助我们在 AWS 中设置具有多达 6 个虚拟机的 VPC.现在我想登录到这些机器中的每一个.出于安全原因,我们只能通过 SSH 访问其中一个,然后通过隧道/代理连接到其他机器.因此,在我们的清单中,我们有 SSH 主机的 IP 地址(我们称之为 Redcarpet)和一些其他主机,如 ElasticsearchMongodb工人:

We have some scripts to help us set up VPCs with up to 6 VMs in AWS. Now I want to log in to each of these machines. For security reasons we can only access one of them via SSH and then tunnel/proxy through that to the other machines. So in our inventory we have the IP address of the SSH host (we call it Redcarpet) and some other hosts like Elasticsearch, Mongodb and Worker:

#inventory/hosts

[redcarpet]
57.44.113.25

[services]
10.0.1.2

[worker]
10.0.5.77
10.0.1.200

[elasticsearch]
10.0.5.30

[mongodb]
10.0.1.5

现在我需要告诉每个组,除了 redcarpet 使用某些 SSH 设置.如果这些适用于所有组,我会将它们放在 inventory/group_vars/all.yml 中,但现在我必须将它们放入:

Now I need to tell each of the groups, EXCEPT redcarpet to use certain SSH settings. If these were applicable to all groups, I would put them in inventory/group_vars/all.yml, but now I will have to put them in:

  • inventory/group_vars/services.yml
  • inventory/group_vars/worker.yml
  • inventory/group_vars/elasticsearch.yml
  • inventory/group_vars/mongodb.yml

这会导致重复.因此,我想使用 includeinclude_vars 来包含来自公共文件(例如 inventory/common.yml)的一两个变量.但是,当我尝试在上面的任何 group_var 文件中执行此操作时,它不会选择变量.对多个组共有的变量使用的最佳做法是什么?

Which leads to duplication. Therefore I would like to use an include or include_vars to include one or two variables from a common file (e.g. inventory/common.yml). However, when I try to do this in any of the group_var files above, it does not pick up the variables. What is the best practice to use with variables that are common to multiple groups?

推荐答案

如果您想使用 group_vars 方法,我建议您添加另一个组,并将依赖组作为子项添加到该组中.

If you want to go with the group_vars approach, I would suggest you add another group, and add the dependent groups as children to that group.

#inventory/hosts

[redcarpet]
57.44.113.25

[services]
10.0.1.2

[worker]
10.0.5.77
10.0.1.200

[elasticsearch]
10.0.5.30

[mongodb]
10.0.1.5

[redcarpet_deps:children]
mongodb
elasticsearch
worker
services

现在你可以有一个名为 redcarpet_deps.yml 的 group_vars 文件,他们应该从那里提取 vars.

And now you can have a group_vars file called redcarpet_deps.yml and they should pickup the vars from there.

这篇关于在 Ansible 中为组放置公共变量的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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