0
votes

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

1 Answers

0
votes

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.