How can I use Ansible nested variable?

Per Ansible FAQ:

Another rule is ‘moustaches don’t stack’. We often see this:

{{ somevar_{{other_var}} }} 

The above DOES NOT WORK, if you need to use a dynamic variable use the
hostvars or vars dictionary as appropriate:

{{ hostvars[inventory_hostname]['somevar_' + other_var] }}

So in your case:

- debug: msg={{hostvars[inventory_hostname][Component].community_release_num}}

Or:

- debug: msg={{vars[Component].community_release_num}}

Or (since Ansible 2.5):

- debug: msg={{(lookup('vars', Component)).community_release_num}}

Leave a Comment