#ansible #yaml #vmware #ansible-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"
}
Как мне изменить строку - 'guest.net'
в файле vm.vmware.yml
, чтобы получить только сетевое имя.
Что я пытался:
- 'guest.net.0.network'
- 'guest.net[0].network'
результатом стал вывод справки для ansible-инвентаризации.
Ответ №1:
Я нашел его. Намек исходил от 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
ключ к пониманию джинджа2. описано в 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