ticktockhouse
ticktockhouse

Reputation: 599

Ansible - Rendering nested variables in a template

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

Answers (1)

ProfHase85
ProfHase85

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

Related Questions