# Using with\_items with a dictionary

**URL:** <https://forum.ansible.com/t/using-with-items-with-a-dictionary/25347>\
**Category:** Ansible Project\
**Created:** [August 28, 2017, 7:51pm UTC](https://forum.ansible.com/t/using-with-items-with-a-dictionary/25347 "2017-08-28T19:51:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Bushey](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@Michael\_Bushey](https://forum.ansible.com/u/Michael_Bushey)\
**Post date:** [August 28, 2017, 7:51pm UTC](https://forum.ansible.com/t/using-with-items-with-a-dictionary/25347/1 "2017-08-28T19:51:19Z")

</div>

Hello,

I have a dict that contains a list of sites. For each of these sites I would like to provide a list of symlinks to create provided by with\_items. Is this possible? I have Ansible 2.3.1.0.

Thanks!  
Michael

---

<div class="post-metadata">

**Author:** ![Uwe\_Sauter](https://avatars.discourse-cdn.com/v4/letter/u/41988e/32.png) [@Uwe\_Sauter](https://forum.ansible.com/u/Uwe_Sauter)\
**Post date:** [August 28, 2017, 7:52pm UTC](https://forum.ansible.com/t/using-with-items-with-a-dictionary/25347/2 "2017-08-28T19:52:40Z")

</div>

I'm not quiet sure I understand your data model but instead of with\_items have a look at with\_dict.

---

<div class="post-metadata">

**Author:** ![Michael\_Bushey](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@Michael\_Bushey](https://forum.ansible.com/u/Michael_Bushey)\
**Post date:** [August 28, 2017, 8:42pm UTC](https://forum.ansible.com/t/using-with-items-with-a-dictionary/25347/3 "2017-08-28T20:42:28Z")

</div>

Thanks for responding Uwe! I appreciate your time. 🙂

I’m already using a with\_dict to get the list of sites, but now for each site I would like to provide a list of symlinks to generate. The list of symlinks does not change.

This creates the docroots:

- name: Create docroot  
file: path=/var/www/html/{{ item.value.url }} state=directory owner=www-data group=www-data  
with\_dict: “{{ sites }}”  
when: item.value.type == “grav”  
tags: grav, grav-sites

I would like to create the same symlinks for each of these sites, but I don’t know how to get {{ site from dict }}:

- name: Create symlinks  
file: dest=/var/www/html/{{ site from dict }}/{{ item }} src=/var/www/grav/grav/{{ item }} state=link owner=www-data group=www-data  
with\_items:
- bin
- CHANGELOG.md
- composer.json
- .editorconfig
- .gitignore
- index.php
- LICENSE.txt
- system
- vendor  
tags: grav, grav-sites

---

<div class="post-metadata">

**Author:** ![Uwe\_Sauter](https://avatars.discourse-cdn.com/v4/letter/u/41988e/32.png) [@Uwe\_Sauter](https://forum.ansible.com/u/Uwe_Sauter)\
**Post date:** [August 29, 2017, 7:04am UTC](https://forum.ansible.com/t/using-with-items-with-a-dictionary/25347/4 "2017-08-29T07:04:52Z")

</div>

My first thought was to put those two tasks into a block, modify task 1 to register a new variable that contains the current site  
and use it in task 2. Then I remembered that you cannot loop over a block (currently…  
[https://github.com/ansible/ansible/issues/13262](https://github.com/ansible/ansible/issues/13262)).

You could try to create a new list variable out of your dict which only contains all {{ site from dict }} and then use  
with\_nested. E.g.

- name: 'create docroot'  
&nbsp;&nbsp;...

- name: 'extract sites'  
&nbsp;&nbsp;set\_fact: sitenames='{{ sitenames | default() }} + {{ item.name }}'  
&nbsp;&nbsp;with\_dict: '{{ sites }}'

- name: 'create symlinks'  
&nbsp;&nbsp;file:  
&nbsp;&nbsp;&nbsp;&nbsp;dest: '/var/www/html/{{ item[0] }}/{{ item[1] }}'  
&nbsp;&nbsp;&nbsp;&nbsp;src: '/var/www/grav/grav/{{ item[1] }}'  
&nbsp;&nbsp;&nbsp;&nbsp;state: link  
&nbsp;&nbsp;&nbsp;&nbsp;owner: www-data  
&nbsp;&nbsp;&nbsp;&nbsp;group: www-data  
&nbsp;&nbsp;with\_nested:  
&nbsp;&nbsp;&nbsp;&nbsp;- '{{ sitenames }}'  
&nbsp;&nbsp;&nbsp;&nbsp;- ['bin', 'CHANGELOG.md', ...]

---

<div class="post-metadata">

**Author:** ![Arbab\_Nazar1](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@Arbab\_Nazar1](https://forum.ansible.com/u/Arbab_Nazar1)\
**Post date:** [August 29, 2017, 10:29am UTC](https://forum.ansible.com/t/using-with-items-with-a-dictionary/25347/5 "2017-08-29T10:29:48Z")

</div>

Hi Michael, This simple example might help you that how we can use the with\_items with dictionary:

`
