For Example:
I Have Variable {{ ami_redhat_7_2 }} That I want to use
vars:
OsType: redhat
OsVersion: '7_2'
tasks:
- debug: 'msg="{{ ami_{{OsType}}_{{ OsVersion }} }}"'
I got Error:
fatal: [localhost]: FAILED! => {
"failed": true,
"msg": "template error while templating string: expected token 'end of print statement', got '{'. String: {{ ami_{{ OsType }}_{{ OsVersion }} }}"
}
'root' variables with dynamic names is a tricky thing in Ansible.
If they are host facts, you can access them like this:
If they are play-bound variables, you can access them via undocumented
vars
object:But they will never get templated, because
vars
is treated in a special way.The easiest way for you is some dict with predefined name and dynamic key names, like:
And to access it, you can use:
P.S. and remove quotes around
msg
as suggested in other answer.