Gana Sagar
Gana Sagar

Reputation: 131

ansible dynamic group based on IP range

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 two groups based on their IP range (two ranges for two locations) . I already have groups based on distribution, prod, dev, test, qa ,dr in my host inventory. I know I can create a thr grouping using group_by. But How do I call them ? and How to assign a custom variable name to them ? - hosts: all tasks: - group_by: key=network_{{ ansible_default_ipv4.network }}

But how do I call them ? and How to assign a custom variable name to them ? Is there a way that I could create a group once and store all the hosts in a static inventory file. ?

Upvotes: 3

Views: 1379

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68289

I've answered in the comment.

You can use when statement with group_by as with any other task.
For example:

---
- hosts: all
  tasks:
   - group_by: key=London
     when: ansible_default_ipv4.network == '192.168.0.0'
   - group_by: key=Paris
     when: ansible_default_ipv4.network == '10.0.0.0'

Upvotes: 3

Related Questions