# Fetching varialble data into loops iterate/repeat

**URL:** <https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728>\
**Category:** Ansible Project\
**Created:** [March 4, 2023, 12:27pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728 "2023-03-04T12:27:52Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![AharonuReddy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aharonureddy/32/5464_2.png) [@AharonuReddy](https://forum.ansible.com/u/AharonuReddy)\
**Post date:** [March 4, 2023, 12:27pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728/1 "2023-03-04T12:27:52Z")

</div>

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”  
]  
]  
}

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 6, 2023, 12:54pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728/2 "2023-03-06T12:54:46Z")

</div>

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%7Cd01184be3425441edc7f08db1cabea6c%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638135296936895989%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=8ufA%2FwkmWiC3nxg1BzqEHFbaiEIgeJsE9ZDnbuhO%2BUQ%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

---

<div class="post-metadata">

**Author:** ![AharonuReddy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aharonureddy/32/5464_2.png) [@AharonuReddy](https://forum.ansible.com/u/AharonuReddy)\
**Post date:** [March 6, 2023, 1:24pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728/3 "2023-03-06T13:24:19Z")

</div>

Yes, I got it what I was looking for.

Thank you very much for your help **Todd** and **Walter**.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 6, 2023, 2:13pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728/4 "2023-03-06T14:13:47Z")

</div>

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: “{{ 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.

---

<div class="post-metadata">

**Author:** ![AharonuReddy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aharonureddy/32/5464_2.png) [@AharonuReddy](https://forum.ansible.com/u/AharonuReddy)\
**Post date:** [March 6, 2023, 3:36pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728/5 "2023-03-06T15:36:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 6, 2023, 3:58pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728/6 "2023-03-06T15:58:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AharonuReddy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aharonureddy/32/5464_2.png) [@AharonuReddy](https://forum.ansible.com/u/AharonuReddy)\
**Post date:** [March 6, 2023, 5:10pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728/7 "2023-03-06T17:10:46Z")

</div>

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  
}

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 6, 2023, 5:40pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728/8 "2023-03-06T17:40:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AharonuReddy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aharonureddy/32/5464_2.png) [@AharonuReddy](https://forum.ansible.com/u/AharonuReddy)\
**Post date:** [March 6, 2023, 6:04pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728/9 "2023-03-06T18:04:42Z")

</div>

Hi Walter,

Sorry for the trouble. I got still some error

TASK [convert list of lists to list of dicts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=[‘cluster1’, ‘aggr1’])  
ok: [localhost] =\> (item=[‘cluster1’, ‘aggr2’])  
ok: [localhost] =\> (item=[‘cluster1’, ‘aggr3’])  
ok: [localhost] =\> (item=[‘cluster1’, ‘aggr2’])  
ok: [localhost] =\> (item=[‘cluster2’, ‘aggr4’])

TASK [convert list of lists to list of dicts 2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {}

MSG:

Invalid data passed to ‘loop’, it requires a list, got this instead: \<generator object sync\_do\_map at 0x7f086aad40f8\>. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 6, 2023, 6:10pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728/10 "2023-03-06T18:10:38Z")

</div>



---

<div class="post-metadata">

**Author:** ![AharonuReddy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aharonureddy/32/5464_2.png) [@AharonuReddy](https://forum.ansible.com/u/AharonuReddy)\
**Post date:** [March 6, 2023, 6:55pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728/11 "2023-03-06T18:55:59Z")

</div>

The same playbook also giving issue from end. May be i will look into in deep if any version dependance.

the output i got for you provided playbook:

TASK [convert list of lists to list of dicts 1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [ciccdot1] =\> (item=[‘Cluster 1’, ‘aggr1’])  
ok: [ciccdot1] =\> (item=[‘Cluster 1’, ‘aggr2’])  
ok: [ciccdot1] =\> (item=[‘Cluster 1’, ‘aggr4’])  
ok: [ciccdot1] =\> (item=[‘Cluster 2’, ‘aggr1’])  
ok: [ciccdot1] =\> (item=[‘Cluster 3’, ‘aggr2’])

TASK [convert list of lists to list of dicts 2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [ciccdot1]: FAILED! =\> {}

MSG:

Invalid data passed to ‘loop’, it requires a list, got this instead: \<generator object sync\_do\_map at 0x7f1b5adf7830\>. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [March 6, 2023, 7:00pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728/12 "2023-03-06T19:00:36Z")

</div>

You probably need to upgrade your ansible.

Walter

---

<div class="post-metadata">

**Author:** ![AharonuReddy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/aharonureddy/32/5464_2.png) [@AharonuReddy](https://forum.ansible.com/u/AharonuReddy)\
**Post date:** [March 6, 2023, 7:06pm UTC](https://forum.ansible.com/t/fetching-varialble-data-into-loops-iterate-repeat/37728/13 "2023-03-06T19:06:57Z")

</div>

Yes guess so, Thanking you once again.
