Add umask option for mount module

This commit is contained in:
satken2 2021-06-27 23:56:42 +09:00
parent c14f4d75a6
commit d6ae0981c5
2 changed files with 49 additions and 168 deletions

View file

@ -107,7 +107,7 @@ options:
default: no default: no
umask: umask:
description: 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. If the mount point already exists, this parameter is not used.
- Note that after running this task and the device being successfully mounted, - 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. the mode of the original directory will be hidden by the target device.
@ -801,8 +801,19 @@ def main():
changed = True changed = True
elif state == 'mounted': elif state == 'mounted':
dirs_created = [] dirs_created = []
if not os.path.exists(name) and not module.check_mode: 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: try:
# Something like mkdir -p but with the possibility to undo. # Something like mkdir -p but with the possibility to undo.
# Based on some copy-paste from the "file" module. # Based on some copy-paste from the "file" module.
@ -827,31 +838,9 @@ def main():
except (OSError, IOError) as e: except (OSError, IOError) as e:
module.fail_json( module.fail_json(
msg="Error making dir %s: %s" % (name, to_native(e))) msg="Error making dir %s: %s" % (name, to_native(e)))
finally:
# Set permissions to the newly created mount point. if old_umask is not None:
if umask is not None: os.umask(old_umask)
# 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)))
name, backup_lines, changed = _set_mount_save_old(module, args) name, backup_lines, changed = _set_mount_save_old(module, args)
res = 0 res = 0

View file

@ -335,55 +335,37 @@
- name: Block to test umask option - name: Block to test umask option
block: 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 - name: Make sure that mount point does not exist
file: file:
path: /tmp/myfs_mountpoint path: /tmp/mount_dest
state: absent state: absent
- name: Create a directory to bind mount
- name: Mount the FS to non existent directory with raw umask file:
state: directory
path: /tmp/mount_source
- name: Bind mount a filesystem with umask
mount: mount:
path: /tmp/myfs_mountpoint src: /tmp/mount_source
src: /tmp/myfs.img path: /tmp/mount_dest
fstype: ext3
state: mounted state: mounted
fstype: None
opts: bind
umask: 0777 umask: 0777
when: ansible_system == 'Linux' when: ansible_system != 'FreeBSD'
- name: Mount the FS to non existent directory with raw umask(FreeBSD) - name: Bind mount a filesystem with umask(FreeBSD)
mount: mount:
path: /tmp/myfs_mountpoint src: /tmp/mount_source
src: /tmp/myfs.img path: /tmp/mount_dest
fstype: nullfs
state: mounted state: mounted
fstype: nullfs
umask: 0777 umask: 0777
when: ansible_system == 'FreeBSD' 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 - name: Unmount FS to access underlying directory
command: | command: |
umount /tmp/myfs.img umount /tmp/mount_dest
- name: Check status of mount point - name: Stat mount point directory
stat: stat:
path: /tmp/myfs_mountpoint path: /tmp/mount_dest
register: mount_point_stat register: mount_point_stat
- name: Assert that the mount point has right permission - name: Assert that the mount point has right permission
assert: assert:
@ -391,128 +373,38 @@
- mount_point_stat['stat']['mode'] == '0000' - mount_point_stat['stat']['mode'] == '0000'
- name: Cleanup directory - name: Cleanup directory
file: file:
path: /tmp/myfs_mountpoint path: /tmp/mount_dest
state: absent state: absent
- name: Bind mount a filesystem with string umask
- name: Mount the FS to non existent directory with string umask
mount: mount:
path: /tmp/myfs_mountpoint src: /tmp/mount_source
src: /tmp/myfs.img path: /tmp/mount_dest
fstype: ext3
state: mounted state: mounted
fstype: None
opts: bind
umask: "0777" umask: "0777"
when: ansible_system == 'Linux' when: ansible_system != 'FreeBSD'
- name: Mount the FS to non existent directory with string umask(FreeBSD) - name: Bind mount a filesystem with string umask(FreeBSD)
mount: mount:
path: /tmp/myfs_mountpoint src: /tmp/mount_source
src: /tmp/myfs.img path: /tmp/mount_dest
fstype: nullfs
state: mounted state: mounted
fstype: nullfs
umask: "0777" umask: "0777"
when: ansible_system == 'FreeBSD' 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 - name: Unmount FS to access underlying directory
command: | command: |
umount /tmp/myfs.img umount /tmp/mount_dest
- name: Check status of mount point - name: Stat mount point directory
stat: stat:
path: /tmp/myfs_mountpoint path: /tmp/mount_dest
register: mount_point_stat register: mount_point_stat
- name: Assert that the mount point has right permission - name: Assert that the mount point has right permission
assert: assert:
that: that:
- mount_point_stat['stat']['mode'] == '0000' - 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 - name: Remove the test FS
file: file:
path: '{{ item }}' path: /tmp/mount_dest
state: absent state: absent
loop: when: ansible_system not in ('MacOS')
- /tmp/myfs.img
- /tmp/myfs_mountpoint