我尝试从 API 调用返回的复杂负载中提取多个属性,同时拒绝“名称”字段中具有特定值的成员。考虑以下负载:
{
"npod_volumes": {
"changed": false,
"failed": false,
"volumes": [
{
"accessible_by_host_uuids": [
"9e8ba77c-8e09-4c69-b893-876742f83f34",
"ad5a5431-17e1-4023-8373-a6cf64cc5151",
"3333a278-414b-45d1-8972-611115c76f87",
"1e36d01a-f936-4249-9045-28232bb103e7"
],
"boot": false,
"creation_time": "2022-01-26T18:25:46",
"current_owner_host_uuid": "ad5a5431-17e1-4023-8373-a6cf64cc5151",
"expiration_time": null,
"lun_uuids": [
"8cfe80b6-5773-41bd-b175-e6e495ca0023",
"8e54f045-89dd-462a-bab4-552ed745ed24",
"136d7028-2b20-41c7-8b30-e104d211242c",
"4e7aac12-3b29-41c7-b016-36556feea9f1"
],
"name": "VMware_Lenovo_VV7",
"natural_backup_host_uuid": "9e8ba77c-8e09-4c69-b893-876742f83f34",
"natural_backup_spu_serial": "xxxxxxxx",
"natural_owner_host_uuid": "ad5a5431-17e1-4023-8373-a6cf64cc5151",
"natural_owner_spu_serial": "xxxxxxxx",
"npod_uuid": "4b60437a-f8dc-4c80-b4b2-51b64b790487",
"read_only_snapshot": false,
"size_bytes": 4000000000000,
"snapshot_parent_uuid": null,
"snapshot_uuids": null,
"sync_state": "InSync",
"uuid": "ed73e3a0-25e2-4672-8048-84ad4468c0d7",
"wwn": "6f497c2006174fed000ab00000007000"
},
{
"accessible_by_host_uuids": [
"9e8ba77c-8e09-4c69-b893-876742f83f34",
"1e36d01a-f936-4249-9045-28232bb103e7",
"ad5a5431-17e1-4023-8373-a6cf64cc5151",
"3333a278-414b-45d1-8972-611115c76f87"
],
"boot": false,
"creation_time": "2022-01-26T18:25:40",
"current_owner_host_uuid": "9e8ba77c-8e09-4c69-b893-876742f83f34",
"expiration_time": null,
"lun_uuids": [
"49e511b7-2b5f-4727-ac63-de12c4b6eb45",
"2baba8a4-dd23-4be4-ad9d-47580efa60d2",
"4ff844d6-c677-4c2a-a2e5-c13a5c062b3c",
"21e7f0c8-5f1a-438f-b503-e568f3691c01"
],
"name": "VMware_Lenovo_VV4",
"natural_backup_host_uuid": "ad5a5431-17e1-4023-8373-a6cf64cc5151",
"natural_backup_spu_serial": "xxxxxxxx",
"natural_owner_host_uuid": "9e8ba77c-8e09-4c69-b893-876742f83f34",
"natural_owner_spu_serial": "xxxxxxxx",
"npod_uuid": "4b60437a-f8dc-4c80-b4b2-51b64b790487",
"read_only_snapshot": false,
"size_bytes": 4000000000000,
"snapshot_parent_uuid": null,
"snapshot_uuids": null,
"sync_state": "InSync",
"uuid": "cc3991e2-5443-4bd0-a75d-3e8341d26282",
"wwn": "6f497c2006174fed000ab00000004000"
},
{
"accessible_by_host_uuids": [
"ad5a5431-17e1-4023-8373-a6cf64cc5151"
],
"boot": true,
"creation_time": "2022-01-26T18:25:46",
"current_owner_host_uuid": "ad5a5431-17e1-4023-8373-a6cf64cc5151",
"expiration_time": null,
"lun_uuids": [
"d5ce25a9-15d1-4e36-878e-5e645ef0c557"
],
"name": "VMware_Lenovo_server-09.tme.nebulon.com_os",
"natural_backup_host_uuid": "9e8ba77c-8e09-4c69-b893-876742f83f34",
"natural_backup_spu_serial": "xxxxxxxx",
"natural_owner_host_uuid": "ad5a5431-17e1-4023-8373-a6cf64cc5151",
"natural_owner_spu_serial": "xxxxxxxx",
"npod_uuid": "4b60437a-f8dc-4c80-b4b2-51b64b790487",
"read_only_snapshot": false,
"size_bytes": 20000000000,
"snapshot_parent_uuid": null,
"snapshot_uuids": null,
"sync_state": "InSync",
"uuid": "bdbce49d-834d-438e-a56d-9c384bc229c0",
"wwn": "6f497c2006174fed000ab00000000000"
}
]
}
}
从此返回中,我需要存储每个卷的“名称”和“wwn”字段。此外,我需要拒绝“名称”字段中包含“_os”的任何卷。
我可以使用 json_query 获取信息,但还没有弄清楚如何使用这种方法拒绝名称中带有“_os”的卷。
- name: Set volume name and WWN
set_fact:
volume_pairs: "{{ npod_volumes | json_query(volume_query) }}"
vars:
volume_query: "volumes[].{name: name, wwn: wwn}"
tags: volumes
这将产生以下输出,其中包括不需要的“VMware_Lenovo_server-09.tme.nebulon.com_os”卷。
ok: [127.0.0.1] => {
"volume_pairs": [
{
"name": "VMware_Lenovo_VV7",
"wwn": "6f497c2006187474000ab00000007000"
},
{
"name": "VMware_Lenovo_VV4",
"wwn": "6f497c2006187474000ab00000004000"
},
{
"name": "VMware_Lenovo_server-09.tme.nebulon.com_os",
"wwn": "6f497c2006187474000ab00000000000"
}
]
}
提前感谢您的时间和考虑!
答案1
所以我想到了这个解决方案。使用“rejectattr”,我省略了名称中包含“_os”的所有卷。也许可以用一种更优雅的方式来完成,这样我就不需要使用 json 查询了,但它完成了工作。
- name: Filter out OS volumes
set_fact:
shared_volumes: "{{ npod_volumes.volumes |
rejectattr('name', 'contains', '_os') |
sort(attribute='name') | list }}"
register: result
tags: volumes
# This builds a list of volumes names and their WWNs
- name: Set volume name and WWN
set_fact:
volume_pairs: "{{ shared_volumes | json_query(volume_query) }}"
vars:
volume_query: "[].{name: name, wwn: wwn}"
tags: volumes
# {
# "volume_pairs": [
# {
# "name": "VMware_Lenovo_VV4",
# "wwn": "6f497c2006187474000ab00000004000"
# },
# {
# "name": "VMware_Lenovo_VV5",
# "wwn": "6f497c2006187474000ab00000005000"
# },
# {
# "name": "VMware_Lenovo_VV6",
# "wwn": "6f497c2006187474000ab00000006000"
# },
# {
# "name": "VMware_Lenovo_VV7",
# "wwn": "6f497c2006187474000ab00000007000"
# }
# ]
# }