# Issue with loop in template

**URL:** <https://forum.ansible.com/t/issue-with-loop-in-template/14975>\
**Category:** Ansible Project\
**Created:** [April 9, 2014, 7:19am UTC](https://forum.ansible.com/t/issue-with-loop-in-template/14975 "2014-04-09T07:19:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pawel\_Peryga](https://avatars.discourse-cdn.com/v4/letter/p/ce73a5/32.png) [@Pawel\_Peryga](https://forum.ansible.com/u/Pawel_Peryga)\
**Post date:** [April 9, 2014, 7:19am UTC](https://forum.ansible.com/t/issue-with-loop-in-template/14975/1 "2014-04-09T07:19:55Z")

</div>

Hi,

I have some issues with for loop in jinja2 template. In hosts\_vars/name\_of\_host i have defined:

add\_php\_packages:

- php5-xcache  
apps:
- {  
…  
postgres: {  
db\_host: ‘xxx’,  
db\_user: ‘xxx’,  
db\_name: ‘some\_name’,  
db\_pass: ‘xxx’,  
db\_port: 5432  
},  
…  
}

Than task:

- name: Generate /etc/system.monitoring/config.cfg  
action: template src=etc/system.monitoring/config.cfg.j2 dest=/etc/system.monitoring/config.cfg owner=root group=root mode=0644

And in template:  
{% for db in apps[‘postgres’][‘db\_name’] %}  
check.pg\_locks\_{{ db }}.env.PGDATABASE={{ db }}  
{% endfor %}

This give me error:  
fatal: [uc4qaap01] =\> {‘msg’: “One or more undefined variables: ‘list object’ has no attribute ‘postgres’”, ‘failed’: True}  
fatal: [uc4qaap01] =\> {‘msg’: ‘One or more items failed.’, ‘failed’: True, ‘changed’: False, ‘results’: [{‘msg’: “One or more undefined variables: ‘list object’ has no attribute ‘postgres’”, ‘failed’: True}]}

When i use for example:  
{% for db in add\_php\_packages %}  
check.pg\_locks\_{{ db }}.env.PGDATABASE={{ db }}  
{% endfor %}

Everything is fine and i get this line:  
+check.pg\_locks\_php5-xcache.env.PGDATABASE=php5-xcache

What am i doing wrong?
