Lefty G Balogh
Lefty G Balogh

Reputation: 1888

How to colonize Ansible inventories in yaml format

I got this task to convert Ansible inventories from ini to yml, so I take the working ini file and make the usual rounds: - Github: check - solution hangs - Online tool: check - solution does not resemble anything from this planet - Last resort: I read the official guide that brings the following example:

all: # keys must be unique, i.e. only one 'hosts' per group
   hosts:
       test1:
       test2:
           var1: value1
   vars:
       group_var1: value2
   children:   # key order does not matter, indentation does
       other_group:
           children:
               group_x:
                   hosts:
                       test5

Cool, I quickly turn my original ini to this beauty

ORIGINAL

[thingiebob_master]
thng-esxi96

[thingiebob_slaves]
thng-esxi97

CONVERTED v1

all:
   children:
        thingiebob_master:
            hosts: thng-esxi96
        thingiebob_slaves:
            hosts:
                thng-esxi97

I can run a ping and it reaches both machines. But when I add another node to the second group: CONVERTED v2

all:
   children:
        thingiebob_master:
            hosts: thng-esxi96
        thingiebob_slaves:
            hosts:
                thng-esxi97
                thng-esxi98

I get an error:

PLAY [all] **************************************************************************************

TASK [Gathering Facts] **************************************************************************
fatal: [thng-esxi97 thng-esxi98]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: 
Could not resolve hostname thng-esxi97 thng-esxi98: Name or service not known\r\n", "unreachable": true}

But if I put a colon after them, effectively turning them into keys, as in v3 below, all three nodes are found and pinged. CONVERTED v3

    all:
       children:
            thingiebob_master:
                hosts: thng-esxi96
            thingiebob_slaves:
                hosts:
                    thng-esxi97:
                    thng-esxi98:

Result

PLAY [all] **************************************************************************************

TASK [Gathering Facts] **************************************************************************
ok: [thng-esxi98]
ok: [thng-esxi96]
ok: [thng-esxi97]

TASK [ping] *************************************************************************************
ok: [thng-esxi97]
ok: [thng-esxi96]
ok: [thng-esxi98]

PLAY RECAP **************************************************************************************
thng-esxi96               : ok=2    changed=0    unreachable=0    failed=0
thng-esxi97               : ok=2    changed=0    unreachable=0    failed=0
thng-esxi98               : ok=2    changed=0    unreachable=0    failed=0

Question: (How) Can I add multiple nodes to my thingiebob_slaves group? (Without colonizing them.)

Upvotes: 2

Views: 1794

Answers (1)

clockworknet
clockworknet

Reputation: 3056

Entertaining question :)

Got this completely wrong on the first pass :( I presumed that a YAML version of the basic INI file mirrored the layout of the data structure used when writing a dynamic inventory script and therefore groups contained lists of hosts. It doesn't and they don't.

I happened to be reading the Inventory docs today. This clearly shows that in a YAML version of the basic INI inventory, that a group is indeed a dictionary, containing keys of hostnames and values of either an empty dictionary, or a dictionary of host specific options.

Example from the docs:

all:
  hosts:
    mail.example.com:
        ansible_port: 5555
        ansible_host: 192.0.2.50
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
    dbservers:
      hosts:
        one.example.com:
        two.example.com:
        three.example.com:

Long story short, you are stuck with the colons.

Upvotes: 1

Related Questions