Ansible inventory group array pointer to parent

2.1k Views Asked by At

I want to check if anyone knows if there is a solution to this problem I am facing in Ansible.

I have a inventory file that looks like this:-

[clusterA]
10.0.0.1
10.0.0.2
10.0.0.3
....

[clusterB]
10.1.0.1
10.1.0.2
10.1.0.3
....

[web-lb]
10.0.0.1
10.1.0.1

Instead of repeating the IP address in web-lb group, I want to do something like this:-

[web-lb:children]
clusterA[0]
clusterB[0]

If we can script the group as mentioned above, I don't need to duplicate IP addresses, and i can mix different item from a group into another group, Eg

[webA-lb:children]
clusterA[1]
clusterA[5]
clusterB[3]

UPDATED

Having the below configuration doesn't work as well

[webA-lb]
clusterA[1]

Error:

bbed5901ea74:~$ ansible -i hosts all --list-hosts
hosts (6):
10.1.0.1
10.1.0.2
10.1.0.3
10.0.0.1
10.0.0.2
10.0.0.3
bbed5901ea74:~$ vi hosts
bbed5901ea74:~$ ansible -i hosts all --list-hosts
ERROR! Attempted to read "hosts" as YAML: Syntax Error while loading YAML.


The error appears to have been in '/home/jenkins/hosts': line 2, column 1, 
but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

[clusterA]
10.0.0.1
^ here

Attempted to read "hosts" as ini file: host range must be begin:end or 
begin:end:step
1

There are 1 best solutions below

6
On

I don't think you can use :children combined with the individual selector [] like that. The :children suffix denotes a group of groups.

So you could do this:

[web-lb]
clusterA[0]
clusterB[0]

But not this:

[web-lb:children]
clusterA[0]
clusterB[0]

See Ansible's Patterns and Inventory documentation for more details. Ansible is very flexible. You can also use the ! symbol to exclude certain children from a group.