# Jinja2 template with nested loops

**URL:** <https://forum.ansible.com/t/jinja2-template-with-nested-loops/45047>\
**Category:** Get Help\
**Tags:** jinja2\
**Created:** [January 3, 2026, 2:37pm UTC](https://forum.ansible.com/t/jinja2-template-with-nested-loops/45047 "2026-01-03T14:37:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Django](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@Django](https://forum.ansible.com/u/Django)\
**Post date:** [January 3, 2026, 2:37pm UTC](https://forum.ansible.com/t/jinja2-template-with-nested-loops/45047/1 "2026-01-03T14:37:23Z")

</div>

OK: what’s this about?

I have defined various hosts in my inventory. Among other things, the hosts in the IDMZ zone each have an array with a different number of alias definitions.

The host **vml010110** has the following array:

```auto
host_cnames:
  - name: "host_alias"
    cname: '{{ host_alias }}'
  - name: "Firewall C"
    cname: 'fwc'
  - name: "Time-Server"
    cname: 'time'
  - name: "DHCP-Server"
    cname: 'dhcp'
  - name: "DNS-Server"
    cname: 'dns'

```

The host **vml010200** , on the other hand, looks like this:

```auto
host_cnames:
  - name: "host_alias"
    cname: '{{ host_alias }}'
  - name: "Build-Maschine"
    cname: 'build'

```

In my jinja2 looks like:

```auto
{% for host in groups['idmz'] %}    
{{'{:31}'.format(host) }} has IPv4: {{ hostvars[host]['host_ipv4'] }}
                                has IPv6: {{ hostvars[host]['host_ipv6'] }}
{% for item in host_cnames %}       
{{ '%-31s' % (item.cname) }} belongs to: {{ host }}
{% endfor %}                        

{% endfor %} 

```

Unfortunately, this gives me the following result:

```auto
vml010110 has IPv4: 10.0.10.110
                                has IPv6: fd00::3:10:0:10:110
fwi-arch belongs to: vml010110
fwc belongs to: vml010110
time belongs to: vml010110
dhcp belongs to: vml010110
dns belongs to: vml010110
 
 
vml010200 has IPv4: 10.0.10.200
                                has IPv6 fd00::3:10:0:10:200
fwi-arch belongs to: vml010200
fwc belongs to: vml010200
time belongs to: vml010200
dhcp belongs to: vml010200
dns belongs to: vml010200

```

On the second host, **vml010200** , the array from host **vml010110** is therefore attracted. However, I would like to produce the following result:

```auto
vml010110 has IPv4: 10.0.10.110
                                has IPv6: fd00::3:10:0:10:110
fwi-arch belongs to: vml010110
fwc belongs to: vml010110
time belongs to: vml010110
dhcp belongs to: vml010110
dns belongs to: vml010110
 
 
vml010200 has IPv6: 10.0.10.200
                                has IPv4 fd00::3:10:0:10:200
build-arch belongs to: vml010200
build belongs to: vml010200

```

I’ve already spent hours on this, reading how2’s and trying to understand. One thing I already know is that I have to link my second “inner” for item loop with the first “outer” for item loop. But how? I’m sure it’s relatively easy, but my problem is probably stuck between my ears again. 🤔

---

<div class="post-metadata">

**Author:** ![Django](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@Django](https://forum.ansible.com/u/Django)\
**Post date:** [January 3, 2026, 5:09pm UTC](https://forum.ansible.com/t/jinja2-template-with-nested-loops/45047/2 "2026-01-03T17:09:00Z")

</div>

Yeahhhhhhhhhhhhhh … 😎

I fixed it!

```auto
{% for host in groups['idmz'] %}    
{{'{:31}'.format(host) }} has IPv4: {{ hostvars[host]['host_ipv4'] }}
                                has IPv6: {{ hostvars[host]['host_ipv6'] }}
{% for item in hostvars[host]['host_cnames'] %}       
{{ '%-31s' % (item.cname) }} belongs to: {{ host }}
{% endfor %}                        

{% endfor %} 

```

Now it works! 👍

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 3, 2026, 8:44pm UTC](https://forum.ansible.com/t/jinja2-template-with-nested-loops/45047/3 "2026-01-03T20:44:28Z")

</div>

I stared at your fix for a while before I spotted the difference.  
For the benefit of anybody else casually reading along…

The inner `for` loop before the fix was (with spaces added for easier comparison)  
`{% for item in host_cnames %}`  
The fix was to change this to  
`{% for item in hostvars[host]['host_cnames'] %}`  
That is, to loop over the `host_cnames` relevant to the outer loop’s `host` rather than to the current play host.

Even thinking it was probably something like that, it _still_ took me several passes to spot the difference.

Glad you got it working, @Django .

---

<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:** [February 2, 2026, 8:44pm UTC](https://forum.ansible.com/t/jinja2-template-with-nested-loops/45047/4 "2026-02-02T20:44:51Z")

</div>

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