Add umask option for mount module

This commit is contained in:
satken2 2021-06-27 19:56:31 +09:00
parent d9f85d3ce8
commit c14f4d75a6
2 changed files with 176 additions and 19 deletions

View file

@ -105,13 +105,10 @@ options:
the original file back if you somehow clobbered it incorrectly. the original file back if you somehow clobbered it incorrectly.
type: bool type: bool
default: no default: no
mode: umask:
description: description:
- The permission applied to create a new directory for the mount point. - The permission applied to create 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.
- This parameter only affects the mount point itself.
If this module creates multiple directories recursively,
other directories follow the system's default umask.
- 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.
type: raw type: raw
@ -133,7 +130,7 @@ EXAMPLES = r'''
fstype: iso9660 fstype: iso9660
opts: ro,noauto opts: ro,noauto
state: present state: present
mode: 0755 umask: 0022
- name: Mount up device by label - name: Mount up device by label
ansible.posix.mount: ansible.posix.mount:
@ -677,7 +674,7 @@ def main():
src=dict(type='path'), src=dict(type='path'),
backup=dict(type='bool', default=False), backup=dict(type='bool', default=False),
state=dict(type='str', required=True, choices=['absent', 'mounted', 'present', 'unmounted', 'remounted']), state=dict(type='str', required=True, choices=['absent', 'mounted', 'present', 'unmounted', 'remounted']),
mode=dict(type='raw'), umask=dict(type='raw'),
), ),
supports_check_mode=True, supports_check_mode=True,
required_if=( required_if=(
@ -774,7 +771,7 @@ def main():
state = module.params['state'] state = module.params['state']
name = module.params['path'] name = module.params['path']
mode = module.params['mode'] umask = module.params['umask']
changed = False changed = False
if state == 'absent': if state == 'absent':
@ -832,10 +829,27 @@ def main():
msg="Error making dir %s: %s" % (name, to_native(e))) msg="Error making dir %s: %s" % (name, to_native(e)))
# Set permissions to the newly created mount point. # Set permissions to the newly created mount point.
if mode is not None: 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: try:
changed = module.set_mode_if_different(name, mode, changed) for dirname in dirs_created:
changed = module.set_mode_if_different(dirname, directory_mode, changed)
except Exception as e: except Exception as e:
try:
for dirname in dirs_created[::-1]:
os.rmdir(dirname)
except Exception:
pass
module.fail_json( module.fail_json(
msg="Error setting permissions %s: %s" % (name, to_native(e))) msg="Error setting permissions %s: %s" % (name, to_native(e)))

View file

@ -333,43 +333,186 @@
- /tmp/myfs - /tmp/myfs
when: ansible_system in ('Linux') when: ansible_system in ('Linux')
- name: Block to test mode option in Linux - name: Block to test umask option
block: block:
- name: Create empty file - name: Create empty file
community.general.filesize: community.general.filesize:
path: /tmp/myfs.img path: /tmp/myfs.img
size: 20M size: 1M
- name: Format FS - name: Format FS
community.general.filesystem: community.general.filesystem:
fstype: ext3 fstype: ext3
dev: /tmp/myfs.img 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 path: /tmp/myfs_mountpoint
state: absent state: absent
- name: Mount the FS to non existent directory with mode option
- name: Mount the FS to non existent directory with raw umask
mount: mount:
path: /tmp/myfs path: /tmp/myfs_mountpoint
src: /tmp/myfs.img src: /tmp/myfs.img
fstype: ext3 fstype: ext3
state: mounted state: mounted
mode: 0000 umask: 0777
when: ansible_system == 'Linux'
- name: Mount the FS to non existent directory with raw umask(FreeBSD)
mount:
path: /tmp/myfs_mountpoint
src: /tmp/myfs.img
fstype: nullfs
state: mounted
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 - name: Unmount FS to access underlying directory
command: | command: |
umount /tmp/myfs.img umount /tmp/myfs.img
- name: Check status of mount point - name: Check status of mount point
stat: stat:
path: /tmp/myfs path: /tmp/myfs_mountpoint
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: Mount the FS to non existent directory with string umask
mount:
path: /tmp/myfs_mountpoint
src: /tmp/myfs.img
fstype: ext3
state: mounted
umask: "0777"
when: ansible_system == 'Linux'
- name: Mount the FS to non existent directory with string umask(FreeBSD)
mount:
path: /tmp/myfs_mountpoint
src: /tmp/myfs.img
fstype: nullfs
state: mounted
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
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'] == '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: '{{ item }}'
state: absent state: absent
loop: loop:
- /tmp/myfs.img - /tmp/myfs.img
- /tmp/myfs - /tmp/myfs_mountpoint
when: ansible_system in ('Linux')