Hi, I’m trying to create a configuration for haproxy, that renders a haproxy.cfg.j2 jinja template, and populates it based on the ansible inventory. I’m having issues with jinja and iterating/loops.
My Inventory looks like this:
site1:
vars:
listenport: 2220
hosts:
localhost ansible_connection=local
192.168.2.22
site2:
vars:
listenport: 2221
hosts:
192.168.115
192.168.224
And on the haproxy template I have this to create the frontends and backends:
{% for group in groups %}
{% if not group in ['all','ungrouped'] %}
frontend {{ group }}
mode log
bind :{{ hostvars[groups[group][0]].listenport }}
default_backend {{ group }}
{% endif %}
{% endfor %}
{% for group in groups %}
{% if not group in ['all','ungrouped'] %}
backend {{ group }}
mode log
balance roundrobin
{% for host in groups[group] %}
server {{ host }}
{% endfor %}
{% endif %}
{% endfor %}
The second loop I need it to list each host, with “server” word in front on a separate line, but it puts them inline like this:
frontend site1
mode log
bind :2220
default_backend site1
frontend site2
mode log
bind :2221
default_backend site2
backend site1
mode log
balance roundrobin
server localhost ansible_connection=local 192.168.2.22
backend site2
mode log
balance roundrobin
server 192.168.115 192.168.224