Loop dictionary in ansible template

I discovered today that using dict.values() loops over each dict element's values rather than its keys. So you should be able use something like this for your template.

{% for item in databases.values() %}
    <resource name="{{ item.db_resource }}" auth="container" type="javax.sql.datasource"  maxtotal="{{ item.db_maxconn }}" maxidle="{{ item.db_maxidle }}" maxwaitmillis="{{ item.db_maxwait }}" username="{{ item.db_user }}" password="{{ item.db_pass }}" driverclassname="{{ item.db_driver }}" url="{{ item.db_url }}" />
{% endfor %}

I know that it's way after the fact, but maybe somebody else searching for this answer can make use of this additional discovery.


In Jinja, when databases is a dictionary, for items in databases will (as in Python) iterate over the keys of the dictionary, not its key/value pairs. Thus, in your template, item.value (which I'm assuming is meant to be items.value) should be databases[items] in order to get the value associated with the key items.