Reputation: 599
I'm trying to render some nested variables to a file using templating in ansible.
The governing playbook looks like:
---
- hosts: tag_Cluster_restore
vars:
pg_password:
qa: blah
staging: blahblah
production: blahblahblah
roles:
- role: psql_helper
For simplicity, my role's tasks/main.yml looks like this:
---
- debug: msg="Password is {{ pg_password.[env] }}"
And I would like to specify the env
variable on the command line like this:
ansible-playbook playbook.yml -e "env=qa"
...and have it render the qa password. However, at the moment I get fatal: [1.1.1.1]: FAILED! => {"failed": true, "msg": "ERROR! template error while templating string: expected name or number"}
Obviously I have the syntax wrong somewhere, Or I am specifying something incorrectly. Can anyone help?
Note that I do not want to loop over the pg_password
variable - most stuff I've googled points out how to do this, I would like to "drill down" to the nested variable that I need, if this is in fact possible...
Thanks
Upvotes: 1
Views: 7654
Reputation: 12183
Your debug task should not have the .
on invocation:
- debug: msg="Password is {{ pg_password[env] }}"
This simply means you are getting the key env
from the dictionary pg_password
Upvotes: 4