如何使用 ansible inventory 插件解决网络问题 vmware_vm_inventory?

How to address the network with ansible inventory plugin vmware_vm_inventory?

我使用 ansible inventory 插件 vmware_vm_inventory(参见 https://docs.ansible.com/ansible/latest/scenario_guides/vmware_scenarios/vmware_inventory_hostnames.html)。我也想为第一个连接的网络分组。那么它的正确属性名称是什么?

我的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'

如果我 运行 命令:

ansible-inventory --host TESTVM001

我得到:

{
    "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"
}

我应该如何将文件 vm.vmware.yml 中的行 - 'guest.net' 更改为仅获取网络名称。

我尝试了什么:

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

结果就是 ansible-inventory 的帮助输出。

我找到了。提示来自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: "-"

compose重点了解jinja2。 https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_vm_inventory_inventory.html

中描述

所以我用 ansible-inventory --graph 得到的是:

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