mount: Create a state like absent but keeps the mountpoint (#774)

* Create a new state, with an example and integration test

* Now that I have my PR, add in my changelog

* Fix linter issues

* Another attempt at getting the sanity tests to pass

* Fix FreeBSD integration test

* Try to get sonarcloud code analysis happy

* Change state option name
This commit is contained in:
Michael Sprott
2026-08-31 12:51:24 +09:00
committed by GitHub
parent 7ed7d2399c
commit 20699d7184
3 changed files with 93 additions and 4 deletions
@@ -0,0 +1,2 @@
minor_changes:
- "Introduce an option to remove only the mount and fstab entry but not the mountpoint (https://github.com/ansible-collections/ansible.posix/issues/331)."
+29 -4
View File
@@ -88,6 +88,13 @@ options:
fail if multiple devices are mounted on the same mount point. Using
V(absent) with a mount point that is not registered in the I(fstab) has
no effect, use V(unmounted) instead.
- V(absent_keep_mountpoint) specifies that the mount point entry O(path) will be removed
from I(fstab) and will also unmount the mounted device. The associated
mountpoint will not be removed. A mounted device will be unmounted regardless
of O(src) or its real source. V(absent_keep_mountpoint) does not unmount recursively,
and the module will fail if multiple devices are mounted on the same mount
point. Using V(absent_keep_mountpoint) with a mount point that is not registered in the
I(fstab) has no effect, use V(unmounted) instead.
- V(remounted) specifies that the device will be remounted for when you
want to force a refresh on the mount itself (added in 2.9). This will
always return RV(ignore:changed=true). If O(opts) is set, the options will be
@@ -102,7 +109,7 @@ options:
mountpoint.
type: str
required: true
choices: [ absent, absent_from_fstab, mounted, present, unmounted, remounted, ephemeral ]
choices: [ absent, absent_from_fstab, absent_keep_mountpoint, mounted, present, unmounted, remounted, ephemeral ]
fstab:
description:
- File to use instead of C(/etc/fstab).
@@ -170,6 +177,11 @@ EXAMPLES = r'''
path: /tmp/mnt-pnt
state: unmounted
- name: Unmount a mounted volume and remove its fstab entry
ansible.posix.mount:
path: /tmp/mnt-pnt
state: absent_keep_mountpoint
- name: Remount a mounted volume
ansible.posix.mount:
path: /tmp/mnt-pnt
@@ -774,7 +786,20 @@ def main():
passno=dict(type='str', no_log=False, default='0'),
src=dict(type='path'),
backup=dict(type='bool', default=False),
state=dict(type='str', required=True, choices=['absent', 'absent_from_fstab', 'mounted', 'present', 'unmounted', 'remounted', 'ephemeral']),
state={
'type': 'str',
'required': True,
'choices': [
'absent',
'absent_from_fstab',
'absent_keep_mountpoint',
'mounted',
'present',
'unmounted',
'remounted',
'ephemeral'
]
},
),
supports_check_mode=True,
required_if=(
@@ -883,7 +908,7 @@ def main():
if state == 'absent_from_fstab':
name, changed, _dummy = unset_mount(module, args, backup=False)
elif state == 'absent':
elif state == 'absent' or state == 'absent_keep_mountpoint':
name, changed, backup_lines = unset_mount(module, args, backup=True)
if changed and not module.check_mode:
@@ -895,7 +920,7 @@ def main():
write_fstab(module, backup_lines, args['fstab'])
module.fail_json(msg="Error unmounting %s: %s" % (name, msg))
if os.path.exists(name):
if os.path.exists(name) and state == 'absent':
try:
os.rmdir(name)
except (OSError, IOError) as e:
@@ -297,10 +297,17 @@
when: ansible_system in ('FreeBSD', 'Linux')
register: dest_stat
- name: Check if the mountpoint still exists
ansible.builtin.stat:
path: '{{ output_dir }}/mount_dest'
when: ansible_system in ('FreeBSD', 'Linux')
register: mountpoint_stat
- name: Check that we did not unmount
ansible.builtin.assert:
that:
- unmount_result['changed']
- mountpoint_stat['stat']['exists']
- dest_stat['stat']['exists']
when: ansible_system in ('FreeBSD', 'Linux')
@@ -318,10 +325,65 @@
when: ansible_system in ('FreeBSD', 'Linux')
register: dest_stat
- name: Check that the mountpoint no longer exists
ansible.builtin.stat:
path: '{{ output_dir }}/mount_dest'
when: ansible_system in ('FreeBSD', 'Linux')
register: mountpoint_stat
- name: Check that we unmounted
ansible.builtin.assert:
that:
- unmount_result['changed']
- not mountpoint_stat['stat']['exists']
- not dest_stat['stat']['exists']
when: ansible_system in ('FreeBSD', 'Linux')
# unmount (absent_keep_mountpoint)
- name: Bind mount a filesystem (Linux)
ansible.posix.mount:
src: '{{ output_dir }}/mount_source'
name: '{{ output_dir }}/mount_dest'
state: mounted
fstype: None
opts: bind
when: ansible_system == 'Linux'
register: bind_result_linux
- name: Bind mount a filesystem (FreeBSD)
ansible.posix.mount:
src: '{{ output_dir }}/mount_source'
name: '{{ output_dir }}/mount_dest'
state: mounted
fstype: nullfs
when: ansible_system == 'FreeBSD'
register: bind_result_freebsd
- name: Unmount the bind mount while keeping the mountpoint
ansible.posix.mount:
name: '{{ output_dir }}/mount_dest'
state: absent_keep_mountpoint
when: ansible_system in ('Linux', 'FreeBSD')
register: unmount_result
- name: Make sure the file no longer exists in dest
ansible.builtin.stat:
path: '{{ output_dir }}/mount_dest/test_file'
when: ansible_system in ('FreeBSD', 'Linux')
register: dest_stat
- name: Check if the mountpoint still exists
ansible.builtin.stat:
path: '{{ output_dir }}/mount_dest'
when: ansible_system in ('FreeBSD', 'Linux')
register: mountpoint_stat
- name: Check that we unmounted and that the mountpoint still exists
ansible.builtin.assert:
that:
- (ansible_system == 'Linux' and bind_result_linux['changed']) or (ansible_system == 'FreeBSD' and bind_result_freebsd['changed'])
- unmount_result['changed']
- mountpoint_stat['stat']['exists']
- not dest_stat['stat']['exists']
when: ansible_system in ('FreeBSD', 'Linux')