# Need basic help with jinja and ansible inventory

**URL:** <https://forum.ansible.com/t/need-basic-help-with-jinja-and-ansible-inventory/41239>\
**Category:** Get Help\
**Tags:** jinja2\
**Created:** [March 23, 2025, 11:12pm UTC](https://forum.ansible.com/t/need-basic-help-with-jinja-and-ansible-inventory/41239 "2025-03-23T23:12:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wes](https://avatars.discourse-cdn.com/v4/letter/w/e9c0ed/32.png) [@wes](https://forum.ansible.com/u/wes)\
**Post date:** [March 23, 2025, 11:12pm UTC](https://forum.ansible.com/t/need-basic-help-with-jinja-and-ansible-inventory/41239/1 "2025-03-23T23:12:57Z")

</div>

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:

```auto
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:

```auto
{% 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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![mkrizek](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mkrizek/32/2055_2.png) [@mkrizek](https://forum.ansible.com/u/mkrizek)\
**Post date:** [March 24, 2025, 9:48am UTC](https://forum.ansible.com/t/need-basic-help-with-jinja-and-ansible-inventory/41239/2 "2025-03-24T09:48:44Z")

</div>

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

```auto
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](https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html).

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [March 25, 2025, 4:29pm UTC](https://forum.ansible.com/t/need-basic-help-with-jinja-and-ansible-inventory/41239/3 "2025-03-25T16:29:56Z")

</div>

> [@wes](#):
>
> ```auto
> localhost ansible_connection=local
> 
> ```

#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)

> **[Implicit ‘localhost’ — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/inventory/implicit_localhost.html)**

---

<div class="post-metadata">

**Author:** ![wes](https://avatars.discourse-cdn.com/v4/letter/w/e9c0ed/32.png) [@wes](https://forum.ansible.com/u/wes)\
**Post date:** [March 26, 2025, 12:57pm UTC](https://forum.ansible.com/t/need-basic-help-with-jinja-and-ansible-inventory/41239/4 "2025-03-26T12:57:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![iwt-cmd](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/iwt-cmd/32/9120_2.png) [@iwt-cmd](https://forum.ansible.com/u/iwt-cmd)\
**Post date:** [March 26, 2025, 4:25pm UTC](https://forum.ansible.com/t/need-basic-help-with-jinja-and-ansible-inventory/41239/5 "2025-03-26T16:25:24Z")

</div>

@bcoca you need to have bot that auto-replies with this when any of us include ansible\_connection=local (speaking as a previous offender 🙂 )

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [March 26, 2025, 4:28pm UTC](https://forum.ansible.com/t/need-basic-help-with-jinja-and-ansible-inventory/41239/6 "2025-03-26T16:28:14Z")

</div>

how do you know I’m not a bot?

---

<div class="post-metadata">

**Author:** ![iwt-cmd](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/iwt-cmd/32/9120_2.png) [@iwt-cmd](https://forum.ansible.com/u/iwt-cmd)\
**Post date:** [March 26, 2025, 4:36pm UTC](https://forum.ansible.com/t/need-basic-help-with-jinja-and-ansible-inventory/41239/7 "2025-03-26T16:36:29Z")

</div>

![Homer Hiding GIFs - Find & Share on GIPHY](https://us1.discourse-cdn.com/ansible/original/2X/2/2079f240063337ca3193d13c10c8f049d94b34da.gif)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 25, 2025, 4:37pm UTC](https://forum.ansible.com/t/need-basic-help-with-jinja-and-ansible-inventory/41239/8 "2025-04-25T16:37:03Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
