3

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 !

1 Answer 1

3

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 }}"
4
  • 1
    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 ?
    – motorbass
    Commented Apr 4, 2023 at 12:55
  • 1
    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.
    – Saxtheowl
    Commented Apr 4, 2023 at 13:20
  • 1
    Alright, thank you a lot for all of these explanation, it really helps me a lot lot lot !!! ;)
    – motorbass
    Commented Apr 4, 2023 at 13:34
  • You welcome :))
    – Saxtheowl
    Commented Apr 4, 2023 at 13:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .