# How to do something with nested variables...

**URL:** <https://forum.ansible.com/t/how-to-do-something-with-nested-variables/19549>\
**Category:** Ansible Project\
**Created:** [October 19, 2015, 6:33pm UTC](https://forum.ansible.com/t/how-to-do-something-with-nested-variables/19549 "2015-10-19T18:33:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [October 19, 2015, 6:33pm UTC](https://forum.ansible.com/t/how-to-do-something-with-nested-variables/19549/1 "2015-10-19T18:33:28Z")

</div>

Hi AnsiPeeps,

I have a nested structure in vars as so:

data\_bricks:  
ovirt-node-01:

- gluster\_brick2
- gluster\_brick4  
ovirt-node-02:
- gluster\_brick1
- gluster\_brick3  
ovirt-node-03:
- gluster\_brick2
- gluster\_brick4

(the number of items in the 2nd-level node now is two, but may be higher if necessary…)

So in the associated playbook, I need to do something akin to:

“mkdir -p /{{ item }}/data\_brick”

For each item under the second-level item, which happens to be the {{ ansible\_hostname }} of the server that I need the commands to be run on - so for instance on “ovirt-node-01” I’d need to run the commands:  
“mkdir -p /gluster\_brick2/data\_brick”

“mkdir -p /gluster\_brick4/data\_brick”

In the playbook I want to use the above in, I have written a test debug task as so:

- name: Figure out how to use nested vars  
debug: msg=“brick name is {{ item }}”  
with\_subelements:
- engine\_bricks.{{ ansible\_hostname }}  
tags: tryme

Which must be horribly wrong, because when I run it against the group of servers having the server names on the second level of the nested vars, it blows up with this error:

TASK: [ist-ovirt-gluster | Figure out how to use nested vars] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [ovirt-node-02] =\> subelements lookup expects a list of two items, first a dict or a list, and second a string  
fatal: [ovirt-node-01] =\> subelements lookup expects a list of two items, first a dict or a list, and second a string  
fatal: [ovirt-node-03] =\> subelements lookup expects a list of two items, first a dict or a list, and second a string

I’ve read over [http://docs.ansible.com/ansible/playbooks\_loops.html](http://docs.ansible.com/ansible/playbooks_loops.html) a few times, thought i’d want “with\_subelements”, but maybe not? Can anyone give me a pointer how to achieve what I’m trying to do?

TIA,  
Will

---

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [October 20, 2015, 3:09am UTC](https://forum.ansible.com/t/how-to-do-something-with-nested-variables/19549/2 "2015-10-20T03:09:05Z")

</div>

Never mind all this, thought about it some more and will handle it with a two-level structure (i.e. a list var) in host\_vars
