我目前正在编写一本在 Nutanix Prism Central 上创建 K8s 集群的剧本。一切运行顺利,但当一个或多个集群上存在 VLAN 时,我遇到了一个问题。
在我的例子中,一个 VLAN 由它的名称和它的 UUID 表示(这个可以在 entities[] 内的每个索引上被检索到元数据中)。
由于一个 VLAN 可能存在多次(因为它是在不同的集群上配置的),这意味着 UUID 值在元数据 {} 上可能不同,但它的名称永远不会。下面是我得到的结果:
ok: [localhost] => {
"msg": {
"changed": false,
"error": null,
"failed": false,
"response": {
"api_version": "3.1",
"entities": [
{
"metadata": {
"categories": {},
"categories_mapping": {},
"creation_time": "2023-03-08T10:11:44Z",
"kind": "subnet",
"last_update_time": "2023-03-08T10:11:44Z",
"spec_hash": "00000000000000000000000000000000000000000000000000",
"spec_version": 0,
"uuid": "aaaa-bbbb-cccc-dddddd"
},
"spec": {
"cluster_reference": {
"kind": "cluster",
"name": "Cluster02",
"uuid": "123123-123123-123123"
},
"name": "MY-VLAN",
"resources": {
"subnet_type": "VLAN",
"virtual_switch_uuid": "321-321-321-321-321",
"vlan_id": 66,
"vswitch_name": "br0"
}
},
"status": {
"cluster_reference": {
"kind": "cluster",
"name": "Cluster02",
"uuid": "123123-123123-123123"
},
"name": "MY-VLAN",
"resources": {
"ip_usage_stats": {
"num_macs": 0
},
"subnet_type": "VLAN",
"virtual_switch_uuid": "321-321-321-321-321",
"vlan_id": 66,
"vswitch_name": "br0"
},
"state": "COMPLETE"
}
},
{
"metadata": {
"categories": {},
"categories_mapping": {},
"creation_time": "2023-03-08T10:11:42Z",
"kind": "subnet",
"last_update_time": "2023-03-08T10:11:42Z",
"spec_hash": "00000000000000000000000000000000000000000000000000",
"spec_version": 0,
"uuid": "999-999-999-999-999"
},
"spec": {
"cluster_reference": {
"kind": "cluster",
"name": "Cluster01",
"uuid": "0005f64b-e395-8cc2-16af-88e9a456e31a"
},
"name": "MY-VLAN",
"resources": {
"subnet_type": "VLAN",
"virtual_switch_uuid": "d4f9ed4f-72c3-4ad0-8223-070fba6a9aea",
"vlan_id": 66,
"vswitch_name": "br0"
}
},
"status": {
"cluster_reference": {
"kind": "cluster",
"name": "Cluster01",
"uuid": "0005f64b-e395-8cc2-16af-88e9a456e31a"
},
"name": "MY-VLAN",
"resources": {
"ip_usage_stats": {
"num_macs": 0
},
"subnet_type": "VLAN",
"virtual_switch_uuid": "d4f9ed4f-72c3-4ad0-8223-070fba6a9aea",
"vlan_id": 66,
"vswitch_name": "br0"
},
"state": "COMPLETE"
}
}
}
}
}
目前,如果我通过执行以下任务知道索引号,我知道如何手动检索 UUID:
- name: Get Network
ntnx_subnets_info:
filter:
#subnet_type: "VLAN"
name: "MY-VLAN"
kind: subnet
register: result
ignore_errors: True
- name: output of identified network
debug:
msg: '{{ result.response.entities[0].metadata.uuid }}'
但我目前无法在专用集群上获取 UUID。例如,如果我需要从 Cluster01 检索 vlan uuid,我必须选择“{{ result.response.entities[1].metadata.uuid }}”
有人知道是否可以根据嵌套属性(在我的例子中是 spec.cluster_reference.name)动态检索值吗?
多谢 !
我认为您可以将
selectattr
过滤器与map
您可以像这样检索 VLAN 的 UUID: