如何在 Ansible 上解析和过滤字典值?

如何在 Ansible 上解析和过滤字典值?

我目前正在编写一个剧本,用于在 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)动态检索值?

多谢 !

答案1

我认为你可以使用selectattr过滤器以及map

您可以像这样检索 VLAN 的 UUID:

- name: Find VLAN UUID for Cluster01
    set_fact:
      vlan_uuid: "{{ result.response.entities | selectattr('spec.cluster_reference.name', 'equalto', 'Cluster01') | map(attribute='metadata.uuid') | first }}"
  - name: Display VLAN UUID
    debug:
      msg: "VLAN UUID for Cluster01: {{ vlan_uuid }}"

相关内容