根据我在the documentation of the module 中看到的内容,它会产生如下所示的结果:
[
{
"attributes":{
"job":"backup-prepare"
},
"cluster":"None",
"datacenter":"Datacenter-1",
"esxi_hostname":"10.76.33.226",
"folder":"/Datacenter-1/vm",
"guest_fullname":"Ubuntu Linux (64-bit)",
"guest_name":"ubuntu_t",
"ip_address":"",
"mac_address":[
"00:50:56:87:a5:9a"
],
"moid":"vm-24",
"power_state":"poweredOff",
"tags":[
{
"category_id":"urn:vmomi:InventoryServiceCategory:b316cc45-f1a9-4277-811d-56c7e7975203:GLOBAL",
"category_name":"cat_0001",
"description":"",
"id":"urn:vmomi:InventoryServiceTag:43737ec0-b832-4abf-abb1-fd2448ce3b26:GLOBAL",
"name":"tag_0001"
}
],
"uuid":"4207072c-edd8-3bd5-64dc-903fd3a0db04",
"vm_network":{
"00:50:56:87:a5:9a":{
"ipv4":[
"10.76.33.228"
],
"ipv6":[
]
}
}
}
]
*来源:https://docs.ansible.com/ansible/2.9/modules/vmware_vm_info_module.html#return-values*
由于我在这里看不到任何vm_name,我将假设您的意思是guest_name,但本剧本背后的想法将适用于您认为合适的任何属性。
您可以使用 Jinja 过滤器map 将字典列表的值提取为简单列表。
下面是一个基于此模块记录的数据的示例剧本,我在其中添加了第二个假 VM,以展示列表中包含多个项目的结果:
- hosts: localhost
gather_facts: no
tasks:
- debug:
msg: "{{ virtual_machines | map(attribute='guest_name') | list }}"
vars:
virtual_machines: [
{
"attributes":{
"job":"backup-prepare"
},
"cluster":"None",
"datacenter":"Datacenter-1",
"esxi_hostname":"10.76.33.226",
"folder":"/Datacenter-1/vm",
"guest_fullname":"Ubuntu Linux (64-bit)",
"guest_name":"ubuntu_t",
"ip_address":"",
"mac_address":[
"00:50:56:87:a5:9a"
],
"moid":"vm-24",
"power_state":"poweredOff",
"tags":[
{
"category_id":"urn:vmomi:InventoryServiceCategory:b316cc45-f1a9-4277-811d-56c7e7975203:GLOBAL",
"category_name":"cat_0001",
"description":"",
"id":"urn:vmomi:InventoryServiceTag:43737ec0-b832-4abf-abb1-fd2448ce3b26:GLOBAL",
"name":"tag_0001"
}
],
"uuid":"4207072c-edd8-3bd5-64dc-903fd3a0db04",
"vm_network":{
"00:50:56:87:a5:9a":{
"ipv4":[
"10.76.33.228"
],
"ipv6":[
]
}
}
},
{
"attributes":{
"job":"backup-prepare"
},
"cluster":"None",
"datacenter":"Datacenter-1",
"esxi_hostname":"10.76.33.226",
"folder":"/Datacenter-1/vm",
"guest_fullname":"Ubuntu Linux (64-bit)",
"guest_name":"ubuntu_t_2",
"ip_address":"",
"mac_address":[
"00:50:56:87:a5:9a"
],
"moid":"vm-24",
"power_state":"poweredOff",
"tags":[
{
"category_id":"urn:vmomi:InventoryServiceCategory:b316cc45-f1a9-4277-811d-56c7e7975203:GLOBAL",
"category_name":"cat_0001",
"description":"",
"id":"urn:vmomi:InventoryServiceTag:43737ec0-b832-4abf-abb1-fd2448ce3b26:GLOBAL",
"name":"tag_0001"
}
],
"uuid":"4207072c-edd8-3bd5-64dc-903fd3a0db04",
"vm_network":{
"00:50:56:87:a5:9a":{
"ipv4":[
"10.76.33.228"
],
"ipv6":[
]
}
}
}
]
这里是它的回顾:
PLAY [localhost] **************************************************************************************************
TASK [debug] ******************************************************************************************************
ok: [localhost] => {
"msg": [
"ubuntu_t",
"ubuntu_t_2"
]
}
PLAY RECAP ********************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0