How to loop over variables so task "template" can generate 1 file per variable

3,609 views
Skip to first unread message

Valentino Gagliardi

unread,
Apr 30, 2014, 9:15:16 AM4/30/14
to ansible...@googlegroups.com
Hello friends.

Given a list of variables like this:

---
nginx_vhosts:
  - {domain: "thisisatest.com", usname: "thisisatest"}
  - {domain: "example.com", usname: "example"}
  - {domain: "company.com", usname: "company"}

assuming that all works fine in Jinja with:

....
{% for vhost in nginx_vhosts %}
    server_name {{ vhost.domain }} www.{{ vhost.domain }};
    root /home/{{ vhost.usname }}/public_html/;
{% endfor }
...

how to loop over nginx_vhosts.domain so task "template" can generate 1 file per domain? This task:

- name: Enable Vhost on CentOS OS family
  template: src=templates/vhost-nginx.j2
            dest=/etc/nginx/conf.d/{% for vhost in nginx_vhosts %}{{ vhost.domain }}.conf{% endfor %}
            owner=root
            group=root
            mode=0644

give me: thisisatest.com.confexample.com.confcompany.com.conf

Already tried this but no luck, gives me 1 file per domain and all files have all vhosts.domain inside:

- name: Enable Vhost on CentOS OS family
  template: src=templates/vhost-nginx.j2
            dest=/etc/nginx/conf.d/{{ item.domain }}.conf
            owner=root
            group=root
            mode=0644
  with_item: nginx_vhosts

Thank you!

Brian Coca

unread,
Apr 30, 2014, 9:28:02 AM4/30/14
to ansible...@googlegroups.com
use 2nd option (with_items: nginx_vhosts) and use this template:


    server_name {{ item.domain }} www.{{ item.domain }};
    root /home/{{ item.usname }}/public_html/;

Servermanaged

unread,
Apr 30, 2014, 9:34:43 AM4/30/14
to ansible...@googlegroups.com
Thank you Brian! It worked!


--
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/DWOwJOyga24/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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CADn%2BHsy%2BqqfHXBBDfHzStyeC%3DY6Mz2oRiseVA8tQSBuabB_O4w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages