# Help with Nested Loops

**URL:** <https://forum.ansible.com/t/help-with-nested-loops/23536>\
**Category:** Ansible Project\
**Created:** [January 23, 2017, 7:00pm UTC](https://forum.ansible.com/t/help-with-nested-loops/23536 "2017-01-23T19:00:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vnick](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vnick/32/7295_2.png) [@vnick](https://forum.ansible.com/u/vnick)\
**Post date:** [January 23, 2017, 7:00pm UTC](https://forum.ansible.com/t/help-with-nested-loops/23536/1 "2017-01-23T19:00:58Z")

</div>

Hey, everyone,  
I’m having figuring out how to use with\_items (or some variation thereof) in order to loop over a list of items within another list. What I’m trying to do is allow a playbook to accept a mountpoint variable/input, and then find the top-level disk device responsible for that. The trick here is that, when using LVM, you have to find not just the LV associated with the mountpoint, but also the SCSI device that holds that LV. For example, if I’m looking at mountpoint / on one of my Linux hosts, Ansible has the following facts about that mountpoint:

{  
“device”: “/dev/mapper/centos-root”,  
“fstype”: “xfs”,  
“mount”: “/”,  
“options”: “rw,seclabel,relatime,attr2,inode64,noquota”,  
“size\_available”: 9923756032,  
“size\_total”: 14879293440,  
“uuid”: “e871816f-a069-44a4-a563-cb80e9b733c2”  
},

This tells me the device (/dev/mapper/centos-root), but I want the parent device, and that info is contained in the ansbile\_devices fact:

“sda”: {  
“holders”: ,  
“host”: “SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)”,  
“model”: “Virtual disk”,  
“partitions”: {  
“sda1”: {  
“holders”: ,  
“sectors”: “1024000”,  
“sectorsize”: 512,  
“size”: “500.00 MB”,  
“start”: “2048”,  
“uuid”: “e3e17f87-fc28-46a4-9842-d8844c618946”  
},  
“sda2”: {  
“holders”: [  
“centos-root”,  
“centos-swap”  
],  
“sectors”: “32528384”,  
“sectorsize”: 512,  
“size”: “15.51 GB”,  
“start”: “1026048”,  
“uuid”: null  
}  
},

So, we see that the “holders” sub-element of the “sda2” partition, which is under the partitions element of “sda”, which is a device in ansible\_devices is what actually contains the LV that I’m after. Basically what I want to be able to do is identify that disk “sda” is what contains this volume given the mountpoint /. I can’t quite figure out how, in a playbook, to loop through in such a way that I’m looking at each of the entries in ansible\_devices (sda, sdb, sdc), then each of the holders entries there (empty in the above example), then each of the partitions (sda1, sda2, sda3, etc.), then each of the holders devices on those partitions, until I find one or more devices that contain the logical volume associated with the mount point?

Any ideas?

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 24, 2017, 11:52am UTC](https://forum.ansible.com/t/help-with-nested-loops/23536/2 "2017-01-24T11:52:49Z")

</div>

> I can't quite figure out how, in a playbook, to loop through in  
> such a way that I'm looking at each of the entries in  
> ansible\_devices (sda, sdb, sdc), then each of the holders entries  
> there (empty in the above example), then each of the partitions  
> (sda1, sda2, sda3, etc.), then each of the holders devices on those  
> partitions, until I find one or more devices that contain the  
> logical volume associated with the mount point?

This seems like too many levels of nesting.

> What I'm trying to do is allow a playbook to accept a mountpoint  
> variable/input, and then find the top-level disk device responsible  
> for that.

How about grepping the output of 'mount' or /etc/mtab or /etc/fstab?

Johannes
