# nested variables - again

**URL:** <https://forum.ansible.com/t/nested-variables-again/20011>\
**Category:** Ansible Project\
**Created:** [December 15, 2015, 12:09am UTC](https://forum.ansible.com/t/nested-variables-again/20011 "2015-12-15T00:09:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [December 15, 2015, 12:09am UTC](https://forum.ansible.com/t/nested-variables-again/20011/1 "2015-12-15T00:09:55Z")

</div>

I see a lot of discussion about nested variables in Ansible, but I have not yet been able to find a solution to my problem. So yet again another question about nested variables (pease forgive me if this has been answered before, just point me at the previous discussions).

I want to do define a structure like the following:

STRUCTURE:  
list1:

- {var1: val1, var2, val2, var3, val3}

list2:

- {var1: val1, var2, val2, var3, val3}

list3:

- {var1: val1, var2, val2, var3, val3}

Then is a playbook I want to be able to use the value in a variable to select one of the lists:

vars:  
list: list1

- debug: msg=“var1={{item.var1}}, var2={{item.var2}}, var3={{item.var3}}”  
with\_(items,subelements,nested,dict,?)
- STRUCTURE
- list

Essentially, I need to have a list of dictionaries, I need to select one of those dictionaries with a key, and then I need to pull the values out of the selected dictionary. What is the best way to do this?  
Thanks,  
-Mark

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [December 15, 2015, 3:01pm UTC](https://forum.ansible.com/t/nested-variables-again/20011/2 "2015-12-15T15:01:03Z")

</div>

Will this do what you need? using `structure[list]` instead of trying to nest.

`

---

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [December 15, 2015, 4:53pm UTC](https://forum.ansible.com/t/nested-variables-again/20011/3 "2015-12-15T16:53:24Z")

</div>

That is close but not quite what I am looking for. I oversimplified my description of the problem. I should have said “I need to select one or more of those dictionaries with keys”. So ‘list’ is a list of one or more keys:

list:

- list1
- list2

And that is where this got complicated. The temptation is to do something like this:

- debug: msg=“var1={{STRUCTURE.{{item}}.val1}}, var2={{STRUCTURE.{{item}}.val2}}, var3={{STRUCTURE.{{item}}.val3}}”  
with\_items:
- “{{list}}”

Of course, I can’t have nested braces, but your example gave me the clue I needed to make this work. Following your lead I came up with this:

- debug: msg=“var1={{STRUCTURE[item].0.val1}}, var2={{STRUCTURE[item].0.val2}}, var3={{STRUCTURE[item].0.val3}}”  
with\_items:
- “{{list}}”

And that got me what I needed!  
Thanks for your help!  
-Mark
