Need basic help with jinja and ansible inventory

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

Your inventory is not defined in the correct format. The hosts must be YAML dictionary keys:

site1:                                                                                                    
  vars:                                                                                                                                                                                                             
    listenport: 2220                                                                                      
  hosts:                                                                                                  
    localhost:                                                                                            
      ansible_connection: local                                                                           
    192.168.2.22:                                                                                         
                                                                                                          
site2:                                                                                                    
  vars:                                                                                                   
    listenport: 2221                                                                                      
  hosts:                                                                                                  
    192.168.115:                                                                                          
    192.168.224:

See How to build your inventory — Ansible Community Documentation.

2 Likes

#1 there is no real need to add localhost to inventory 99% of the time
#2 IF you really need to add it, you need to do it correctly (connection is not enough)

2 Likes

Thanks! @bcoca and @mkrizek . All good now. Fixed the yaml.

@bcoca you need to have bot that auto-replies with this when any of us include ansible_connection=local (speaking as a previous offender :slight_smile: )

1 Like

how do you know I’m not a bot?

2 Likes

Homer Hiding GIFs - Find & Share on GIPHY