diff --git a/plugins/modules/mount.py b/plugins/modules/mount.py index 8d75b4a..6e3193d 100644 --- a/plugins/modules/mount.py +++ b/plugins/modules/mount.py @@ -107,7 +107,7 @@ options: default: no umask: description: - - The permission applied to create new directory(ies) for the mount point. + - The umask to set before creating new directory(ies) for the mount point. If the mount point already exists, this parameter is not used. - Note that after running this task and the device being successfully mounted, the mode of the original directory will be hidden by the target device. @@ -801,8 +801,19 @@ def main(): changed = True elif state == 'mounted': + dirs_created = [] if not os.path.exists(name) and not module.check_mode: + old_umask = None + if umask is not None: + if not isinstance(umask, int): + try: + umask = int(umask, 8) + except ValueError as e: + module.fail_json(msg="umask must be an octal integer: %s" % (to_native(e))) + old_umask = os.umask(umask) + os.umask(umask) + try: # Something like mkdir -p but with the possibility to undo. # Based on some copy-paste from the "file" module. @@ -827,31 +838,9 @@ def main(): except (OSError, IOError) as e: module.fail_json( msg="Error making dir %s: %s" % (name, to_native(e))) - - # Set permissions to the newly created mount point. - if umask is not None: - # When umask is integer, calculate logical complement of the value - # otherwise, pass it to set_mode_if_different() as is. - if isinstance(umask, int): - directory_mode = 0o0777 & ~umask - else: - try: - umask = int(umask, 8) - directory_mode = 0o0777 & ~umask - except Exception: - directory_mode = umask - - try: - for dirname in dirs_created: - changed = module.set_mode_if_different(dirname, directory_mode, changed) - except Exception as e: - try: - for dirname in dirs_created[::-1]: - os.rmdir(dirname) - except Exception: - pass - module.fail_json( - msg="Error setting permissions %s: %s" % (name, to_native(e))) + finally: + if old_umask is not None: + os.umask(old_umask) name, backup_lines, changed = _set_mount_save_old(module, args) res = 0 diff --git a/tests/integration/targets/mount/tasks/main.yml b/tests/integration/targets/mount/tasks/main.yml index 8d4a85c..a7daa6c 100644 --- a/tests/integration/targets/mount/tasks/main.yml +++ b/tests/integration/targets/mount/tasks/main.yml @@ -335,55 +335,37 @@ - name: Block to test umask option block: - - name: Create empty file - community.general.filesize: - path: /tmp/myfs.img - size: 1M - - name: Format FS - community.general.filesystem: - fstype: ext3 - dev: /tmp/myfs.img - when: ansible_system == 'Linux' - - name: Format FS - community.general.filesystem: - fstype: nullfs - dev: /tmp/myfs.img - when: ansible_system == 'FreeBSD' - name: Make sure that mount point does not exist file: - path: /tmp/myfs_mountpoint + path: /tmp/mount_dest state: absent - - - name: Mount the FS to non existent directory with raw umask + - name: Create a directory to bind mount + file: + state: directory + path: /tmp/mount_source + - name: Bind mount a filesystem with umask mount: - path: /tmp/myfs_mountpoint - src: /tmp/myfs.img - fstype: ext3 + src: /tmp/mount_source + path: /tmp/mount_dest state: mounted + fstype: None + opts: bind umask: 0777 - when: ansible_system == 'Linux' - - name: Mount the FS to non existent directory with raw umask(FreeBSD) + when: ansible_system != 'FreeBSD' + - name: Bind mount a filesystem with umask(FreeBSD) mount: - path: /tmp/myfs_mountpoint - src: /tmp/myfs.img - fstype: nullfs + src: /tmp/mount_source + path: /tmp/mount_dest state: mounted + fstype: nullfs umask: 0777 when: ansible_system == 'FreeBSD' - - name: Check status of parent directory of mount point - stat: - path: /tmp/foobar - register: parent_dir_stat - - name: Assert that the parent directory of the mount point has right permission - assert: - that: - - parent_dir_stat['stat']['mode'] == '0000' - name: Unmount FS to access underlying directory command: | - umount /tmp/myfs.img - - name: Check status of mount point + umount /tmp/mount_dest + - name: Stat mount point directory stat: - path: /tmp/myfs_mountpoint + path: /tmp/mount_dest register: mount_point_stat - name: Assert that the mount point has right permission assert: @@ -391,128 +373,38 @@ - mount_point_stat['stat']['mode'] == '0000' - name: Cleanup directory file: - path: /tmp/myfs_mountpoint + path: /tmp/mount_dest state: absent - - - name: Mount the FS to non existent directory with string umask + - name: Bind mount a filesystem with string umask mount: - path: /tmp/myfs_mountpoint - src: /tmp/myfs.img - fstype: ext3 + src: /tmp/mount_source + path: /tmp/mount_dest state: mounted + fstype: None + opts: bind umask: "0777" - when: ansible_system == 'Linux' - - name: Mount the FS to non existent directory with string umask(FreeBSD) + when: ansible_system != 'FreeBSD' + - name: Bind mount a filesystem with string umask(FreeBSD) mount: - path: /tmp/myfs_mountpoint - src: /tmp/myfs.img - fstype: nullfs + src: /tmp/mount_source + path: /tmp/mount_dest state: mounted + fstype: nullfs umask: "0777" when: ansible_system == 'FreeBSD' - - name: Check status of parent directory of mount point - stat: - path: /tmp/foobar - register: parent_dir_stat - - name: Assert that the parent directory of the mount point has right permission - assert: - that: - - parent_dir_stat['stat']['mode'] == '0000' - name: Unmount FS to access underlying directory command: | - umount /tmp/myfs.img - - name: Check status of mount point + umount /tmp/mount_dest + - name: Stat mount point directory stat: - path: /tmp/myfs_mountpoint + path: /tmp/mount_dest register: mount_point_stat - name: Assert that the mount point has right permission assert: that: - mount_point_stat['stat']['mode'] == '0000' - - name: Cleanup directory - file: - path: /tmp/myfs_mountpoint - state: absent - - - name: Remount the FS to non existent directory with symbolic umask expression - mount: - path: /tmp/myfs_mountpoint - src: /tmp/myfs.img - fstype: ext3 - state: mounted - umask: "u+rw,g-wx,o-rwx" - when: ansible_system == 'Linux' - - name: Remount the FS to non existent directory with symbolic umask expression(FreeBSD) - mount: - path: /tmp/myfs_mountpoint - src: /tmp/myfs.img - fstype: nullfs - state: mounted - umask: "u+rw,g-wx,o-rwx" - when: ansible_system == 'FreeBSD' - - name: Check status of parent directory of mount point - stat: - path: /tmp/foobar - register: parent_dir_stat - - name: Assert that the parent directory of the mount point has right permission - assert: - that: - - parent_dir_stat['stat']['mode'] == '0640' - - name: Unmount FS to access underlying directory - command: | - umount /tmp/myfs.img - - name: Check status of mount point - stat: - path: /tmp/myfs_mountpoint - register: mount_point_stat - - name: Assert that the mount point has right permission - assert: - that: - - mount_point_stat['stat']['mode'] == '0640' - - name: Cleanup directory - file: - path: /tmp/myfs_mountpoint - state: absent - - - name: Remount the FS to non existent directory with symbolic umask expression - mount: - path: /tmp/myfs_mountpoint - src: /tmp/myfs.img - fstype: ext3 - state: mounted - umask: "u=rw,g=r,o=r" - when: ansible_system == 'Linux' - - name: Remount the FS to non existent directory with symbolic umask expression(FreeBSD) - mount: - path: /tmp/myfs_mountpoint - src: /tmp/myfs.img - fstype: nullfs - state: mounted - umask: "u=rw,g=r,o=r" - when: ansible_system == 'FreeBSD' - - name: Check status of parent directory of mount point - stat: - path: /tmp/foobar - register: parent_dir_stat - - name: Assert that the parent directory of the mount point has right permission - assert: - that: - - parent_dir_stat['stat']['mode'] == '0644' - - name: Unmount FS to access underlying directory - command: | - umount /tmp/myfs.img - - name: Check status of mount point - stat: - path: /tmp/myfs_mountpoint - register: mount_point_stat - - name: Assert that the mount point has right permission - assert: - that: - - mount_point_stat['stat']['mode'] == '0644' - name: Remove the test FS file: - path: '{{ item }}' + path: /tmp/mount_dest state: absent - loop: - - /tmp/myfs.img - - /tmp/myfs_mountpoint + when: ansible_system not in ('MacOS')