ansible - 从主机文件中的动态清单定义主机/标签 [英] ansible - define hosts/tag from dynamic inventory in the host file
问题描述
我在 ec2 实例上使用 ec2.py
和特定标签来获取我的主机,结果显示为 IP 地址列表,例如:
I am using ec2.py
and specific tag on ec2 instances to get my hosts, the results are shown as list of IP addresses, for example:
ec2.py 的结果:
The results from ec2.py:
"tag_test_staging": [
"10_80_20_47"
],
我在我的剧本 - hosts: tag_Name_test
中定义了标签,并且它在所有带有 tag_Name_test
的实例上运行.
I define the tag in my playbook - hosts: tag_Name_test
and it is run on all the instances with tag_Name_test
.
有没有办法在 inventory/
文件夹下的 hosts
文件中定义主机/标签,剧本将从那里获取主机而不是指定 ec2像现在一样直接在剧本上标记?
Is there a way to define the hosts/tag in the hosts
file under the inventory/
folder and the playbook will take the hosts from there instead of specify the ec2 tag directly on the playbook like now ?
如有任何建议,我们将不胜感激.
Any suggestions would be appreciated.
推荐答案
您已经走对了方向.
假设您通过 ec2.py
获得动态库存,并且它是 tag_test_staging
.因此,您可以按如下方式构建库存文件夹和文件
Suppose you got dynamic inventory by ec2.py
and it is tag_test_staging
. So you can build inventory folder and files as below
inventory
staging
hosts
group_vars
all.yml
tag_test_staging.yml
tag_Name_test.yml
您在每个 YAML 文件中添加变量定义.tag_test_staging.yml
中的变量将仅应用于具有该标签的实例.
You add the variable define in each YAML file. the variable in tag_test_staging.yml
will be only applied to the instance with that tag.
所以现在您可以将您的剧本应用为:
So now you can apply your playbook as:
ansible-playbook -i inventory/staging your_playbook.yml
有一个关于如何的最佳实践文档使用带有云的动态库存,也请看一下.
There is a best practices document on how to use dynamic inventory with clouds, please take a look as well.
这篇关于ansible - 从主机文件中的动态清单定义主机/标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!