ansible - define hosts/tag from dynamic inventory in the host file

五迷三道 提交于 2019-12-12 04:36:26

问题


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:

The results from ec2.py:

 "tag_test_staging": [
    "10_80_20_47"
  ],

I define the tag in my playbook - hosts: tag_Name_test and it is run on all the instances with tag_Name_test.

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.


回答1:


You already go to the right direction.

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

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.



来源:https://stackoverflow.com/questions/42643684/ansible-define-hosts-tag-from-dynamic-inventory-in-the-host-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!