# How to iterate a list of dictionaries where the dictionary names are contained in a list in Ansible ?

**URL:** <https://forum.ansible.com/t/how-to-iterate-a-list-of-dictionaries-where-the-dictionary-names-are-contained-in-a-list-in-ansible/26446>\
**Category:** Ansible Project\
**Created:** [January 9, 2018, 9:47pm UTC](https://forum.ansible.com/t/how-to-iterate-a-list-of-dictionaries-where-the-dictionary-names-are-contained-in-a-list-in-ansible/26446 "2018-01-09T21:47:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amishi\_Shah](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Amishi\_Shah](https://forum.ansible.com/u/Amishi_Shah)\
**Post date:** [January 9, 2018, 9:47pm UTC](https://forum.ansible.com/t/how-to-iterate-a-list-of-dictionaries-where-the-dictionary-names-are-contained-in-a-list-in-ansible/26446/1 "2018-01-09T21:47:05Z")

</div>

Hi team,

Below is the scenario and snippet of the files.

~/ansible/group\_vars/all/main.yml

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 10, 2018, 6:49am UTC](https://forum.ansible.com/t/how-to-iterate-a-list-of-dictionaries-where-the-dictionary-names-are-contained-in-a-list-in-ansible/26446/2 "2018-01-10T06:49:27Z")

</div>

Check out loop\_control  
[http://docs.ansible.com/ansible/latest/playbooks\_loops.html#loop-control](http://docs.ansible.com/ansible/latest/playbooks_loops.html#loop-control)

---

<div class="post-metadata">

**Author:** ![Amishi\_Shah](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Amishi\_Shah](https://forum.ansible.com/u/Amishi_Shah)\
**Post date:** [January 10, 2018, 9:08pm UTC](https://forum.ansible.com/t/how-to-iterate-a-list-of-dictionaries-where-the-dictionary-names-are-contained-in-a-list-in-ansible/26446/3 "2018-01-10T21:08:47Z")

</div>

Hi Kai Stian Olstad,

Thanks for the reply.

Yes I did try the loop\_control. The problem with loop\_control is when it iterates over the list\_of\_dict and takes each item of it, it considers the item as “String” and not the dict to iterate over, so I get the error saying with\_dict expects a dict on the task of iterating over the dict.

Here are the playbooks and ansible output.

**test.yml**

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 12, 2018, 6:09am UTC](https://forum.ansible.com/t/how-to-iterate-a-list-of-dictionaries-where-the-dictionary-names-are-contained-in-a-list-in-ansible/26446/4 "2018-01-12T06:09:11Z")

</div>

> Yes I did try the loop\_control. The problem with loop\_control is when it  
> iterates over the list\_of\_dict and takes each item of it, it considers the  
> item as "String" and not the dict to iterate over, so I get the error  
> saying with\_dict expects a dict on the task of iterating over the dict.

Since you variable look like this

list\_of\_dict:  
&nbsp;&nbsp;- dict1\_test  
&nbsp;&nbsp;- dict2\_test  
dict1\_test:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var1: test1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var2: test2  
dict2\_test:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var3: test3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var4: test4

Your list\_of\_dict is a list of string literally called dict1\_test and dict2\_test.  
If you like to have the content you need to specify them as variables.

list\_of\_dict:  
&nbsp;&nbsp;- '{{ dict1\_test }}'  
&nbsp;&nbsp;- '{{ dict2\_test }}'

With this list\_of\_dict will contain the content of dict1\_test and dict2\_test and not just the names.

You can use both, the only difference is how your something.yml look like.

> Here are the playbooks and ansible output.
> 
> \*test.yml\*
> 
> ---  
> - hosts: localhost  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: print  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include: "something.yml listDict={{listDict}}"

Remove the listDict={{listDict}}, it shouldn't be there  
&nbsp;&nbsp;&nbsp;include: something.yml

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ list\_of\_dict }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_control:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_var: listDict
> 
> \*something.yml\*  
> ---  
> - debug: var=listDict
> 
> - name: print the task  
> &nbsp;&nbsp;debug: msg="{{ item.key }} is key,{{ item.value }} is value"  
> &nbsp;&nbsp;with\_dict: listDict

Here you feed the with\_dict the literally string listDict and not it content, to do that you need to say its a varaiable  
&nbsp;&nbsp;&nbsp;with\_dict: '{{ listDict }}'

So it you would like to use  
list\_of\_dict:  
&nbsp;&nbsp;- dict1\_test  
&nbsp;&nbsp;- dict2\_test

This something.yml will work  
&nbsp;&nbsp;&nbsp;- debug: var=listDict

&nbsp;&nbsp;&nbsp;- name: print the task  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug: msg="{{ item.key }} is key,{{ item.value }} is value"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_dict: '{{ vars[listDict] }}'

If you would like to use  
list\_of\_dict:  
&nbsp;&nbsp;- '{{ dict1\_test }}'  
&nbsp;&nbsp;- '{{ dict2\_test }}'

This something.yml will work  
&nbsp;&nbsp;&nbsp;- debug: var=listDict

&nbsp;&nbsp;&nbsp;- name: print the task  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug: msg="{{ item.key }} is key,{{ item.value }} is value"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_dict: '{{ listDict }}'
