Absent community.general.dict you can do it in two steps:
- name: convert list of lists to list of dicts 1 set_fact: json_list: "{{ aggregates2 | map('zip',['cluster','aggregate']) | map('map','reverse') }}" loop: "{{ aggregates2 }}" - name: convert list of lists to list of dicts 2 set_fact: new_list: "{{ (new_list | default([])) + [ { item.0.0: item.0.1, item.1.0: item.1.1 } ] }}" loop: "{{ json_list }}" Walter -- Walter Rowe, Division Chief Infrastructure Services, OISM Mobile: 202.355.4123 On Mar 6, 2023, at 12:10 PM, Aharonu <aharon...@gmail.com> wrote: Hi, Is there any version dependance to use community.general.dict? my current Ansible version 2.9.13. When i use - name: convert list of lists to list of dicts set_fact: json_list: "{{ aggregates | map('zip',['cluster','aggregate']) | map('map','reverse') | map('community.general.dict') }}" output: ok: [localhost] => { "json_list": "<generator object sync_do_map at 0x7f47f2a99fc0>" When i use: - name: convert list of lists to list of dicts set_fact: json_list: "{{ aggregates | map('zip',['cluster','aggregate']) | map('map','reverse') | map('community.general.dict')|list }}" Output: An exception occurred during task execution. To see the full traceback, use -vvv. The error was: jinja2.exceptions.TemplateRuntimeError: No filter named 'community.general.dict'. fatal: [localhost]: FAILED! => { "changed": false } On Mon, Mar 6, 2023 at 3:58 PM 'Rowe, Walter P. (Fed)' via Ansible Project <ansible-project@googlegroups.com<mailto:ansible-project@googlegroups.com>> wrote: No problem. - name: convert list of lists to list of dicts set_fact: json_list: "{{ aggregates2 | map('zip',['cluster','aggregate']) | map('map','reverse') | map('community.general.dict') }}" produces "json_list": [ { "aggregate": "aggr1", "cluster": "Cluster 1" }, { "aggregate": "aggr2", "cluster": "Cluster 1" }, { "aggregate": "aggr4", "cluster": "Cluster 1" }, { "aggregate": "aggr1", "cluster": "Cluster 2" }, { "aggregate": "aggr2", "cluster": "Cluster 3" } ] Walter -- Walter Rowe, Division Chief Infrastructure Services, OISM Mobile: 202.355.4123 On Mar 6, 2023, at 10:36 AM, Aharonu <aharon...@gmail.com<mailto:aharon...@gmail.com>> wrote: Hi, All and Walter, Thank you for good suggestions. That's a really good way to deal with it but there is something I should mention here to request help. Actually below aggregates data coming from another set_task ( see below) which contains a list data. vars: aggregates: [] aggregates data coming below set_fact. - set_fact: aggregates: "{{ aggregates|default([]) + ([item.item]| product(item | json_query(get_attrs)))| map('flatten')|list }}" vars: get_attrs: "ontap_info.storage_aggregates.records[*].[name]" loop: "{{ ontap.results }}" no_log: true Looking for: As above mentioned set_fact giving data into list into lists, Can we make aggregates data into dict structure as mentioned below from same set_fact ? aggregates: - { cluster: "Cluster 1", aggregate: "aggr1" } - { cluster: "Cluster 1", aggregate: "aggr2" } - { cluster: "Cluster 1", aggregate: "aggr4" } - { cluster: "Cluster 2", aggregate: "aggr1" } - { cluster: "Cluster 3", aggregate: "aggr2" } Thank you for your time and helping out. On Mon, Mar 6, 2023 at 2:14 PM 'Rowe, Walter P. (Fed)' via Ansible Project <ansible-project@googlegroups.com<mailto:ansible-project@googlegroups.com>> wrote: If you are defining aggregates (vs getting from some other task register var), there is another way you could construct your aggregates var. aggregates: - { cluster: "Cluster 1", aggregate: "aggr1" } - { cluster: "Cluster 1", aggregate: "aggr2" } - { cluster: "Cluster 1", aggregate: "aggr4" } - { cluster: "Cluster 2", aggregate: "aggr1" } - { cluster: "Cluster 3", aggregate: "aggr2" } This syntax makes it really easy to add more items and maintain proper syntax. Your item.0 changes to item.cluster, and your item.1 change to item.aggregate. - name: Get info na_ontap_rest_info: <<: *login gather_subset: - storage/volumes parameters: aggregates.name<https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Faggregates.name%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7C69767c1f1c924200923f08db1e65c4f3%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638137194670729272%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=nhJGV9raD1uxD%2BSPQAqX%2F5AC7wXFzeWC4S2r4%2Fspt0s%3D&reserved=0>: "{{ item.cluster }}" hostname: "{{ item.aggregate }}" use_python_keys: true register: vol_data loop: "{{ aggregates }}" no_log: false - debug: var=vol_data This might be more description and easier for someone else to make the connection between your aggregates var and your loop references. The aggregates var is still a list. It is a list of JSON dictionary items. --- - name: test loop with JSON dict list hosts: all become: false gather_facts: false vars: aggregates: - { cluster: "Cluster 1", aggregate: "aggr1" } - { cluster: "Cluster 1", aggregate: "aggr2" } - { cluster: "Cluster 1", aggregate: "aggr4" } - { cluster: "Cluster 2", aggregate: "aggr1" } - { cluster: "Cluster 3", aggregate: "aggr2" } tasks: - name: show reference debug: msg="cluster {{ item.cluster }}, hostname {{ item.aggregate }}" loop: "{{ aggregates }}" % ansible-playbook -i localhost, netapp.yml PLAY [test loop with JSON dict list] *********************************************************************************** TASK [show reference] ************************************************************************************************** ok: [localhost] => (item={'cluster': 'Cluster 1', 'aggregate': 'aggr1'}) => { "msg": "cluster Cluster 1, hostname aggr1" } ok: [localhost] => (item={'cluster': 'Cluster 1', 'aggregate': 'aggr2'}) => { "msg": "cluster Cluster 1, hostname aggr2" } ok: [localhost] => (item={'cluster': 'Cluster 1', 'aggregate': 'aggr4'}) => { "msg": "cluster Cluster 1, hostname aggr4" } ok: [localhost] => (item={'cluster': 'Cluster 2', 'aggregate': 'aggr1'}) => { "msg": "cluster Cluster 2, hostname aggr1" } ok: [localhost] => (item={'cluster': 'Cluster 3', 'aggregate': 'aggr2'}) => { "msg": "cluster Cluster 3, hostname aggr2" } PLAY RECAP ************************************************************************************************************* localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 Just another idea ... Walter -- Walter Rowe, Division Chief Infrastructure Services, OISM Mobile: 202.355.4123 On Mar 6, 2023, at 8:24 AM, Aharonu <aharon...@gmail.com<mailto:aharon...@gmail.com>> wrote: Yes, I got it what I was looking for. Thank you very much for your help Todd and Walter. On Mon, 6 Mar 2023, 18:25 'Rowe, Walter P. (Fed)' via Ansible Project, <ansible-project@googlegroups.com<mailto:ansible-project@googlegroups.com>> wrote: Your 'aggregates' is a list of lists. For each aggregate list item, you have a two-element list where element [0] is cluster name and element [1] is aggregate name. Loop through aggregates, and use item.0 for cluster and item.1 for hostname ? - name: Get info na_ontap_rest_info: <<: *login gather_subset: - storage/volumes parameters: aggregates.name<https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Faggregates.name%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7C69767c1f1c924200923f08db1e65c4f3%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638137194670729272%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=nhJGV9raD1uxD%2BSPQAqX%2F5AC7wXFzeWC4S2r4%2Fspt0s%3D&reserved=0>: "{{ item.1 }}" hostname: "{{ item.0 }}" use_python_keys: true register: vol_data loop: "{{ aggregates }}" loop_control: label: "{{ ?? }}" no_log: false - debug: var=vol_data Walter -- Walter Rowe, Division Chief Infrastructure Services, OISM Mobile: 202.355.4123 On Mar 4, 2023, at 7:27 AM, Aharonu <aharon...@gmail.com<mailto:aharon...@gmail.com>> wrote: Hi Team, Greetings for the day! I have a variables called 'aggregates' under vars which ontaints cluster1,cluster3,cluster2..etc and aggr1,aggr2,aggr3..etc list as mentioned below. { "aggregates": [ [ "Cluster1", "aggr1" ], [ "Cluster1", "aggr2" ], [ "Cluster1", "aggr4" ], [ "Cluster2", "aggr1" ], [ "Cluster3", "aggr2" ] ] } ******************************************************************** Based on data from 'aggregates', Below playbook has 2 items to work on: aggregates.name<https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Faggregates.name%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7C69767c1f1c924200923f08db1e65c4f3%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638137194670729272%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=nhJGV9raD1uxD%2BSPQAqX%2F5AC7wXFzeWC4S2r4%2Fspt0s%3D&reserved=0>: here we need to use aggr1,aggr3,aggr2...from variable 'aggregates' hostname: here we need to use Cluster1,Cluster2,Cluster3...from variable 'aggregates' What looking for is: it has to go through loop iteration until 'aggregates' list completed. First loop iteration has to go for Cluster1, aggr1, Second loop iteration to go for Cluster1, aggr2 based on data from 'aggregates'. 1st loop iteration: aggregates.name<https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Faggregates.name%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7C69767c1f1c924200923f08db1e65c4f3%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638137194670729272%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=nhJGV9raD1uxD%2BSPQAqX%2F5AC7wXFzeWC4S2r4%2Fspt0s%3D&reserved=0>: 'aggr1' hostname: 'Cluster1' 2nd loop iteration: aggregates.name<https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Faggregates.name%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7C69767c1f1c924200923f08db1e65c4f3%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638137194670729272%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=nhJGV9raD1uxD%2BSPQAqX%2F5AC7wXFzeWC4S2r4%2Fspt0s%3D&reserved=0>: 'aggr2' hostname: "Cluster1}" Playbook: --- - hosts: localhost gather_facts: false collections: - netapp.ontap vars_files: - < login file path > vars: aggregates: [] login: &login username: "{{ username }}" password: "{{ password }}" https: true validate_certs: false feature_flags: trace_apis: true tasks: - name: Get info na_ontap_rest_info: <<: *login gather_subset: - storage/volumes parameters: aggregates.name<https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Faggregates.name%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7C69767c1f1c924200923f08db1e65c4f3%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638137194670729272%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=nhJGV9raD1uxD%2BSPQAqX%2F5AC7wXFzeWC4S2r4%2Fspt0s%3D&reserved=0>: "{{ ?? }}" --> here we need to use aggr1,aggr3,aggr2...from variable 'aggregates' hostname: "{{ ?? }}" --> here we need to use Cluster1,Cluster2,Cluster3...from variable 'aggregates' use_python_keys: true register: vol_data loop: "{{ ?? }}" loop_control: label: "{{ ?? }}" no_log: false - debug: var=vol_data ... -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com<mailto:ansible-project+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANGEjuX20ndh8%2Bh_oCx8nGGBJTZ4oeEFfGQb0tWy6MXAuhTucA%40mail.gmail.com<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2FCANGEjuX20ndh8%252Bh_oCx8nGGBJTZ4oeEFfGQb0tWy6MXAuhTucA%2540mail.gmail.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C69767c1f1c924200923f08db1e65c4f3%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638137194670729272%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=UQYQe39y6UGSbA2RBfSiy6Xpk%2FAo2YyQ%2FUifm%2BZXIko%3D&reserved=0>. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com<mailto:ansible-project+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/03325950-FBD3-49F2-82BC-485D290BDEE1%40nist.gov<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2F03325950-FBD3-49F2-82BC-485D290BDEE1%2540nist.gov%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C69767c1f1c924200923f08db1e65c4f3%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638137194670729272%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=%2F5FWpzkpjLp0VPCZzAUEhk4U8pw3oz7vbtrBIWCGzJc%3D&reserved=0>. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com<mailto:ansible-project+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANGEjuXJdpJe7gBCCY1nQYxhz44DuQA4V7c5qVXAakPCe_-hEA%40mail.gmail.com<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2FCANGEjuXJdpJe7gBCCY1nQYxhz44DuQA4V7c5qVXAakPCe_-hEA%2540mail.gmail.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C69767c1f1c924200923f08db1e65c4f3%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638137194670729272%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=ixPJysM%2BEZ5WXiDAPOsbY4SIe8O9S9MQVwaW%2Fpw4LWQ%3D&reserved=0>. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com<mailto:ansible-project+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3006506F-DEBA-4596-BF35-765F7DA80939%40nist.gov<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2F3006506F-DEBA-4596-BF35-765F7DA80939%2540nist.gov%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C69767c1f1c924200923f08db1e65c4f3%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638137194670885490%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=JaIMne%2BZpLoufFmpIC%2B4yiqGWxHgrFkYVyLKx4VRGC4%3D&reserved=0>. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com<mailto:ansible-project+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANGEjuVhbZtfcH8cYjaM1%2BsypwfbHQK1P_QFQozw3%3DKOizudrA%40mail.gmail.com<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2FCANGEjuVhbZtfcH8cYjaM1%252BsypwfbHQK1P_QFQozw3%253DKOizudrA%2540mail.gmail.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C69767c1f1c924200923f08db1e65c4f3%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638137194670885490%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=qLQQIVeZZwfU93d5AyfKgRKLPmlUq%2Fo7m4ZD4p3LNDQ%3D&reserved=0>. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com<mailto:ansible-project+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/BFABBF7B-29C9-4299-B0A8-3F6FCB1EE68D%40nist.gov<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2FBFABBF7B-29C9-4299-B0A8-3F6FCB1EE68D%2540nist.gov%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C69767c1f1c924200923f08db1e65c4f3%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638137194670885490%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=D44ewyz4sdgUbpro4rUolEdIkDa5Ra4it7qVRr%2BZkx0%3D&reserved=0>. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com<mailto:ansible-project+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANGEjuWVoLVV%2BQJU0AC9fSn%2BCpnPp%2BETkMpCV61fuVt_UbhFhg%40mail.gmail.com<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2FCANGEjuWVoLVV%252BQJU0AC9fSn%252BCpnPp%252BETkMpCV61fuVt_UbhFhg%2540mail.gmail.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C69767c1f1c924200923f08db1e65c4f3%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638137194670885490%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=MB5g9G8HO3Jefb3BOtp41AUi2K5XWZvCSKkcN1sI6Xk%3D&reserved=0>. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/464F213D-3BF5-401F-ADC3-E917E992DDFC%40nist.gov.