Nested loop in template

24 views
Skip to first unread message

David Goodall

unread,
Apr 8, 2016, 5:34:16 AM4/8/16
to Ansible Project
Hi,

I'm trying to create an Apache vhost config file using a legacy yaml file *that I can't change*.

I'm nearly there, but the nested loop in the template doesn't work as expected.


Here's the yaml:


sites:


  site1.com:

    vhosts:

      - vhost1.com

      - vhost2.com

      - vhost3.com

    other1: blah

    other2: blah

etc

etc

  site50.com:

    vhosts:

      - vhost51.com

      - vhost52.com

     - vhost53.com

    other1: blah

    other2: blah


Here's my play:


- name: Populate vhost config

  template: src=httpd.j2 dest=/etc/httpd/conf.d/vhosts.conf

  with_dict: "{{ sites }}"


Here's my template:


<VirtualHost *>

{% for site in sites %}

    ServerName {{ site }}

    {% for vhost in item.value.vhosts %}

    ServerAlias {{ vhost }}

    {% endfor %}

        DocumentRoot /var/www/html/{{ site }}

        <Directory /var/www/html/{{ site }}

                AllowOverride All

                Order allow,deny

                Allow from All

        </Directory>

{% endfor %}

</Virtualhost>


Expected result:


<VirtualHost *>

    ServerName site1.com

        ServerAlias vhost1.com

        ServerAlias vhost2.com

        ServerAlias vhost3.com

        DocumentRoot /var/www/html/site1.com

        <Directory /var/www/html/site1.com

                AllowOverride All

                Order allow,deny

                Allow from All

        </Directory>

etc

etc

    ServerName site50.com

        ServerAlias vhost51.com

        ServerAlias vhost52.com

        ServerAlias vhost53.com

        DocumentRoot /var/www/html/site50.com

        <Directory /var/www/html/site50.com

                AllowOverride All

                Order allow,deny

                Allow from All

        </Directory>

etc.

etc.


Actual result:


<VirtualHost *>

    ServerName site1.com

        ServerAlias vhost51.com

        ServerAlias vhost52.com

        ServerAlias vhost53.com

        DocumentRoot /var/www/html/site1.com

        <Directory /var/www/html/site1.com

                AllowOverride All

                Order allow,deny

                Allow from All

        </Directory>

etc

etc

    ServerName site50.com

        ServerAlias vhost51.com

        ServerAlias vhost52.com

        ServerAlias vhost53.com

        DocumentRoot /var/www/html/site50.com

        <Directory /var/www/html/site50.com

                AllowOverride All

                Order allow,deny

                Allow from All

        </Directory>

etc.

etc.

David Goodall

unread,
Apr 8, 2016, 10:00:09 AM4/8/16
to Ansible Project
Solved it - had to use "key,value" - here is the revised template:

<VirtualHost *>
{% for site,value in sites.iteritems() %}
    ServerName {{ site }}
    {% for vhost in value.vhosts %}
    ServerAlias {{ vhost }}
    {% endfor %}
        DocumentRoot /var/www/html/{{ site }}
        <Directory /var/www/html/{{ site }}
                AllowOverride All
                Order allow,deny
                Allow from All
        </Directory>
{% endfor %}
</Virtualhost>

iteritems() is there to get around the "too many values to unpack" problem.
Reply all
Reply to author
Forward
0 new messages