# Ansible iterate a dictionary with lists and templates

**URL:** <https://forum.ansible.com/t/ansible-iterate-a-dictionary-with-lists-and-templates/21433>\
**Category:** Ansible Project\
**Created:** [May 13, 2016, 2:56pm UTC](https://forum.ansible.com/t/ansible-iterate-a-dictionary-with-lists-and-templates/21433 "2016-05-13T14:56:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![swen.b](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@swen.b](https://forum.ansible.com/u/swen.b)\
**Post date:** [May 13, 2016, 2:56pm UTC](https://forum.ansible.com/t/ansible-iterate-a-dictionary-with-lists-and-templates/21433/1 "2016-05-13T14:56:48Z")

</div>

Hi,

my goal is to use multiple dictionaries within a template. I want one file per username with all projects in it of this user.  
My playbook looks like:

```auto

- name: create files template: src=vhost.conf.j2 dest=/tmp/{{ item.0.username }}.conf with_subelements: - "{{ users }}" - projects vars: users: - username: jon password: 123456 sftp: True projects: - projectname: Projekt1 documentroot: root1 - projectname: Projekt2 documentroot: root2 - username: jane password: 123456 projects: - projectname: Projekt3 documentroot: root3 - projectname: Projekt4 documentroot: root4

How does a working {% for %} loop needs to look like in the template file? Let's say I want to insert all projectnames of each username in a per username file?
Thanks for helping me out!

```

---

<div class="post-metadata">

**Author:** ![swen.b](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@swen.b](https://forum.ansible.com/u/swen.b)\
**Post date:** [May 13, 2016, 3:38pm UTC](https://forum.ansible.com/t/ansible-iterate-a-dictionary-with-lists-and-templates/21433/2 "2016-05-13T15:38:02Z")

</div>

Here is the playbook more readable:

```auto
- name: create files
  template:
    src=vhost.conf.j2
    dest=/tmp/{{ item.0.username }}.conf
  with_subelements:
    - "{{ users }}"
    - projects

  vars:
    users:
      - username: jon
        password: 123456
        sftp: True
        projects:
          - projectname: Projekt1
            documentroot: root1
          - projectname: Projekt2
            documentroot: root2

      - username: jane
        password: 123456
        projects:
          - projectname: Projekt3
            documentroot: root3
          - projectname: Projekt4
            documentroot: root4

```
