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.


On Friday, April 8, 2016 at 10:34:16 AM UTC+1, David Goodall wrote:
>
> 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
>
>       - v <http://www.alpha.org/>host2.com
>
>       - v <http://brand2014.test.alpha.org/>host3.com
>
>     other1: blah
>
>     other2: blah
>
> etc
>
> etc
>
>   s <http://shop.alpha.org/>ite50.com:
>
>     vhosts:
>
>       - v <http://shop.alpha.org/>host51.com
>
>       - vhost52.com
>
>      - v <http://online.alpha.org/>host53.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 <http://vhost51.com/>
>
>         ServerAlias vhost2.com <http://vhost52.com/>
>
>         ServerAlias vhost3.com <http://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.
>
>
> 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.
>

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/2072c47a-9fc7-4d9d-9ba7-21457e7de54c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to