Search code examples
ansibleansible-inventory

Inventory with both a hostname and IP address pattern


I'm using the vmware_guest module to create a bunch of vms.

Is there anyway to use a pattern for both the hostname and IP address?

For example I want to create 5 master vms with hostname/IP like:

  • master1 10.123.0.1
  • master2 10.123.0.2
  • master3 10.123.0.3
  • etc.

So an inventory yaml with something like the following: all: children: Elvis: children: masterNodes: hosts: master[1-5]: ansible_host: 10.123.0.[1-5] slaveNodes: hosts: slave[1-10]: ansible_host: 10.124.0.[1-10]


Also, is there a way to run a playbook for a specific parent:child group? How can I run a play for Elvis:masterNodes only? If for instance, the same inventory yaml has another parent child group Bono:masterNodes


Solution

  • Is there anyway to use a pattern for both the hostname and IP address?

    No. Instead, Ansible allows running a script to generate an inventory - it is trivial to implement a loop populating inventory-hostname along with IP address in Python (apparently it was made clear as early as in July 2013, although in the same thread someone suggested a workaround).

    Also, is there a way to run a playbook for a specific parent:child group?

    No. There is no hierarchy in Ansible inventory; the namespace is flat; there are no child groups; Ansible operates on host/group sets.

    In effect, you can't have distinct (sub)groups bearing the same name. The contents of those (sub)groups will be merged. So if Elvis contains a group masterNodes and you execute a play with hosts:Elvis declaration, it will run on all hosts defined for masterNodes anywhere (including in Bono:masterNodes).