Score:2

How to parse and filter on dict values on Ansible?

cn flag

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 !

Score:2
by flag

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 }}"
inframan avatar
cn flag
Wow thanks a lot dude ! i wasn't aware about selectattr and map. So it means, we create a new fact (with set_fact) and this fact is filtered as we want by selecting an attribute equal to the name we need. And finally we map to the other property ? what does the | first means at the end ?
Saxtheowl avatar
by flag
Yes, selectattr filter is used to filter a list of objects based on an attribute value and map is used to transform the filtered list into a list of attribute values. The | first at the end of the pipeline is a Jinja2 filter that is used to select the first item in a list.
inframan avatar
cn flag
Alright, thank you a lot for all of these explanation, it really helps me a lot lot lot !!! ;)
Saxtheowl avatar
by flag
You welcome :))
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.