Calling "sub" variable in task

44 views
Skip to first unread message

Martin Božič

unread,
May 19, 2014, 6:34:22 AM5/19/14
to ansible...@googlegroups.com
Hi,

I'm trying to deploy Apache with virtualhosts and their aliases and
directories. In order to do so I've setup vars host_vars, Apache vhost config
template and task file. With this setup I can create complete config per
virtualhost, but I can't figure out how to call the var within task to create
the directory.

host_vars/myhost example with two virtualhosts:

---
apache_vhosts:
- server_name: www.example.com
doc_root: "/var/www/www.example.com/html"
server_admin: "webm...@example.com"
alias:
- { name: wiki, path: /var/www/www.example.com/wiki }
- { name: blog, path: /var/www/www.example.com/blog }
vhost_dir:
- "/usr/local/share/custom_app1"
- "/usr/local/share/custom_app2"
- server_name: other.example.com
doc_root: "/var/www/other.example.com/html"
server_admin: "webm...@example.com"
alias:
- { name: blah, path: /var/www/other.example.com/html }
- { name: duh, path: /var/www/other.example.com/duh }
dirs:
- "/usr/local/share/custom_app1"
- "/usr/local/share/custom_app2"


roles/apache/templates/vhost_conf.j2 example snippet (this works OK):

---
{% if item.vhost_dir is defined %}
{% for d in item.vhost_dir %}
<Directory "{{ d }}">
AllowOverride All
Options -Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>

{% endfor %}
{% endif %}


roles/apache/tasks/main.yml 3 examples (I've tried all sorts of combinations,
nothing works):

---
- name: create additional vhost dirs
file: path={{ item.vhost_dir }} state=directory group=apache mode=0755
with_items: apache_vhosts
when: apache_vhosts is defined

- name: create additional vhost dirs
file: path={{ item.apache_vhosts.vhost_dir }} state=directory
group=apache mode=0755
with_items: apache_vhosts
when: apache_vhosts is defined

- name: create additional vhost dirs
file: path={{ item }} state=directory group=apache mode=0755
with_items: apache_vhosts.vhost_dir
when: apache_vhosts is defined


Ansible either skips the task or reports it done, but the directory isn't
created. I guess this is probably YAML issue and I'm trying to do something
impossible?

Regards,
Martin

Abhijit Menon-Sen

unread,
May 19, 2014, 7:45:12 AM5/19/14
to ansible...@googlegroups.com
At 2014-05-19 12:34:22 +0200, martin...@gmail.com wrote:
>
> - name: create additional vhost dirs
> file: path={{ item.vhost_dir }} state=directory group=apache mode=0755
> with_items: apache_vhosts
> when: apache_vhosts is defined

The reason this doesn't work is that your vhost_dir is a list.

Perhaps you could use something like:

- name: create additional vhost dirs
file: path={{ item.1 }} state=directory group=apache mode=0755
when: apache_vhosts is defined
with_subelements:
- apache_vhosts
- vhost_dirs

http://docs.ansible.com/playbooks_loops.html#looping-over-subelements

-- ams
Reply all
Reply to author
Forward
0 new messages