Search code examples
ansibleyamlvmwareansible-inventory

How to address the network with ansible inventory plugin vmware_vm_inventory?


I use the ansible inventory plugin vmware_vm_inventory (see https://docs.ansible.com/ansible/latest/scenario_guides/vmware_scenarios/vmware_inventory_hostnames.html). i want to group for the first connected network too. So what is the right Propertyname for it?

my vm.vmware.yml:

plugin: vmware_vm_inventory
strict: False
hostname: 10.10.10.1
username: user
password: password
validate_certs: False
with_tags: False
hostnames:
  - config.name
properties:
- 'config.name'
- 'config.guestId'
- 'guest.ipAddress'
- 'guest.net'
- 'summary.runtime.powerState'

if I run the command:

ansible-inventory --host TESTVM001

i get:

{
    "ansible_host": "10.20.20.20",
    "config": {
        "guestId": "ubuntu64Guest",
        "name": "TESTVM001"
    },
    "config.guestId": "ubuntu64Guest",
    "config.name": "TESTVM001",
    "guest": {
        "ipAddress": "10.20.20.20",
        "net": [
            {
                "connected": true,
                "deviceConfigId": 4000,
                "dnsConfig": null,
                "ipAddress": [
                    "10.20.20.20",
                    "fe80::333:33ff:aaaa:903f"
                ],
                "ipConfig": {
                    "autoConfigurationEnabled": null,
                    "dhcp": null,
                    "ipAddress": [
                        {
                            "ipAddress": "10.20.20.20",
                            "lifetime": null,
                            "origin": null,
                            "prefixLength": 16,
                            "state": "preferred"
                        },
                        {
                            "ipAddress": "fe80::333:33ff:aaaa:903f",
                            "lifetime": null,
                            "origin": null,
                            "prefixLength": 64,
                            "state": "unknown"
                        }
                    ]
                },
                "macAddress": "00:50:00:ab:cd:ef",
                "netBIOSConfig": null,
                "network": "MYNETWORK"
            }
        ]
    },
    "guest.ipAddress": "10.20.20.20",
    "guest.net": [
        {
            "connected": true,
            "deviceConfigId": 4000,
            "dnsConfig": null,
            "ipAddress": [
                "10.20.20.20",
                "fe80::333:33ff:aaaa:903f"
            ],
            "ipConfig": {
                "autoConfigurationEnabled": null,
                "dhcp": null,
                "ipAddress": [
                    {
                        "ipAddress": "10.20.20.20",
                        "lifetime": null,
                        "origin": null,
                        "prefixLength": 16,
                        "state": "preferred"
                    },
                    {
                        "ipAddress": "fe80::333:33ff:aaaa:903f",
                        "lifetime": null,
                        "origin": null,
                        "prefixLength": 64,
                        "state": "unknown"
                    }
                ]
            },
            "macAddress": "00:50:00:ab:cd:ef",
            "netBIOSConfig": null,
            "network": "MYNETWORK"
        }
    ],
    "runtime": {
        "connectionState": "connected"
    },
    "runtime.connectionState": "connected",
    "summary": {
        "runtime": {
            "powerState": "poweredOn"
        }
    },
    "summary.runtime.powerState": "poweredOn"
}

How should i change the line - 'guest.net' in the file vm.vmware.yml to only get the network name.

What i tried:

- 'guest.net.0.network' - 'guest.net[0].network'

the result was then the help output for ansible-inventory.


Solution

  • I have found it. The hint came from https://github.com/ansible-collections/community.vmware/issues/902

    plugin: vmware_vm_inventory
    strict: False
    hostname: 10.10.10.1
    username: user
    password: password
    validate_certs: False
    with_tags: False
    hostnames:
      - config.name
    properties:
    - 'config.name'
    - 'config.guestId'
    - 'guest.ipAddress'
    - 'guest.net'
    - 'summary.runtime.powerState'
    compose:
      ansible_host: 'guest.ipAddress'
      ansible_networkname: 'guest.net[0].network'
    keyed_groups:
      - key: ansible_networkname
        prefix: "net0"
        seperator: "-"
    

    the compose key understand jinja2. described in https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_vm_inventory_inventory.html

    So what i get with ansible-inventory --graph is:

    @all:
      |--@ubuntu64Guest:
      |  |--vm1
      |  |--vm2
      |--@net0_NET01:
      |  |--vm1
      |--@net0_NET02:
      |  |--vm2