# Filtering unpartitioned devices on a host

**URL:** <https://forum.ansible.com/t/filtering-unpartitioned-devices-on-a-host/28089>\
**Category:** Ansible Project\
**Tags:** azure\
**Created:** [July 19, 2018, 7:56am UTC](https://forum.ansible.com/t/filtering-unpartitioned-devices-on-a-host/28089 "2018-07-19T07:56:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vid.jelen1](https://avatars.discourse-cdn.com/v4/letter/v/6f9a4e/32.png) [@vid.jelen1](https://forum.ansible.com/u/vid.jelen1)\
**Post date:** [July 19, 2018, 7:56am UTC](https://forum.ansible.com/t/filtering-unpartitioned-devices-on-a-host/28089/1 "2018-07-19T07:56:35Z")

</div>

Hi,

I’d like to automatically check all available disk devices if they are partitioned and filter out the ones that aren’t. Later I’d also like to use that list to decide which ones to partition.

I’m trying to achieve this (currently only for **/dev/sdb** ) by using:

- name: Print out unmounted devices  
debug: msg=“{{ hostvars[inventory\_hostname].ansible\_devices.sdb | rejectattr(‘partitions’, ‘equalto’, true) | list }}”

This however returns me an error:

fatal: [[host1.com](http://host1.com)]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘unicode object’ has no attribute ‘partitions’\n\nThe error appears to have been in ‘~/ansible/roles/devices/tasks/main.yml’: line 72, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Print out unmounted devices\n ^ here\n”}

fatal: [[host2.com](http://host2.com)]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘unicode object’ has no attribute ‘partitions’\n\nThe error appears to have been in ‘~/ansible/roles/devices/tasks/main.yml’: line 72, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Print out unmounted devices\n ^ here\n”}  
fatal: [[host3.com](http://host3.com)]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘unicode object’ has no attribute ‘partitions’\n\nThe error appears to have been in ‘~/ansible/roles/devices/tasks/main.yml’: line 72, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Print out unmounted devices\n ^ here\n”}

Leaving the “| list” out at the end, it returns me a proper generator object, that I would expect from filtering…

ok: [[ncfressnapf-p-jira001.westeurope.cloudapp.azure.com](http://ncfressnapf-p-jira001.westeurope.cloudapp.azure.com)] =\> {  
“msg”: “\<generator object select\_or\_reject at 0x7f4673ac0960\>”  
}  
ok: [[ncfressnapf-p-logjira001.westeurope.cloudapp.azure.com](http://ncfressnapf-p-logjira001.westeurope.cloudapp.azure.com)] =\> {  
“msg”: “\<generator object select\_or\_reject at 0x7f4673ac0b90\>”  
}  
ok: [[ncfressnapf-p-confluence001.westeurope.cloudapp.azure.com](http://ncfressnapf-p-confluence001.westeurope.cloudapp.azure.com)] =\> {  
“msg”: “\<generator object select\_or\_reject at 0x7f4673ac0e60\>”  
}

Q1: Why is that error coming up? Is there a different way I would need to perform this filtering? I used a similar approach for filtering hosts missing ssh keys in the past and it worked well 😕

This snippet however correctly prints out all the information I am looking for.

- name: Print out unmounted devices  
debug: msg=“{{ hostvars[inventory\_hostname].ansible\_devices.sdb.partitions }}”

Q2: As an additional question… Is there a way to use a wildcard search or regex to search over all devices? Something in the lines of:

- name: Print out unmounted devices  
debug: msg=“{{ hostvars[inventory\_hostname].ansible\_devices.sd\*.partitions }}”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 20, 2018, 8:00am UTC](https://forum.ansible.com/t/filtering-unpartitioned-devices-on-a-host/28089/2 "2018-07-20T08:00:30Z")

</div>

> I'd like to automatically check all available disk devices if they are  
> partitioned and filter out the ones that aren't. Later I'd also like to use  
> that list to decide which ones to partition.
> 
> I'm trying to achieve this (currently only for \*/dev/sdb\*) by using:
> 
> - name: Print out unmounted devices  
> &nbsp;&nbsp;debug: msg="{{ hostvars[inventory\_hostname].ansible\_devices.sdb |  
> rejectattr('partitions', 'equalto', true) | list }}"

To me this seams a little backwards.  
The parted module will only partition a disk if it doesn't have any partitions.

- parted:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;device: /dev/sdb  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number: 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present

So if the sdb has a partition it won't do anything, but if it hasn't it will partition the device.

> Q2: As an additional question... Is there a way to use a wildcard search or  
> regex to search over all devices? Something in the lines of:
> 
> - name: Print out unmounted devices  
> &nbsp;&nbsp;debug: msg="{{  
> hostvars[inventory\_hostname].ansible\_devices.sd\*.partitions }}"

You could use with\_dict:
