Dynamic groups based on ip range in Ansible groups

蹲街弑〆低调 提交于 2019-12-12 05:06:40

问题


How to create variable group based on ip address-range in ansible inventory groups ? I have two groups of servers in different location. I want to create the groups every time the playbook so that the playbook is run on a updated list of servers. I already groups based on distribution, prod, dev, test, qa ,dr in my host inventory.

Thanks in Advance


回答1:


You can scan all hosts at the beginning and group them how you want, e.g. by network:

- hosts: all
  tasks:
    - group_by: key=network_{{ ansible_default_ipv4.network }}

This will create as many groups with names network_<network> as many different networks you have. But keep in mind that it need to gather facts from all hosts, so depending on count of hosts and speed of connections it can require significant time. Facts caching will speed up things, but anyway...

If you manage your inventory file by hand, consider manually specifying regional groups as well.



来源:https://stackoverflow.com/questions/38571141/dynamic-groups-based-on-ip-range-in-ansible-groups

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