Ashwin Narayanan Ashwin Narayanan - 1 month ago 11
YAML Question

Accessing a dictionary using another variable as key YAML

My requirements are these: a deployment environment is passed into the playbook as extra vars, for ex : dev, qa or prod

I have a variable called DEPLOY_URL

Based on the value of the env variable, the DEPLOY_URL has to change.

I tried doing the following :

DEPLOY_URLS:
"dev": "xyz"
"prod" : "abc"
"qa" : "123"

DEPLOY_URL: "{{DEPLOY_URLS['{{DEPLOY_ENV}}']}}"


The value is never correct.
Is there a way to access a dictionary using another variable as key?
(Using YAML and ansible)

Answer

Try this: DEPLOY_URL: "{{ DEPLOY_URLS[DEPLOY_ENV] }}"

Comments