0

I'm trying to transform keys of the items "datacenter_clusters" to a key/value pair in itself. This will also to loop through the following list of dict :

"list_of_dict": [
        {
            "datacenter": "DC_A",
            "datacenter_clusters": {
                "CLUSTER_ALPHA": {
                    "datacenter": "DC_A",
                    "cluster_info_1": "xxxxxx",
                    "cluster_info_2": true,
                    "hosts": [
                        {
                            "folder": "/path/to/host-1",
                            "name": "hostname-1"
                        },
                        {
                            "folder": "/path/to/host-2",
                            "name": "hostname-2"
                        }
                },
                "CLUSTER_BETA": {
                    "datacenter": "DC_A",
                    "cluster_info_1": "yyyyyyy",
                    "cluster_info_2": false,
                }
            }
            "vcenter": "vcenter-1"
        },
        {
            "datacenter": "DC_B",
            "datacenter_clusters": {
                "CLUSTER_OMEGA": {
                    "datacenter": "DC_B",
                    "cluster_info_1": "aaaaaaaa",
                    "cluster_info_2": true,
                },
                "CLUSTER_ZETA": {
                    "datacenter": "DC_A",
                    "cluster_info_1": "bbbbbbbbbb",
                    "cluster_info_2": false,
                }
            }
            "vcenter": "vcenter-2"
        }
]

and add the new key/value pair "cluster_name":"<KEY_OF_PARENT_ELEMENT>" thus transforming the dict "datacenter_clusters" to a list. The expected result should look like :

"list_of_dict": [
        {
            "datacenter": "DC_A",
            "datacenter_clusters": [
                {           
                    "cluster_name": "CLUSTER_ALPHA",
                    "datacenter": "DC_A",
                    "cluster_info_1": "xxxxxx",
                    "cluster_info_2": true,
                    "hosts": [
                        {
                            "folder": "/path/to/host-1",
                            "name": "hostname-1"
                        },
                        {
                            "folder": "/path/to/host-2",
                            "name": "hostname-2"
                        }
                    ]
                },
                {
                    "cluster_name": "CLUSTER_ALPHA",
                    "datacenter": "DC_A",
                    "cluster_info_1": "yyyyyyy",
                    "cluster_info_2": false,
                }
            ]
            "vcenter": "vcenter-1"
        },
        {
            "datacenter": "DC_B",
            "datacenter_clusters": [
                {
                    "cluster_name": "CLUSTER_OMEGA"
                    "datacenter": "DC_B",
                    "cluster_info_1": "aaaaaaaa",
                    "cluster_info_2": true,
                },
                {
                    "cluster_name": "CLUSTER_ZETA"
                    "datacenter": "DC_A",
                    "cluster_info_1": "bbbbbbbbbb",
                    "cluster_info_2": false,
                }
            ]
            "vcenter": "vcenter-2"
        }
]

I tried to combine what I found in this link with "with_subelements" but I couldn't loop through a list of dicts, I could only loop through a simple dict or a simple list or a list of lists. Any help would be much appreciated

damien
  • 15
  • 5
  • 1
    Can you specify what code you have done so far and where specifically you are struggling? – Jose Marin Feb 01 '21 at 12:38
  • If you haven't yet found it, you should have a look at the [`dict2items` filter](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#transforming-dictionaries-into-lists) – Zeitounator Feb 01 '21 at 13:00
  • I tried to combine what I found in this [link](https://stackoverflow.com/questions/43202825/ansible-jinja2-how-to-append-key-into-list-of-dict) with "with_subelements" but I couldn't loop through a list of dicts, I could only loop through a simple dict or a simple list or a list of lists – damien Feb 01 '21 at 15:53

0 Answers0