2

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
jlim
  • 909
  • 2
  • 12
  • 24

1 Answers1

0

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.

freginold
  • 3,946
  • 3
  • 13
  • 28
  • I did try the approach you have mentioned and ansible doesn't seems to understand even we omitted the `children` tag in the group. – jlim Sep 14 '17 at 21:02
  • 1
    Hmm... do you know why it's saying it's trying to read your `hosts` file as YAML when it's INI syntax? – freginold Sep 15 '17 at 00:00
  • 1
    The moment I have the square bracket in the group, this error happens. I then change the `hosts` to `hosts.ini` and try one more time and I got this error `29286d512845:~$ ansible -i hosts.ini all --list-hosts ERROR! Attempted to read "hosts.ini" as ini file: host range must be begin:end or begin:end:step` – jlim Sep 18 '17 at 13:45
  • @jlim [This answer](https://stackoverflow.com/a/41095437/5463636) has a good guide for how to format hosts in a YAML file. I don't know why your syntax isn't working as an INI file, though. – freginold Sep 18 '17 at 14:18
  • 1
    Thanks for your quick response. If I understand the ansible `inventory` correctly, the `pattern` usage can be applied to host machines like `192.168.2.[100-110]`, but it cannot use on a group. From a `playbook` perspective, you can apply group patterns like `hosts: clusterA[1]`. Nevertheless, I have given up on this approach of how I want to layout my `inventory` file in the way I wanted in my earlier post. – jlim Sep 18 '17 at 16:05
  • 1
    I have gone ahead to test once more with YAML from the link you have provided. It does show the same error `host range must be begin:end or begin:end:step`. I am concluding that you cannot assign pattern on group in a children or another group itself. You can perform pattern on host instead – jlim Sep 20 '17 at 16:40