# Confusion about nested loop in jinja2 template

**URL:** <https://forum.ansible.com/t/confusion-about-nested-loop-in-jinja2-template/21420>\
**Category:** Ansible Project\
**Created:** [May 12, 2016, 12:53pm UTC](https://forum.ansible.com/t/confusion-about-nested-loop-in-jinja2-template/21420 "2016-05-12T12:53:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joseph\_DJOMEDA](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Joseph\_DJOMEDA](https://forum.ansible.com/u/Joseph_DJOMEDA)\
**Post date:** [May 12, 2016, 12:53pm UTC](https://forum.ansible.com/t/confusion-about-nested-loop-in-jinja2-template/21420/1 "2016-05-12T12:53:50Z")

</div>

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](http://pastebin.com/eFKvL58G)

Thanks for helping

Best Regards,

---

<div class="post-metadata">

**Author:** ![Michel\_Blanc1](https://avatars.discourse-cdn.com/v4/letter/m/fbc32d/32.png) [@Michel\_Blanc1](https://forum.ansible.com/u/Michel_Blanc1)\
**Post date:** [May 12, 2016, 5:34pm UTC](https://forum.ansible.com/t/confusion-about-nested-loop-in-jinja2-template/21420/2 "2016-05-12T17:34:24Z")

</div>

> 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
