Reputation: 114
I'm trying to figure out how I can reference a dictionary item in a variable based on another variable. For example:
{{ myVariable.someItem.someVariableItem.someOtherItem }}
Essentially, myVariable
is (obviously) a variable, but I want the item someVariableItem
to be set elsewhere. Is this possible?
Upvotes: 0
Views: 622
Reputation: 67959
Yes. It's possible.
"{{ myVariable.someItem[someVariableItem].someOtherItem }}"
For example the play below
- hosts: localhost
vars:
my_variable_item: item_z
my_dict:
item_x:
other_item_a: 1
item_y:
other_item_b: 2
item_z:
other_item_c: 3
tasks:
- debug:
msg: '{{ my_dict[my_variable_item].other_item_c }}'
gives
"msg": "3"
Upvotes: 1