# advanced jinja2 templating help - dynamic configuration

**URL:** <https://forum.ansible.com/t/advanced-jinja2-templating-help-dynamic-configuration/12307>\
**Category:** Ansible Project\
**Created:** [February 7, 2013, 8:39pm UTC](https://forum.ansible.com/t/advanced-jinja2-templating-help-dynamic-configuration/12307 "2013-02-07T20:39:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lester\_Wade](https://avatars.discourse-cdn.com/v4/letter/l/97f17d/32.png) [@Lester\_Wade](https://forum.ansible.com/u/Lester_Wade)\
**Post date:** [February 7, 2013, 8:39pm UTC](https://forum.ansible.com/t/advanced-jinja2-templating-help-dynamic-configuration/12307/1 "2013-02-07T20:39:23Z")

</div>

Hi all,

I’m starting to play with the Jinja2 templating, specifically generating a load balancer configuration based on public\_dns and public\_ip information returned by the ec2 module.

I’ve got the following haproxy.cfg.j2:

> {% set inst = ec2 %}  
> global  
> log 127.0.0.1 local2
> 
> chroot /var/lib/haproxy  
> pidfile /var/run/haproxy.pid  
> maxconn 4000  
> user haproxy  
> group haproxy  
> daemon
> 
> stats socket /var/lib/haproxy/stats
> 
> defaults  
> mode http  
> timeout connect 5000ms  
> timeout client 50000ms  
> timeout server 50000ms
> 
> #---------------------------------------------------------------------
> 
> # main frontend which proxys to the backends
> 
> #---------------------------------------------------------------------
> 
> frontend main \*:8888  
> default\_backend console
> 
> #---------------------------------------------------------------------
> 
> # round robin balancing between the various backends
> 
> #---------------------------------------------------------------------
> 
> backend console  
> {% for instances in inst %}  
> server {{ public\_dns }} {{ public\_ip }}:8888 check  
> {% endfor %}
> 
> # server console1 {{ inst.instances[0].public\_ip }}:8888 check
> 
> # server console2 {{ inst.instances[1].public\_ip }}:8888 check
> 
> # server console3 {{ inst.instances[2].public\_ip }}:8888 check

I can pick out the items from the instances return by those last 3 lines commented above. However, I’d like this to be dynamic based on the number of instances as a user spins up. Unfortunately this doesn’t work:

> {% for instances in inst %}  
> server {{ public\_dns }} {{ public\_ip }}:8888 check  
> {% endfor %}

Am I missing something obvious here? Should it be instances.public\_dns and instances.public\_ip ?

I’m starting to familiarise myself with Jinja2 template designer but I’m in the early stages 😉

Thanks for continued help 🙂

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [February 7, 2013, 9:26pm UTC](https://forum.ansible.com/t/advanced-jinja2-templating-help-dynamic-configuration/12307/2 "2013-02-07T21:26:01Z")

</div>

A small jinja trick: {{ inst|pprint}} will show you data structure.  
That should help narrow down the issue if it is a incorrect key or  
scope.

---

<div class="post-metadata">

**Author:** ![Tin\_Tvrtkovic](https://avatars.discourse-cdn.com/v4/letter/t/bc8723/32.png) [@Tin\_Tvrtkovic](https://forum.ansible.com/u/Tin_Tvrtkovic)\
**Post date:** [February 7, 2013, 11:41pm UTC](https://forum.ansible.com/t/advanced-jinja2-templating-help-dynamic-configuration/12307/3 "2013-02-07T23:41:19Z")

</div>

{% for instance in inst.instances %}  
server {{ instance.public\_dns }} {{ instance.public\_ip }}:8888 check  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![Lester\_Wade](https://avatars.discourse-cdn.com/v4/letter/l/97f17d/32.png) [@Lester\_Wade](https://forum.ansible.com/u/Lester_Wade)\
**Post date:** [February 8, 2013, 11:09am UTC](https://forum.ansible.com/t/advanced-jinja2-templating-help-dynamic-configuration/12307/4 "2013-02-08T11:09:29Z")

</div>

Thanks folks.

Neat about pprint, that is VERY handy indeed for debugging.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 8, 2013, 12:12pm UTC](https://forum.ansible.com/t/advanced-jinja2-templating-help-dynamic-configuration/12307/5 "2013-02-08T12:12:11Z")

</div>

I didn't know about pprint myself.

You also have {{ foo|to\_json }} and {{ foo|to\_yaml }}

Developer tech kind of sidenote -- These are made available by "filter  
plugins" in Ansible if someone ever wants to add more... but that's a  
niche thing and not many people will need to do that.
