大网数据广告活动
服务器运维问答

How to parse and filter on dict values on Ansible?

· 52CE 我爱测

I'm currently working on a playbook to create a K8s cluster on Nutanix Prism Central. Everything run smoothly but i face an issue while a VLAN exists on one or more clusters. In my case, a VLAN is represented by its name but also…

问题原文

原帖链接 · motorbass · CC BY-SA 4.0

I'm currently working on a playbook to create a K8s cluster on Nutanix Prism Central. Everything run smoothly but i face an issue while a VLAN exists on one or more clusters.

In my case, a VLAN is represented by its name but also its UUID (this one can be retrived into metadata on every index inside entities[]).

As a VLAN may exists many times (cause it's configured on different clusters) it means the UUID value may differs on metadata {} but never its name. Below here's the result i got :

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"
                    }
                }
        }
    }
}

At the moment, i know how to manually retrieve the UUID if I know the index number by having a task like :

  - 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 }}'

But i'm currently not able to get the UUID on a dedicated cluster. For example, if I need to retrieve vlan uuid from Cluster01 , i have to select '{{ result.response.entities[1].metadata.uuid }}'

Does someone knows if it possible to dynamically retrieve a value, based on a nested property (in my case spec.cluster_reference.name) ?

Thanks a lot !

已采纳解答

原帖链接 · Saxtheowl · CC BY-SA 4.0

I think you could use the selectattr filter along with map

You could retrieve the UUID of the VLAN like that:

- 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 }}"

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-04-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。