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

A new `absent_keep_mountpoint` option has been added to the state.
This option keeps the mount point from being deleted when the state is absent.

Co-authored-by: Michael Sprott <165604036+sprottm@users.noreply.github.com>
This commit is contained in:
Hideki Saito
2026-09-01 08:44:28 +09:00
committed by GitHub
co-authored by Michael Sprott
parent 7811740296
commit e03234e855
3 changed files with 93 additions and 4 deletions
+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: