Issue with loop in template

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?