I’m trying to use a variable in a variable.
I have one JSON variable :
os: {
"centos_7_5": {
offer: "CentOS",
publisher: "OpenLogic",
sku: "7.5",
version: "latest"
},
"debian_9": {
offer: "Debian",
publisher: "credativ",
sku: "9",
version: "latest"
}
}
If I use
- debug:
msg: " {{ os.debian_9.offer }}"
The output is as desired :
"msg": " Debian"
Now, I’m trying to put the OS name in a variable (so that the variable can be in a config file) as follows :
desired_os: debian_9
I would like to do something like this :
- debug:
msg: " {{ os.desired_os.offer }}"
But I can’t find a way to make it work.
I tried some concatenation in a set_fact using '{{ "os."~desired_os~".offer" }}'
but the output is not as desired :
"msg": "stuff.os.debian_9.offer"
Thanks.
4
Answers
Hi try use this snippet:
json
playbook:
You can use the
varname[var]
notation.Simply add the variable within double brackets.
Please try as below