# How to mount partition when NTFS only

**URL:** <https://forum.ansible.com/t/how-to-mount-partition-when-ntfs-only/25079>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [July 25, 2017, 12:42pm UTC](https://forum.ansible.com/t/how-to-mount-partition-when-ntfs-only/25079 "2017-07-25T12:42:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [July 25, 2017, 12:42pm UTC](https://forum.ansible.com/t/how-to-mount-partition-when-ntfs-only/25079/1 "2017-07-25T12:42:25Z")

</div>

Hello,

I have a playbook that scans for hard drives and mounts them in order to do a backup of the data from those drives. I am able to scan for the drives just fine and I can also mount them (thanks to the Ansible IRC community). The only issue I am having is mounting ONLY when the partition is **fstype: ntfs**. As you can see below, when I run the **parted** module, I can see a key value of **fstype: ntfs**.

`

ok: [localhost] =\> (item=sda) =\> {  
“changed”: false,  
“disk”: {  
“dev”: “/dev/sda”,  
“logical\_block”: 512,  
“model”: “VMware Virtual disk”,  
“physical\_block”: 512,  
“size”: 33554432.0,  
“table”: “msdos”,  
“unit”: “kib”  
},  
“invocation”: {  
“module\_args”: {  
“align”: “optimal”,  
“device”: “/dev/sda”,  
“flags”: null,  
“label”: null,  
“name”: null,  
“number”: null,  
“part\_end”: “100%”,  
“part\_start”: “0%”,  
“part\_type”: “primary”,  
“state”: “info”,  
“unit”: “KiB”  
}  
},  
“item”: “sda”,  
“partitions”: [  
{  
“begin”: 1024.0,  
“end”: 308224.0,  
“flags”: [  
“boot”  
],  
“fstype”: “ntfs”,  
“num”: 1,  
“size”: 307200.0,  
“unit”: “kib”  
},  
{  
“begin”: 308224.0,  
“end”: 33553408.0,  
“flags”: ,  
“fstype”: “ntfs”,  
“num”: 2,  
“size”: 33245184.0,  
“unit”: “kib”  
}  
],  
“script”: “unit ‘KiB’ print”  
}

`

I use the setup module to scan for the devices and the parted module to see the fstype. Thinking about it further, I should probably just use the parted module to get me both since parted does provide both.

Using **parted** alone without **setup** , how can I tell my mount task to just mount ONLY if the fstype is ntfs? Here is my current playbook:

`

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [July 27, 2017, 4:10pm UTC](https://forum.ansible.com/t/how-to-mount-partition-when-ntfs-only/25079/2 "2017-07-27T16:10:19Z")

</div>

Any suggestions? 🙂

---

<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 27, 2017, 4:59pm UTC](https://forum.ansible.com/t/how-to-mount-partition-when-ntfs-only/25079/3 "2017-07-27T16:59:46Z")

</div>

You should check your with\_items: under "Check for hard drive devices" it's in practice empty.

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [July 31, 2017, 11:26pm UTC](https://forum.ansible.com/t/how-to-mount-partition-when-ntfs-only/25079/4 "2017-07-31T23:26:52Z")

</div>

Kai,

Thanks for the response. Can you elaborate on what you by “it’s in practice empty”? From what I can tell, I am parted to then pull the key values for a list of drives. This doesn’t fully work based on what I am trying to do. In essence, I am looking for help to accomplish what I want and that would be filtering through partitions that are NTFS only. An example, if you have one, would be great. I am still lost on how to only list out and mount partitions that are NTFS only

---

<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:** [August 1, 2017, 12:09am UTC](https://forum.ansible.com/t/how-to-mount-partition-when-ntfs-only/25079/5 "2017-08-01T00:09:00Z")

</div>

json\_query is a filter, for a filter to work it need content.  
When you only use a filter and not any content it's basically empty.

&nbsp;&nbsp;&nbsp;- name: Check for hard drive devices  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parted: device=/dev/{{ item }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: driveFStype  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ json\_query('\*.partitions.keys(@)') }}"

Here you only have a filter in with\_items, no content at all.

- name:  
&nbsp;&nbsp;&nbsp;mount:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "/mnt/{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "/dev/{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fstype: ntfs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: mounted  
&nbsp;&nbsp;&nbsp;with\_items: "{{ ansible\_devices | json\_query('\*.partitions.keys(@)') }}"

Here you have content in the form of the variable ansible\_device that's why this task work and the task above fails.

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [August 1, 2017, 2:15am UTC](https://forum.ansible.com/t/how-to-mount-partition-when-ntfs-only/25079/6 "2017-08-01T02:15:01Z")

</div>

Yep, I get that…my point is I don’t know how to pull the **NTFS** partition info from the filter. It seems like a nested key pair value and I don’t know how to extract it for comparison

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [August 1, 2017, 5:48pm UTC](https://forum.ansible.com/t/how-to-mount-partition-when-ntfs-only/25079/7 "2017-08-01T17:48:45Z")

</div>

I got a bit further. I was able to add the following:

`

tasks:

- name: Install jmespath via pip if missing  
pip:  
name: jmespath

- name: check for hard drive devices  
setup:  
filter: ansible\_devices  
tags:

- test

- name: get device info  
parted: device=/dev/{{ item.key }} unit=MiB  
register: results  
tags:

- test  
ignore\_errors: yes  
with\_dict: “{{ ansible\_devices }}”

