Ashwin Narayanan
Ashwin Narayanan

Reputation: 123

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)

Upvotes: 11

Views: 25340

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68329

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

Upvotes: 30

Related Questions