# Loop over facts in playbook

**URL:** <https://forum.ansible.com/t/loop-over-facts-in-playbook/17826>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [March 24, 2015, 10:23am UTC](https://forum.ansible.com/t/loop-over-facts-in-playbook/17826 "2015-03-24T10:23:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [March 24, 2015, 10:23am UTC](https://forum.ansible.com/t/loop-over-facts-in-playbook/17826/1 "2015-03-24T10:23:41Z")

</div>

Hi

I have some troubles to understand how to loop over facts in playbook. Suppose I have the following:

`ansible node01 -m setup -a 'filter=ansible_devices' SSH password: SU password[defaults to SSH password]: node01 | success >> { "ansible_facts": { "ansible_devices": { "sda": { "holders": [], "host": "Serial Attached SCSI controller: VMware PVSCSI SCSI Controller (rev 02)", "model": "Virtual disk", "partitions": { "sda1": { "sectors": "204800", "sectorsize": 512, "size": "100.00 MB", "start": "2048" }, "sda2": { "sectors": "104650752", "sectorsize": 512, "size": "49.90 GB", "start": "206848" } }, "removable": "0", "rotational": "1", "scheduler_mode": "noop", "sectors": "104857600", "sectorsize": "512", "size": "50.00 GB", "support_discard": "0", "vendor": "VMware" }, "sdb": { "holders": [], "host": "Serial Attached SCSI controller: VMware PVSCSI SCSI Controller (rev 02)", "model": "Virtual disk", "partitions": { "sdb1": { "sectors": "10469376", "sectorsize": 512, "size": "4.99 GB", "start": "16384" } }, "removable": "0", "rotational": "1", "scheduler_mode": "noop", "sectors": "10485760", "sectorsize": "512", "size": "5.00 GB", "support_discard": "0", "vendor": "VMware" }, "sdc": { "holders": [], "host": "Serial Attached SCSI controller: VMware PVSCSI SCSI Controller (rev 02)", "model": "Virtual disk", "partitions": {}, "removable": "0", "rotational": "1", "scheduler_mode": "noop", "sectors": "14680064", "sectorsize": "512", "size": "7.00 GB", "support_discard": "0", "vendor": "VMware" } } }, "changed": false }`

So there are 3 disks: sda, sdb and sdc. Two of them has partitions, one does not. So I want to loop over all disks and check which disk have partitions and which does not have and then do something about it. I know I can do something like this:

`{{ ansible_devices['sda']['partitions'] }}`

But when I will run this playbook I will not know neither disk labels nor number of disks.

Any ideas?

Thanks  
Edgars

---

<div class="post-metadata">

**Author:** ![Jonathan\_Davila](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@Jonathan\_Davila](https://forum.ansible.com/u/Jonathan_Davila)\
**Post date:** [March 24, 2015, 1:37pm UTC](https://forum.ansible.com/t/loop-over-facts-in-playbook/17826/2 "2015-03-24T13:37:34Z")

</div>

Hi Edgars,

You want to use ansible’s with\_dict method of looping. Here is an example of looping through that dict and then a sample conditional for it as well  
`

# This is the sample with\_dict loop

- debug: msg=“The device {{ item.key }} with these partitions {{ item.value.partitions }}”  
with\_dict: ansible\_devices

# This msg will only be printed on the condition that a device has no partitions

- debug: msg=“The device {{ item.key }} has no partitions”  
with\_dict: ansible\_devices  
when: not item.value.partitions  
`

Hopefully that helps you out.

---

<div class="post-metadata">

**Author:** ![Edgars](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Edgars](https://forum.ansible.com/u/Edgars)\
**Post date:** [March 24, 2015, 3:23pm UTC](https://forum.ansible.com/t/loop-over-facts-in-playbook/17826/3 "2015-03-24T15:23:31Z")

</div>

Great!! Thanks a lot!

Edgars

otrdiena, 2015. gada 24. marts 14:37:34 UTC+1, Jonathan Davila rakstīja:

---

<div class="post-metadata">

**Author:** ![DS1](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@DS1](https://forum.ansible.com/u/DS1)\
**Post date:** [November 14, 2015, 10:30pm UTC](https://forum.ansible.com/t/loop-over-facts-in-playbook/17826/4 "2015-11-14T22:30:17Z")

</div>

Hey Guys,

I have the exact same issue where I need to find the drive that does not have any partitions on it.

so I have the exact same output from ansible\_devices but I can’t figure out how I load the output into ansible so I can run the filter on it

basically i need an output like /dev/sdb or /dev/sdb or whatever the actual device name is that does not have partitions on it so I can then use that value in a subsequent - command: /usr/sbin//fdisk -l for example - command: /usr/sbin/fdisk -l /dev/sdb

any help will be much appreciated

Thanks

---

<div class="post-metadata">

**Author:** ![Jonathan\_Davila](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@Jonathan\_Davila](https://forum.ansible.com/u/Jonathan_Davila)\
**Post date:** [November 16, 2015, 3:34pm UTC](https://forum.ansible.com/t/loop-over-facts-in-playbook/17826/5 "2015-11-16T15:34:32Z")

</div>

DS,

Try this.

This simple example will print the device name whenever the device has no partitions

`

- name: Loop facts  
hosts: localhost  
connection: local  
tasks:
- name: looping  
debug:  
msg: “{{ item.key }}”  
when: “not {{ item.value.partitions }}”  
with\_dict: ansible\_devices

`

---

<div class="post-metadata">

**Author:** ![DS1](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@DS1](https://forum.ansible.com/u/DS1)\
**Post date:** [November 16, 2015, 4:40pm UTC](https://forum.ansible.com/t/loop-over-facts-in-playbook/17826/6 "2015-11-16T16:40:29Z")

</div>

Hey Jonathan,

Thanks for this.

I’ve figured it out using the previous examples and some more googleing 🙂

I got it to execute a shell command which i what I needed it to do:

- shell: pvcreate /dev/“{{ item.key }}”  
with\_dict: ansible\_devices  
when: not item.value.partitions and item.key != “sr0” and item.key != “fd0”

Thanks again
