# For Loop Construct in YAML Tasks File to Create Multiple Directories

**URL:** <https://forum.ansible.com/t/for-loop-construct-in-yaml-tasks-file-to-create-multiple-directories/14004>\
**Category:** Ansible Project\
**Created:** [November 26, 2013, 2:08pm UTC](https://forum.ansible.com/t/for-loop-construct-in-yaml-tasks-file-to-create-multiple-directories/14004 "2013-11-26T14:08:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Leikind](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@Michael\_Leikind](https://forum.ansible.com/u/Michael_Leikind)\
**Post date:** [November 26, 2013, 2:08pm UTC](https://forum.ansible.com/t/for-loop-construct-in-yaml-tasks-file-to-create-multiple-directories/14004/1 "2013-11-26T14:08:45Z")

</div>

Hi,  
I would like to use a FOR loop construct to create a multiple directories like this:

> {% for dir in temp\_dirs %}
> 
> - name: {{ dir }} Directory  
> file: dest={{ repo\_dest }}/var/{{ dir }} owner={{ apache\_user }} group={{ apache\_group }} state=directory recurse=yes  
> {% endfor %}

my vars file looks like this:

> “temp\_dirs” : [“bla”, “bla2”]

The syntax above does not comply with YAML :

> ERROR: Syntax Error while loading YAML script, \<…\>  
> Note: The error may actually appear before this position: \<…\>  
> {% for dir in temp\_dirs %}  
> ^

Please advise on how to implement this.  
Thanks !

---

<div class="post-metadata">

**Author:** ![Patrick\_Regan](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@Patrick\_Regan](https://forum.ansible.com/u/Patrick_Regan)\
**Post date:** [November 26, 2013, 4:08pm UTC](https://forum.ansible.com/t/for-loop-construct-in-yaml-tasks-file-to-create-multiple-directories/14004/2 "2013-11-26T16:08:35Z")

</div>

You could try this:

```auto
- name: Add Directories
  file: dest={{ repo_dest }}/var/{{ item }} owner={{ apache_user }} group={{ apache_group }} state=directory recurse=yes
  with_items: temp_dirs

```

The `with_times` will take the contents of your variable `temp_dirs` and one at a time substitute them for the `{{ item }}` variable.
