Where to put common variables for groups in Ansibl

2019-07-27 12:02发布

问题:

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?

回答1:

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.