在Ansible中为组添加公共变量的位置(Where to put common variables for groups in Ansible)

编程入门 行业动态 更新时间:2024-10-25 08:20:04
在Ansible中为组添加公共变量的位置(Where to put common variables for groups in Ansible)

我们有一些脚本可以帮助我们在AWS中设置最多6个虚拟机的VPC。 现在我想登录这些机器。 出于安全原因,我们只能通过SSH访问其中一个,然后通过隧道/代理访问其他计算机。 所以在我们的库存中,我们有SSH主机的IP地址(我们称之为Redcarpet )和其他一些主机,如ElasticsearchMongodbWorker

#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 ,但现在我必须将它们放入:

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

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

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

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

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方法,我建议您添加另一个组,并将依赖组作为子组添加到该组。

#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文件,他们应该从那里获取变量。

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

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

更多推荐

本文发布于:2023-07-15 19:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117560.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   中为   位置   Ansible   groups

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!