Search code examples
ansibleansible-inventory

yml format for inventory file...fails


---
all: 
zones:
  - name: accessswitch
    hosts:
     - name: accessswitch-x0
       ip: 192.168.4.xx

  - name: groupswitch
    hosts:
    - name: groupswitch-x1
      ip: 192.168.4.xx
    - name: groupswitch-x2
      ip: 192.168.4.xx
    - name: groupswitch-x3
      ip: 192.168.4.xx

Basically i have a access switch and to that switch there are many group switches connected to it...Tried already "children" but this does not work. A typical ini file works.... Also i have some variables...which do apply to all zones aka. access witch & group switches... in the future there will be more than 1 ..multiple access switches.. .... some docs use ansible-host:...confusing..

And yes..checked the uml structure...

cat@catwomen:~/workspace/ansible-simulator/inventories/simulator/host_vars$ sudo ansible -i testdata.yaml  all -m  ping 
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
'all'

cat@catwomen:~/workspace/ansible-simulator/inventories/simulator/host_vars$ sudo ansible -i testdata.yaml all -m  ping 
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
'all'

Solution

  • You have yaml indentation problems which is an error (indentation in yaml is significant). Moreover, you must follow the specific format described in the documentation.

    Very basically, the file is an imbrication of groups in parent/child relationship starting with the top element special group all. A group definition looks like:

    ---
    my_group1: # group name, use `all` at top level
      vars:
        # definition of vars for this group. Apply to all hosts if defined for `all`
      hosts:
        # hosts in that group (host is ungrouped if it appears only in `all`)
      children:
        # mapping of children group definitions (repeat the current format)
    

    A host definition looks like:

    my_host1:
      # definition of vars specific to that host if any
    

    A var definition (either for vars in group or for a specific host) looks like:

    my_variable1: some value
    

    If I understand correctly from your example, here is how your yaml inventory should look like (inventories/so_example.yml)

    ---
    all:
    
      children:
    
        accessswitch:
          hosts:
            accessswitch-x0:
              ansible_host: 192.168.4.xx
    
        groupswitch:
          hosts:
            groupswitch-x1:
              ansible_host: 192.168.4.xx
            groupswitch-x2:
              ansible_host: 192.168.4.xx
            groupswitch-x3:
              ansible_host: 192.168.4.xx
    

    You can then easilly see how the above is interpreted with the ansible-inventory command:

    $ ansible-inventory -i inventories/so_example.yml --graph 
    @all:
      |--@accessswitch:
      |  |--accessswitch-x0
      |--@groupswitch:
      |  |--groupswitch-x1
      |  |--groupswitch-x2
      |  |--groupswitch-x3
      |--@ungrouped:
    
    $ ansible-inventory -i inventories/so_example.yml --list
    {
        "_meta": {
            "hostvars": {
                "accessswitch-x0": {
                    "ansible_host": "192.168.4.xx"
                },
                "groupswitch-x1": {
                    "ansible_host": "192.168.4.xx"
                },
                "groupswitch-x2": {
                    "ansible_host": "192.168.4.xx"
                },
                "groupswitch-x3": {
                    "ansible_host": "192.168.4.xx"
                }
            }
        },
        "accessswitch": {
            "hosts": [
                "accessswitch-x0"
            ]
        },
        "all": {
            "children": [
                "accessswitch",
                "groupswitch",
                "ungrouped"
            ]
        },
        "groupswitch": {
            "hosts": [
                "groupswitch-x1",
                "groupswitch-x2",
                "groupswitch-x3"
            ]
        }
    }