This commit is contained in:
Sato Kenta 2021-06-28 12:44:42 +09:00 committed by GitHub
commit 4d5b010fb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 102 additions and 0 deletions

View file

@ -0,0 +1,3 @@
---
minor_changes:
- mount - add ``umask`` parameter to control permissions of the directories created by the module (https://github.com/ansible-collections/ansible.posix/issues/163).

View file

@ -108,6 +108,14 @@ 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
umask:
description:
- 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.
type: raw
version_added: '1.3.0'
notes: notes:
- As of Ansible 2.3, the I(name) option has been changed to I(path) as - As of Ansible 2.3, the I(name) option has been changed to I(path) as
default, but I(name) still works as well. default, but I(name) still works as well.
@ -125,6 +133,7 @@ EXAMPLES = r'''
fstype: iso9660 fstype: iso9660
opts: ro,noauto opts: ro,noauto
state: present state: present
umask: 0022
- name: Mount up device by label - name: Mount up device by label
ansible.posix.mount: ansible.posix.mount:
@ -668,6 +677,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']),
umask=dict(type='raw'),
), ),
supports_check_mode=True, supports_check_mode=True,
required_if=( required_if=(
@ -764,6 +774,7 @@ def main():
state = module.params['state'] state = module.params['state']
name = module.params['path'] name = module.params['path']
umask = module.params['umask']
changed = False changed = False
if state == 'absent': if state == 'absent':
@ -795,6 +806,15 @@ def main():
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)
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.
@ -819,6 +839,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:
if old_umask is not None:
os.umask(old_umask)
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

@ -382,3 +382,79 @@
- /tmp/myfs.img - /tmp/myfs.img
- /tmp/myfs - /tmp/myfs
when: ansible_system in ('Linux') when: ansible_system in ('Linux')
- name: Block to test umask option
block:
- name: Make sure that mount point does not exist
file:
path: /tmp/mount_dest
state: absent
- name: Create a directory to bind mount
file:
state: directory
path: /tmp/mount_source
- name: Bind mount a filesystem with umask
mount:
src: /tmp/mount_source
path: /tmp/mount_dest
state: mounted
fstype: None
opts: bind
umask: 0777
when: ansible_system != 'FreeBSD'
- name: Bind mount a filesystem with umask(FreeBSD)
mount:
src: /tmp/mount_source
path: /tmp/mount_dest
state: mounted
fstype: nullfs
umask: 0777
when: ansible_system == 'FreeBSD'
- name: Unmount FS to access underlying directory
command: |
umount /tmp/mount_dest
- name: Stat mount point directory
stat:
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/mount_dest
state: absent
- name: Bind mount a filesystem with string umask
mount:
src: /tmp/mount_source
path: /tmp/mount_dest
state: mounted
fstype: None
opts: bind
umask: "0777"
when: ansible_system != 'FreeBSD'
- name: Bind mount a filesystem with string umask(FreeBSD)
mount:
src: /tmp/mount_source
path: /tmp/mount_dest
state: mounted
fstype: nullfs
umask: "0777"
when: ansible_system == 'FreeBSD'
- name: Unmount FS to access underlying directory
command: |
umount /tmp/mount_dest
- name: Stat mount point directory
stat:
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: Remove the test FS
file:
path: /tmp/mount_dest
state: absent
when: ansible_system not in ('Darwin')