For Loop Construct in YAML Tasks File to Create Multiple Directories

5,588 views
Skip to first unread message

Michael Leikind

unread,
Nov 26, 2013, 9:08:45 AM11/26/13
to ansible...@googlegroups.com
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 ! 

Patrick Regan

unread,
Nov 26, 2013, 11:08:35 AM11/26/13
to ansible...@googlegroups.com

You could try this:

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

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages