# for loop in jinja2

**URL:** <https://forum.ansible.com/t/for-loop-in-jinja2/26837>\
**Category:** Ansible Project\
**Created:** [February 23, 2018, 12:28pm UTC](https://forum.ansible.com/t/for-loop-in-jinja2/26837 "2018-02-23T12:28:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Fazeli](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@David\_Fazeli](https://forum.ansible.com/u/David_Fazeli)\
**Post date:** [February 23, 2018, 12:28pm UTC](https://forum.ansible.com/t/for-loop-in-jinja2/26837/1 "2018-02-23T12:28:32Z")

</div>

Please explain to me how can I fix this problem? i have this file `defaults/main.yml`

`

```auto
---
node1:
 ip: 1.1.1.1

node2:
 ip: 2.2.2.2

node3:
 ip: 3.3.3.3 

```

`

Now, I want in template file `ip.j2` by `for loop` access the IPs of each server and save in `address` `variable`

Like this:

`address= 1.1.1.1,2.2.2.2,3.3.3.3`

I tried this code:

`

```auto
address={% for x in {{nubmer_nodes}} %}
{{node[x].ip}}
{% if loop.last %},{% endif %}
{% endfor %}

```

`

But an error occurs. How should I do this?

**Error** :

`

```auto
TASK [Gathering Facts] *********************************************************************

ok: [db2]
ok: [db3]
ok: [db1]

TASK [ssh : -- my loop --] *************************************************************************

fatal: [db1]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: expected token ':', got '}'. String: \r\naddress={% for x in {{number_nodes}} %}\r\n{{node[x].ip}}\r\n{% if loop.last %},{% endif %}\r\n{% endfor %}"}
fatal: [db2]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: expected token ':', got '}'. String: \r\naddress={% for x in {{number_nodes}} %}\r\n{{node[x].ip}}\r\n{% if loop.last %},{% endif %}\r\n{% endfor %}"}
fatal: [db3]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: expected token ':', got '}'. String: \r\naddress={% for x in {{number_nodes}} %}\r\n{{node[x].ip}}\r\n{% if loop.last %},{% endif %}\r\n{% endfor %}"}
        to retry, use: --limit @/etc/ansible/playbooks/get_ip_ssh.retry

PLAY RECAP ********************************************************

db1 : ok=1 changed=0 unreachable=0 failed=1
db2 : ok=1 changed=0 unreachable=0 failed=1
db3 : ok=1 changed=0 unreachable=0 failed=1

```

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 23, 2018, 1:59pm UTC](https://forum.ansible.com/t/for-loop-in-jinja2/26837/2 "2018-02-23T13:59:07Z")

</div>

> Please explain to me how can I fix this problem? i have this file  
> defaults/main.yml
> 
> ---  
> node1:  
> ip: 1.1.1.1
> 
> node2:  
> ip: 2.2.2.2
> 
> node3:  
> ip: 3.3.3.3
> 
> Now, I want in template file ip.j2 by for loop access the IPs of each  
> server and save in address variable
> 
> Like this:
> 
> address= 1.1.1.1,2.2.2.2,3.3.3.3
> 
> I tried this code:
> 
> address={% for x in {{nubmer\_nodes}} %}

You are already in template mode so you can't use {{ }}  
address={% for x in nubmer\_nodes %}

You have not shown your nubmer\_nodes, but to make it work it need to be [1, 2, 3] for it to work.

> {{node.ip}}

This try to look up a likt named node, you need  
{{ vars['node' ~ x }}

> {% if loop.last %},{% endif %}

This will only print , at the end, you are missing a not.  
And to suppress the newline in the previous line you also need a - added

{%- if not loop.last %},{% endif %}

---

<div class="post-metadata">

**Author:** ![Nigel\_Metheringham](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nigel\_Metheringham](https://forum.ansible.com/u/Nigel_Metheringham)\
**Post date:** [February 23, 2018, 2:54pm UTC](https://forum.ansible.com/t/for-loop-in-jinja2/26837/3 "2018-02-23T14:54:57Z")

</div>

Personally I would have done this like this (but you may have reasons for arranging it differently)…

defaults/main.yml:-
