Re: [ansible-project] How to create multiple folders with loop in ansible

6,068 views
Skip to first unread message
Message has been deleted

David Karban

unread,
Oct 1, 2015, 11:12:44 AM10/1/15
to ansible...@googlegroups.com
Hi Deborah,

use with_items:

- name: Creating multiple tomcat instance
  file: 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


David Karban
Linux server specialist/Specialista na správu linuxových serverů
www.karban.eu

2015-10-01 17:05 GMT+02:00 DEBORAH AKUOKO <ewurama.mi...@gmail.com>:
I am trying to create multiple directories with different names e.g tomacat-1, tomcat-2 etc..
But i want to do this in the form of a loop. Kindly help me with the yaml syntax.

this is what i tried
 
- name: Creating multiple tomcat instance
  file: dest=/var/lib/tomcat-instance{1..3}  state=directory

But instead of creating three directories, it creates only one with name "tomcat-instance{1..3}"
Also i would like each of these folders to have a unique port id and shutdown port  e.g     -p  8000  -c 8006

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c56b427d-167c-4853-91dd-1bbbb1a5cc48%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted
Message has been deleted

Evgen Morokin

unread,
Oct 1, 2015, 4:02:15 PM10/1/15
to Ansible Project
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 )

Message has been deleted
Message has been deleted

Evgen Morokin

unread,
Oct 2, 2015, 11:12:29 PM10/2/15
to Ansible Project
Hello Deborah,

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

Sincerely, Evgen



On Friday, October 2, 2015 at 5:53:07 AM UTC-4, DEBORAH AKUOKO wrote:
Thanks Evgen, 

This works fine. But i keep gettign this error "One or more undefined variables: 'unicode object' has no attribute 'item'"
Please see it clearly in the snapshot attached.

Thanks so much for the help.

Regards,
Deborah.


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/cp5p6rbYkEo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Igor Cicimov

unread,
Oct 3, 2015, 11:25:08 PM10/3/15
to Ansible Project
You have typo here

register: dies

it shoild be:

register: dirs

Evgen Morokin

unread,
Oct 3, 2015, 11:41:03 PM10/3/15
to Ansible Project
As it was described in my previous post ;-), but anyway thanks for the note.
Reply all
Reply to author
Forward
0 new messages