# How to create multiple folders with loop in ansible

**URL:** <https://forum.ansible.com/t/how-to-create-multiple-folders-with-loop-in-ansible/19404>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [October 1, 2015, 3:12pm UTC](https://forum.ansible.com/t/how-to-create-multiple-folders-with-loop-in-ansible/19404 "2015-10-01T15:12:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [October 1, 2015, 3:12pm UTC](https://forum.ansible.com/t/how-to-create-multiple-folders-with-loop-in-ansible/19404/1 "2015-10-01T15:12:09Z")

</div>

Hi Deborah,

use with\_items:

- name: Creating multiple tomcat instancefile: dest=/var/lib/tomcat-instance{{ item }} state=directory

with\_items:

- 1
- 2
- 3

Or, maybe better way with\_sequence:

- name: Creating multiple tomcat instance  
file: dest=/var/lib/tomcat-instance{{ item }} state=directory

with\_sequence: start=1 end=3

More about loops: [http://docs.ansible.com/ansible/playbooks\_loops.html](http://docs.ansible.com/ansible/playbooks_loops.html)

---

<div class="post-metadata">

**Author:** ![Evgen\_Morokin](https://avatars.discourse-cdn.com/v4/letter/e/b77776/32.png) [@Evgen\_Morokin](https://forum.ansible.com/u/Evgen_Morokin)\
**Post date:** [October 1, 2015, 7:39pm UTC](https://forum.ansible.com/t/how-to-create-multiple-folders-with-loop-in-ansible/19404/2 "2015-10-01T19:39:12Z")

</div>

Hi, you can do something like:

- name: create dirs  
file: dest=/tmp/tomcat-instance{{ item }} state=directory  
with\_sequence: start=1 end=3  
register: dies

- debug: var=dirs.results

- name: do something with tomcat  
shell: echo “sudo tomcat-instance1-create -p {{ 8080 + (item.item | int) }} -c {{ 8005 + (item.item | int) }} name{{ item.item }}”  
with\_items:

- “{{ dirs.results }}”  
register: something

- debug: var=something.results

Register with debug can produce a lot of interesting information about tasks )  
[http://docs.ansible.com/ansible/playbooks\_variables.html#registered-variables](http://docs.ansible.com/ansible/playbooks_variables.html#registered-variables)

[http://docs.ansible.com/ansible/playbooks\_filters.html](http://docs.ansible.com/ansible/playbooks_filters.html)

[http://docs.ansible.com/ansible/playbooks\_loops.html](http://docs.ansible.com/ansible/playbooks_loops.html)

---

<div class="post-metadata">

**Author:** ![Evgen\_Morokin](https://avatars.discourse-cdn.com/v4/letter/e/b77776/32.png) [@Evgen\_Morokin](https://forum.ansible.com/u/Evgen_Morokin)\
**Post date:** [October 3, 2015, 3:12am UTC](https://forum.ansible.com/t/how-to-create-multiple-folders-with-loop-in-ansible/19404/3 "2015-10-03T03:12:28Z")

</div>

Hello Deborah,

Please find screenshot attached with some explanations, hope it will help.

Sincerely, Evgen

[![](https://lh3.googleusercontent.com/-7_RHTH7hMSk/Vg9G4OnFnKI/AAAAAAAADA4/E00FIFSvVtk/s320/Screenshot%2B2015-10-02%2B21.19.50.png)](https://lh3.googleusercontent.com/-7_RHTH7hMSk/Vg9G4OnFnKI/AAAAAAAADA4/E00FIFSvVtk/s1600/Screenshot%2B2015-10-02%2B21.19.50.png)

---

<div class="post-metadata">

**Author:** ![igorc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/igorc/32/5491_2.png) [@igorc](https://forum.ansible.com/u/igorc)\
**Post date:** [October 4, 2015, 3:25am UTC](https://forum.ansible.com/t/how-to-create-multiple-folders-with-loop-in-ansible/19404/4 "2015-10-04T03:25:07Z")

</div>

You have typo here

register: dies

it shoild be:

register: dirs

---

<div class="post-metadata">

**Author:** ![Evgen\_Morokin](https://avatars.discourse-cdn.com/v4/letter/e/b77776/32.png) [@Evgen\_Morokin](https://forum.ansible.com/u/Evgen_Morokin)\
**Post date:** [October 4, 2015, 3:41am UTC](https://forum.ansible.com/t/how-to-create-multiple-folders-with-loop-in-ansible/19404/5 "2015-10-04T03:41:03Z")

</div>

As it was described in my previous post ;-), but anyway thanks for the note.
