# Finding unpartitioned drives

**URL:** <https://forum.ansible.com/t/finding-unpartitioned-drives/25427>\
**Category:** Ansible Project\
**Created:** [September 7, 2017, 6:14am UTC](https://forum.ansible.com/t/finding-unpartitioned-drives/25427 "2017-09-07T06:14:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KK1](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@KK1](https://forum.ansible.com/u/KK1)\
**Post date:** [September 7, 2017, 6:14am UTC](https://forum.ansible.com/t/finding-unpartitioned-drives/25427/1 "2017-09-07T06:14:57Z")

</div>

Hi

Am having trouble to find unpartitioned drives through ansible.  
I want all the unpartitioned drives put to a variable

example:

I have sda,sdb and sdc where sda is partitioned and sdb,sdc is left unpartitioned. I want the playbook to register both sub,sac to a variable

something like

“unpartitioned\_drives”: {

“stdout\_lines”: [  
“/dev/sdb”,  
“/dev/sdc”

Currently am doing this with shell which I want to do with ansible

- name: Finding Unpartiotioned drives  
shell: for i in /dev/sd[b-z];do /sbin/partprobe -d -s $i | grep -v 1 | cut -d’:’ -f1;done  
register: unpartitioned\_drives
- debug: var=unpartitioned\_drives

I tried doing this but all am getting is last drive but not both with this code.

- name: setfact  
set\_fact: unpartitioned\_drives=“{{ item.key }}”  
when: not item.value.partitions and item.key != “sr0” and item.key != “fd0”  
with\_dict: “{{ ansible\_devices }}”

- debug: var=unpartitioned\_drives

Output is

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [[dal-appblx118-16.prod.walmart.com](http://dal-appblx118-16.prod.walmart.com)] =\> {  
“changed”: false,  
“unpartitioned\_drives”: “sdc”  
}

Since the last item is sdc it only stores sdc. Can anyone help to use this to store all unpartitioned drives in a single variable.

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [September 7, 2017, 9:34am UTC](https://forum.ansible.com/t/finding-unpartitioned-drives/25427/2 "2017-09-07T09:34:20Z")

</div>

Hi,

Have a look here to append result after each iteration of a loop  
[https://stackoverflow.com/questions/35605603/using-ansible-set-fact-to-create-a-dictionary-from-register-results](https://stackoverflow.com/questions/35605603/using-ansible-set-fact-to-create-a-dictionary-from-register-results)

Be careful that your device can be unpartitioned, but can be used by lvm  
directly (without partition)... So you probably need to remove the  
results of the command pvs from your list

Regards,

JYL

---

<div class="post-metadata">

**Author:** ![KK1](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@KK1](https://forum.ansible.com/u/KK1)\
**Post date:** [September 7, 2017, 8:16pm UTC](https://forum.ansible.com/t/finding-unpartitioned-drives/25427/3 "2017-09-07T20:16:23Z")

</div>

Thanks JYL for the information. Is there any easy way to get hold of the drives rather than looping over the registered value.

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [September 7, 2017, 8:54pm UTC](https://forum.ansible.com/t/finding-unpartitioned-drives/25427/4 "2017-09-07T20:54:34Z")

</div>

ansible\_devices could interesting to look after…