- name: mount ntfs partitions  
mount:  
path: “/mnt/{{ item.item }}”  
src: “{{ item.disk.dev }}”  
fstype: ntfs  
when: “item.partitions | length \> 0 and item.partitions | selectattr(‘fstype’, ‘equalto’, ‘ntfs’) | list | length \> 0”  
with\_items: “{{ results.results }}”  
tags:

- test

`

However, I am getting the following errors:

> PLAY [Backup customer data] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [localhost]  
> TASK [check for hard drive devices] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [localhost]  
> TASK [get device info] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> failed: [localhost] (item={‘key’: u’sr0’, ‘value’: {u’scheduler\_mode’: u’cfq’, u’sectorsize’: u’512’, u’vendor’: u’NECVMWar’, u’sectors’: u’2097151’, u’sas\_device\_handle’: None, u’sas\_address’: None, u’host’: u’IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)‘, u’rotational’: u’1’, u’removable’: u’1’, u’support\_discard’: u’0’, u’holders’: , u’partitions’: {}, u’model’: u’VMware IDE CDR10’, u’size’: u’1024.00 MB’}}) =\> {“err”: “Error: Error opening /dev/sr0: No medium found\n”, “failed”: true, “item”: {“key”: “sr0”, “value”: {“holders”: , “host”: “IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)”, “model”: “VMware IDE CDR10”, “partitions”: {}, “removable”: “1”, “rotational”: “1”, “sas\_address”: null, “sas\_device\_handle”: null, “scheduler\_mode”: “cfq”, “sectors”: “2097151”, “sectorsize”: “512”, “size”: “1024.00 MB”, “support\_discard”: “0”, “vendor”: “NECVMWar”}}, “msg”: “Error while getting device information with parted script: ‘/usr/sbin/parted -s -m /dev/sr0 – unit ‘MiB’ print’”, “out”: “”, “rc”: 1}  
> ok: [localhost] =\> (item={‘key’: u’sda’, ‘value’: {u’scheduler\_mode’: u’cfq’, u’sectorsize’: u’512’, u’vendor’: u’VMware’, u’sectors’: u’67108864’, u’sas\_device\_handle’: None, u’sas\_address’: None, u’host’: u’Serial Attached SCSI controller: LSI Logic / Symbios Logic SAS1068 PCI-X Fusion-MPT SAS (rev 01)‘, u’rotational’: u’1’, u’removable’: u’0’, u’support\_discard’: u’0’, u’holders’: , u’partitions’: {u’sda2’: {u’sectorsize’: 512, u’uuid’: u’A880A12480A0FA48’, u’sectors’: u’66490368’, u’start’: u’616448’, u’holders’: , u’size’: u’31.71 GB’}, u’sda1’: {u’sectorsize’: 512, u’uuid’: u’9844A05B44A03E3E’, u’sectors’: u’614400’, u’start’: u’2048’, u’holders’: , u’size’: u’300.00 MB’}}, u’model’: u’Virtual disk’, u’size’: u’32.00 GB’}})  
> …ignoring  
> TASK [mount ntfs partitions] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “The conditional check ‘item.partitions | length \> 0 and item.partitions | selectattr(‘fstype’, ‘equalto’, ‘ntfs’) | list | length \> 0’ failed. The error was: error while evaluating conditional (item.partitions | length \> 0 and item.partitions | selectattr(‘fstype’, ‘equalto’, ‘ntfs’) | list | length \> 0): ‘dict object’ has no attribute ‘partitions’\n\nThe error appears to have been in ‘/home/svcacctansible/automation/backup/customerBackup.yml’: line 35, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: mount ntfs partitions\n ^ here\n”}  
> to retry, use: --limit @/home/svcacctansible/automation/backup/customerBackup.retry  
> PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> localhost : ok=3 changed=0 unreachable=0 failed=1

I’m only wanting to mount any partition that is **NTFS**

---

<div class="post-metadata">

**Author:** ![Gilberto\_Valentin](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gilberto\_Valentin](https://forum.ansible.com/u/Gilberto_Valentin)\
**Post date:** [August 1, 2017, 11:29pm UTC](https://forum.ansible.com/t/how-to-mount-partition-when-ntfs-only/25079/8 "2017-08-01T23:29:01Z")

</div>

I was able to resolve this by redoing all of my tasks as follows:

`

tasks:

- name: Install jmespath via pip if missing  
pip:  
name: jmespath

- name: run the setup module for some facts about the hardware  
setup:  
filter: ansible\_devices  
tags:

- new

- name: check what drives are available  
parted:  
device: /dev/{{ item }}  
with\_items: “{{ ansible\_devices }}”  
when:

- item != ‘sr0’  
register: result

- name: get a list of paritions that are only ntfs  
debug:  
msg: “{{ item.0.disk.dev }}{{ (item.0.disk.table == ‘loop’) | ternary(‘’, item.1.num) }}”  
when: item.1.fstype == ‘ntfs’  
with\_subelements:

- “{{ result.results }}”

- partitions

- name: mount ntfs partitions  
mount:  
path: “/mnt/{{ item.0.disk.dev }}{{ (item.0.disk.table == ‘loop’) | ternary(‘’, item.1.num) }}”  
src: “{{ item.0.disk.dev }}{{ (item.0.disk.table == ‘loop’) | ternary(‘’, item.1.num) }}”  
fstype: ntfs  
state: mounted  
when: item.1.fstype == ‘ntfs’

# when: results.item.value.partitions.fstype == “ntfs”

with\_subelements:

- “{{ result.results }}”
- partitions

`
