# nested  with\_items

**URL:** <https://forum.ansible.com/t/nested-with-items/33204>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [June 17, 2020, 8:15pm UTC](https://forum.ansible.com/t/nested-with-items/33204 "2020-06-17T20:15:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_Britton](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nicholas\_Britton](https://forum.ansible.com/u/Nicholas_Britton)\
**Post date:** [June 17, 2020, 8:15pm UTC](https://forum.ansible.com/t/nested-with-items/33204/1 "2020-06-17T20:15:32Z")

</div>

I have a senerio where i think a nested with\_items would work , but i am not seeing any examples where there are multiple keys.

Today i have a with\_items that looks like this:

`

- name: Include vmware workflow for Tag Setups  
include\_tasks: tasks/vmworkflow\_assetgroup\_create\_tags\_include.yml  
with\_items:
- {segname: ‘bu1’, segabv: ‘1’, btype: ‘Image Backup’, ptype: ‘pi’, driveselect: ‘0’, ret: ‘30’, tagtype: ‘Image Only’, tagbg: ‘bu1’ }
- {segname: ‘bu2’, segabv: ‘2’, btype: ‘Image Backup’, ptype: ‘pi’, driveselect: ‘0’, ret: ‘30’, tagtype: ‘Image Only’, tagbg: ‘bu2’ }
- {segname: ‘bu3’, segabv: ‘3’, btype: ‘Image Backup’, ptype: ‘pi’, driveselect: ‘0’, ret: ‘30’, tagtype: ‘Image Only’, tagbg: ‘bu3’ }
- {segname: ‘bu1’, segabv: ‘1’, btype: ‘Image with DB Backup’, ptype: ‘pd’, driveselect: ‘2’, ret: ‘30’, tagtype: ‘DB Backup’, tagbg: ‘bu1’ }
- {segname: ‘bu2’, segabv: ‘2’, btype: ‘Image with DB Backup’, ptype: ‘pd’, driveselect: ‘2’, ret: ‘30’, tagtype: ‘DB Backup’, tagbg: ‘bu2’ }
- {segname: ‘bu3’, segabv: ‘3’, btype: ‘Image with DB Backup’, ptype: ‘pd’, driveselect: ‘2’, ret: ‘30’, tagtype: ‘DB Backup’, tagbg: ‘bu3’ }

`

But i need to repeat each of these with two more pecies of data and thought a nested would be better than created 7 more of each one listed above.

I need to device enviornment with prod or nonprod and time with 1800, 2200, 0200, 0400

so the included task would then have :

- {segname: ‘bu1’, segabv: ‘1’, btype: ‘Image Backup’, ptype: ‘pi’, driveselect: ‘0’, ret: ‘30’, tagtype: ‘Image Only’, tagbg: ‘bu1’ } prod 1800

- {segname: ‘bu1’, segabv: ‘1’, btype: ‘Image Backup’, ptype: ‘pi’, driveselect: ‘0’, ret: ‘30’, tagtype: ‘Image Only’, tagbg: ‘bu1’ } prod 2200

- {segname: ‘bu1’, segabv: ‘1’, btype: ‘Image Backup’, ptype: ‘pi’, driveselect: ‘0’, ret: ‘30’, tagtype: ‘Image Only’, tagbg: ‘bu1’ } prod 0200

- {segname: ‘bu1’, segabv: ‘1’, btype: ‘Image Backup’, ptype: ‘pi’, driveselect: ‘0’, ret: ‘30’, tagtype: ‘Image Only’, tagbg: ‘bu1’ } prod 0400

- {segname: ‘bu1’, segabv: ‘1’, btype: ‘Image Backup’, ptype: ‘pi’, driveselect: ‘0’, ret: ‘30’, tagtype: ‘Image Only’, tagbg: ‘bu1’ } nonprod 1800

- {segname: ‘bu1’, segabv: ‘1’, btype: ‘Image Backup’, ptype: ‘pi’, driveselect: ‘0’, ret: ‘30’, tagtype: ‘Image Only’, tagbg: ‘bu1’ } nonprod 2200

- {segname: ‘bu1’, segabv: ‘1’, btype: ‘Image Backup’, ptype: ‘pi’, driveselect: ‘0’, ret: ‘30’, tagtype: ‘Image Only’, tagbg: ‘bu1’ } nonprod 0200

- {segname: ‘bu1’, segabv: ‘1’, btype: ‘Image Backup’, ptype: ‘pi’, driveselect: ‘0’, ret: ‘30’, tagtype: ‘Image Only’, tagbg: ‘bu1’ } nonprod 0400

all as variables that i can call within the include. Currently i am calling the rest with item.segname, item.segabv, etc…

thoughts or am i stuck with just having a longer list of with items?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 18, 2020, 4:58am UTC](https://forum.ansible.com/t/nested-with-items/33204/2 "2020-06-18T04:58:25Z")

</div>

I usually handle this by looping over include\_tasks. So each level has its own file with tasks, and includes another task to ‘go down’ one level so to speak.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Britton](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nicholas\_Britton](https://forum.ansible.com/u/Nicholas_Britton)\
**Post date:** [June 18, 2020, 11:11am UTC](https://forum.ansible.com/t/nested-with-items/33204/3 "2020-06-18T11:11:10Z")

</div>

So with that option I would have two more include tasks prior where I set the the next to vars?

Any other options to keep it in one play?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [June 18, 2020, 11:22am UTC](https://forum.ansible.com/t/nested-with-items/33204/4 "2020-06-18T11:22:06Z")

</div>

> So with that option I would have two more include tasks prior where I set the the next to vars?
> 
> Any other options to keep it in one play?

You could also use the product filter  
([https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#product-filters](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#product-filters)):

Assign your current list to a variable, e.g asset\_list and combine it with lists for environment and time:

with\_items: "{{ asset\_list | product(['prod', 'nonprod']) | product(['1800', '2200', '0200', '0400']) }}"

This will run over all possible combinations of the items in these three lists.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Nicholas\_Britton](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nicholas\_Britton](https://forum.ansible.com/u/Nicholas_Britton)\
**Post date:** [June 18, 2020, 12:40pm UTC](https://forum.ansible.com/t/nested-with-items/33204/5 "2020-06-18T12:40:16Z")

</div>

How do I access that as a var? I am accessing each component as part of a uri call in the include tasks statement.
