using a variable as a key to a dictionary

38 views
Skip to first unread message

Yves Dorfsman

unread,
Jul 31, 2014, 4:41:22 AM7/31/14
to ansible...@googlegroups.com

With jinja2, is it possible to use a variable as a key to a dictionary?
The following doesn't work:

var file:
db_names:
- prod
- dev
- qa

databases:
prod:
connection_string: prod_conn_string
qa:
connection_string: qa_conn_string
dev:
connection_string: dev_conn_string


playbook:
- name: test
debug: msg="database conn string: {{ databases.{{item}}.connection_string }}
with_items: db_names


--
Yves.

Yves Dorfsman

unread,
Jul 31, 2014, 5:23:39 AM7/31/14
to ansible...@googlegroups.com

Got it (from http://jinja.pocoo.org/docs/templates, right in "Variables"):
- name: test
debug: msg="database conn string: {{ databases[item].connection_string }}
with_items: db_names
Reply all
Reply to author
Forward
0 new messages