From 5ee818ec86771b8523517d0f990094fdf2424601 Mon Sep 17 00:00:00 2001 From: Yves MOYROUD <41052194+YvesMP@users.noreply.github.com> Date: Wed, 18 Sep 2024 17:41:21 +0200 Subject: [PATCH 1/4] Update mount.py In function main(), remove rmdir in case if state == 'absent'. Unmounting a file system should not lead to delete anything that is revealed after unmounting. Also, it leads to an error if a non empty directory is present under the ex-mountpoint after umount : [Errno 39] Directory not empty So umount is successfull but the ansible run is failed. Of course, it is solved on second run. --- plugins/modules/mount.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/plugins/modules/mount.py b/plugins/modules/mount.py index fe9faff..7bc897b 100644 --- a/plugins/modules/mount.py +++ b/plugins/modules/mount.py @@ -899,11 +899,6 @@ def main(): module.fail_json( msg="Error unmounting %s: %s" % (name, msg)) - if os.path.exists(name): - try: - os.rmdir(name) - except (OSError, IOError) as e: - module.fail_json(msg="Error rmdir %s: %s" % (name, to_native(e))) elif state == 'unmounted': if ismount(name) or is_bind_mounted(module, linux_mounts, name): if not module.check_mode: From 966df79767deecce410279845df108097367904f Mon Sep 17 00:00:00 2001 From: Yves MOYROUD <41052194+YvesMP@users.noreply.github.com> Date: Thu, 3 Oct 2024 11:36:45 +0200 Subject: [PATCH 2/4] Update mount.py new option keep_mountpoint enables keeping the mountpoint with state=absent --- plugins/modules/mount.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/plugins/modules/mount.py b/plugins/modules/mount.py index 7bc897b..d66e625 100644 --- a/plugins/modules/mount.py +++ b/plugins/modules/mount.py @@ -73,7 +73,7 @@ options: point will be created. - If V(unmounted), the device will be unmounted without changing I(fstab). - V(present) only specifies that the device is to be configured in - I(fstab) and does not trigger or require a mount. + I(fstab) and does not trigger or require a mount. TODO check - V(ephemeral) only specifies that the device is to be mounted, without changing I(fstab). If it is already mounted, a remount will be triggered. This will always return RV(ignore:changed=true). If the mount point O(path) @@ -87,7 +87,8 @@ options: real source. V(absent) does not unmount recursively, and the module will 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. + no effect, use V(unmounted) instead. You can set O(keep_mountpoint) to + True to keep the mountpoint. - 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 @@ -132,6 +133,16 @@ options: the original file back if you somehow clobbered it incorrectly. type: bool default: false + keep_mountpoint: + description: + - Change the default behaviour of state=absent by keeping the mountpoint + - With keep_mountpoint=true, state=absent is like unmounted plus the + fstab update. + - Use it if you care about finding original mountpoint content without failing + and want to remove the entry in fstab. If you have no entry to clean in + fstab you can use state=unmounted + type: bool + default: false notes: - As of Ansible 2.3, the O(name) option has been changed to O(path) as default, but O(name) still works as well. @@ -779,6 +790,7 @@ def main(): 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']), + keep_mountpoint=dict(type='bool', default=False), ), supports_check_mode=True, required_if=( @@ -899,6 +911,11 @@ def main(): module.fail_json( msg="Error unmounting %s: %s" % (name, msg)) + if os.path.exists(name) and module.params['keep_mountpoint'] is False: + try: + os.rmdir(name) + except (OSError, IOError) as e: + module.fail_json(msg="Error rmdir %s: %s" % (name, to_native(e))) elif state == 'unmounted': if ismount(name) or is_bind_mounted(module, linux_mounts, name): if not module.check_mode: From f7f54f242d1a0d1515cdb3d13f5f3a2d05b91d50 Mon Sep 17 00:00:00 2001 From: Yves MOYROUD <41052194+YvesMP@users.noreply.github.com> Date: Thu, 3 Oct 2024 13:32:42 +0200 Subject: [PATCH 3/4] Update mount.py suppressed erroneous remaining TOTO check --- plugins/modules/mount.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/mount.py b/plugins/modules/mount.py index d66e625..2bda2c6 100644 --- a/plugins/modules/mount.py +++ b/plugins/modules/mount.py @@ -73,7 +73,7 @@ options: point will be created. - If V(unmounted), the device will be unmounted without changing I(fstab). - V(present) only specifies that the device is to be configured in - I(fstab) and does not trigger or require a mount. TODO check + I(fstab) and does not trigger or require a mount. - V(ephemeral) only specifies that the device is to be mounted, without changing I(fstab). If it is already mounted, a remount will be triggered. This will always return RV(ignore:changed=true). If the mount point O(path) From 284025660c053de08f5607d41550242584bc94aa Mon Sep 17 00:00:00 2001 From: Yves MOYROUD <41052194+YvesMP@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:27:18 +0200 Subject: [PATCH 4/4] delete trailing spaces in mount.py delete trailing spaces in comments in mount.py --- plugins/modules/mount.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/modules/mount.py b/plugins/modules/mount.py index 2bda2c6..183eb9d 100644 --- a/plugins/modules/mount.py +++ b/plugins/modules/mount.py @@ -73,7 +73,7 @@ options: point will be created. - If V(unmounted), the device will be unmounted without changing I(fstab). - V(present) only specifies that the device is to be configured in - I(fstab) and does not trigger or require a mount. + I(fstab) and does not trigger or require a mount. - V(ephemeral) only specifies that the device is to be mounted, without changing I(fstab). If it is already mounted, a remount will be triggered. This will always return RV(ignore:changed=true). If the mount point O(path) @@ -87,7 +87,7 @@ options: real source. V(absent) does not unmount recursively, and the module will 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. You can set O(keep_mountpoint) to + no effect, use V(unmounted) instead. You can set O(keep_mountpoint) to True to keep the mountpoint. - 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 @@ -138,7 +138,7 @@ options: - Change the default behaviour of state=absent by keeping the mountpoint - With keep_mountpoint=true, state=absent is like unmounted plus the fstab update. - - Use it if you care about finding original mountpoint content without failing + - Use it if you care about finding original mountpoint content without failing and want to remove the entry in fstab. If you have no entry to clean in fstab you can use state=unmounted type: bool