Ansible printing site URLs in .j2 template file

This is driving me nuts so I hope someone can help me out here. I’m trying to build a role that fits into Trellis and fills a few of my needs.

What I’m looking to do is run a loop in a .j2 template file that prints some of the URLs of the wordpress_sites that are set up under that particular environment. But the catch is I only want to print the URLs of sites that for example have ssl enabled or for another example have multisite enabled.

So I have something like 40 sites configured and say 30 have

  enabled: true

I only want to print out a list of the 30 site URLs in a .j2 template file.

Am I needing to set a variable for that list of sites in the playbook first then use that list to print the URLs in the template file because I can only get the full list of all the URLs to print in the .j2 file, if I try to use any other variables out of wordpress_sites to limit the loop I get an error about the variables not being defined.

It would help to see what code you currently have. But there’s a ton of examples in Trellis for looping over wordpress_sites.

Here’s an example:

So in a template it’s something like:

{% for name, site in wordpress_sites.iteritems() if site.ssl.enabled | default(false) %}
  {{ name }}
  {{ site.site_hosts }}
{% endfor %}

Awesome! The letsencrypt role was pretty much the only one I did not pour through trying to find a conditional loop in a template file. Figures it would be right there.

Thanks a bunch for the quick reply and solving this for me.

Am I missing where you mark a reply as the answer to the question?

You can do it now. It’s not available for topics marked as “general” so I switched it to Trellis.