Confusion about nested loop in jinja2 template

Hello Guys,

I got stuck with some little problem on a nginx template and I have not succeeded in making it work. I am working on an nginx template and want to have a list of locations directives containing themselves sub collection of specific directives(instruction collection).

locations:
- context: ~ .php$
instructions:
- “try_files $uri =404”
- “fastcgi_pass unix:/var/run/php5-fpm.sock”
- “fastcgi_index index.php”
- “fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name”
- “include fastcgi_params”

- context: /

instructions:
- “try_files $uri $uri/ /index.php?$args”

The looping part of the template is below:

{% for l in item.value.locations %}
{% for item in l %}
{{ item.value.context }};
{% endfor %}
{****% endfor %}

Trying with with_dict first I could get to the sub collections but only keys of the map get returned. With little of googling I figured I would use with_nested but it didn’t really work out . I am surely not doing something right. Kindly shed some light on how can I get the collections in place.

Full snippet is avaialbe on pastbin.com

Thanks for helping

Best Regards,

Hello Guys,

I got stuck with some little problem on a nginx template and I have not
succeeded in making it work. I am working on an nginx template and want
to have a list of locations directives containing themselves sub
collection of specific directives(instruction collection).

Hi Joseph,

Hoping I am not entering "manspalining" mode or starting some kind of
troll, I would suggest you to reconsider this way of doing things :

* locations:*
* - context: ~ \.php$*
* instructions:*
* - "try_files $uri =404"*
* - "fastcgi_pass unix:/var/run/php5-fpm.sock"*
* - "fastcgi_index index.php"*
* - "fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name"*
* - "include fastcgi_params"
*

Having a template that just builds an nginx config file from this
structure has basically no value: your config is clearly seen in the
structure; there is no need to go though some parsing & templating to
get ... almost the same output.

I suggest you do the opposite: write a generic nginx role that deploys a
healthy server. Then write your application role, make is depend on
nginx (and php-fpm), and deploy your specific nginx configuration FROM
your application role (with some variables in it if needed, like port,
root, log file location, etc...).

If you have multiple instance or the same application to deploy on the
same server, call it several tiles with different values.

It has a lot of advantages in my opinion:

- you will end up with much more manageable, maintanable and scalable
roles and playbooks
- things will be much clearer when chasing problems with variables
interpolation
- writing the exact same configuration as a Yaml structure basically has
no added value, and just make things more brittle than they should be
- you will have to juggle with complex structure using a very limited
(for valid reasons) templating language (Jinja)

Of course, there are cases when you could gain factorizing things, but
it doesn't seem to apply here (while possible, multiple PHP handlers in
the same vhost are pretty rare out there). And if they are very similar,
you should not embed config directive (e.g. fastcgi_pass) in the YAML
file, just the value (unix:/var/run/php5-fpm.sock).

Sure, there are many ways to work with Ansible, and yours is also one
way, and I don't have your insights on your specific problem. But I
tried similar things in the past, but ended with very complex and
brittle stuff.

Michael once told me "Ansible is not a programming langage... you are
too data driven". He was damn right and I changed my way of doing things
since. Let's face it, Ansible is great, but it can not magically turn
complex problems in YAML structures (at least not all of them).

Strive for simplicity (when you can), simple structures, divide and
conquer (avoid that "uber-playbook that manages all my infrastructure",
it won't work unless your infrastructure is ONE single project) and prosper.

Good luck (and sorry for not replying to the original question).

M