How to create multiple folders with loop in ansible

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

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_filters.html

http://docs.ansible.com/ansible/playbooks_loops.html

Hello Deborah,

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

Sincerely, Evgen

You have typo here

register: dies

it shoild be:

register: dirs

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