Skip to content

Commit d6ae098

Browse files
author
satken2
committed
Add umask option for mount module
1 parent c14f4d7 commit d6ae098

File tree

2 files changed

+49
-168
lines changed

2 files changed

+49
-168
lines changed

plugins/modules/mount.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
default: no
108108
umask:
109109
description:
110-
- The permission applied to create new directory(ies) for the mount point.
110+
- The umask to set before creating new directory(ies) for the mount point.
111111
If the mount point already exists, this parameter is not used.
112112
- Note that after running this task and the device being successfully mounted,
113113
the mode of the original directory will be hidden by the target device.
@@ -801,8 +801,19 @@ def main():
801801

802802
changed = True
803803
elif state == 'mounted':
804+
804805
dirs_created = []
805806
if not os.path.exists(name) and not module.check_mode:
807+
old_umask = None
808+
if umask is not None:
809+
if not isinstance(umask, int):
810+
try:
811+
umask = int(umask, 8)
812+
except ValueError as e:
813+
module.fail_json(msg="umask must be an octal integer: %s" % (to_native(e)))
814+
old_umask = os.umask(umask)
815+
os.umask(umask)
816+
806817
try:
807818
# Something like mkdir -p but with the possibility to undo.
808819
# Based on some copy-paste from the "file" module.
@@ -827,31 +838,9 @@ def main():
827838
except (OSError, IOError) as e:
828839
module.fail_json(
829840
msg="Error making dir %s: %s" % (name, to_native(e)))
830-
831-
# Set permissions to the newly created mount point.
832-
if umask is not None:
833-
# When umask is integer, calculate logical complement of the value
834-
# otherwise, pass it to set_mode_if_different() as is.
835-
if isinstance(umask, int):
836-
directory_mode = 0o0777 & ~umask
837-
else:
838-
try:
839-
umask = int(umask, 8)
840-
directory_mode = 0o0777 & ~umask
841-
except Exception:
842-
directory_mode = umask
843-
844-
try:
845-
for dirname in dirs_created:
846-
changed = module.set_mode_if_different(dirname, directory_mode, changed)
847-
except Exception as e:
848-
try:
849-
for dirname in dirs_created[::-1]:
850-
os.rmdir(dirname)
851-
except Exception:
852-
pass
853-
module.fail_json(
854-
msg="Error setting permissions %s: %s" % (name, to_native(e)))
841+
finally:
842+
if old_umask is not None:
843+
os.umask(old_umask)
855844

856845
name, backup_lines, changed = _set_mount_save_old(module, args)
857846
res = 0

tests/integration/targets/mount/tasks/main.yml

Lines changed: 34 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -335,184 +335,76 @@
335335

336336
- name: Block to test umask option
337337
block:
338-
- name: Create empty file
339-
community.general.filesize:
340-
path: /tmp/myfs.img
341-
size: 1M
342-
- name: Format FS
343-
community.general.filesystem:
344-
fstype: ext3
345-
dev: /tmp/myfs.img
346-
when: ansible_system == 'Linux'
347-
- name: Format FS
348-
community.general.filesystem:
349-
fstype: nullfs
350-
dev: /tmp/myfs.img
351-
when: ansible_system == 'FreeBSD'
352338
- name: Make sure that mount point does not exist
353339
file:
354-
path: /tmp/myfs_mountpoint
340+
path: /tmp/mount_dest
355341
state: absent
356-
357-
- name: Mount the FS to non existent directory with raw umask
342+
- name: Create a directory to bind mount
343+
file:
344+
state: directory
345+
path: /tmp/mount_source
346+
- name: Bind mount a filesystem with umask
358347
mount:
359-
path: /tmp/myfs_mountpoint
360-
src: /tmp/myfs.img
361-
fstype: ext3
348+
src: /tmp/mount_source
349+
path: /tmp/mount_dest
362350
state: mounted
351+
fstype: None
352+
opts: bind
363353
umask: 0777
364-
when: ansible_system == 'Linux'
365-
- name: Mount the FS to non existent directory with raw umask(FreeBSD)
354+
when: ansible_system != 'FreeBSD'
355+
- name: Bind mount a filesystem with umask(FreeBSD)
366356
mount:
367-
path: /tmp/myfs_mountpoint
368-
src: /tmp/myfs.img
369-
fstype: nullfs
357+
src: /tmp/mount_source
358+
path: /tmp/mount_dest
370359
state: mounted
360+
fstype: nullfs
371361
umask: 0777
372362
when: ansible_system == 'FreeBSD'
373-
- name: Check status of parent directory of mount point
374-
stat:
375-
path: /tmp/foobar
376-
register: parent_dir_stat
377-
- name: Assert that the parent directory of the mount point has right permission
378-
assert:
379-
that:
380-
- parent_dir_stat['stat']['mode'] == '0000'
381363
- name: Unmount FS to access underlying directory
382364
command: |
383-
umount /tmp/myfs.img
384-
- name: Check status of mount point
365+
umount /tmp/mount_dest
366+
- name: Stat mount point directory
385367
stat:
386-
path: /tmp/myfs_mountpoint
368+
path: /tmp/mount_dest
387369
register: mount_point_stat
388370
- name: Assert that the mount point has right permission
389371
assert:
390372
that:
391373
- mount_point_stat['stat']['mode'] == '0000'
392374
- name: Cleanup directory
393375
file:
394-
path: /tmp/myfs_mountpoint
376+
path: /tmp/mount_dest
395377
state: absent
396-
397-
- name: Mount the FS to non existent directory with string umask
378+
- name: Bind mount a filesystem with string umask
398379
mount:
399-
path: /tmp/myfs_mountpoint
400-
src: /tmp/myfs.img
401-
fstype: ext3
380+
src: /tmp/mount_source
381+
path: /tmp/mount_dest
402382
state: mounted
383+
fstype: None
384+
opts: bind
403385
umask: "0777"
404-
when: ansible_system == 'Linux'
405-
- name: Mount the FS to non existent directory with string umask(FreeBSD)
386+
when: ansible_system != 'FreeBSD'
387+
- name: Bind mount a filesystem with string umask(FreeBSD)
406388
mount:
407-
path: /tmp/myfs_mountpoint
408-
src: /tmp/myfs.img
409-
fstype: nullfs
389+
src: /tmp/mount_source
390+
path: /tmp/mount_dest
410391
state: mounted
392+
fstype: nullfs
411393
umask: "0777"
412394
when: ansible_system == 'FreeBSD'
413-
- name: Check status of parent directory of mount point
414-
stat:
415-
path: /tmp/foobar
416-
register: parent_dir_stat
417-
- name: Assert that the parent directory of the mount point has right permission
418-
assert:
419-
that:
420-
- parent_dir_stat['stat']['mode'] == '0000'
421395
- name: Unmount FS to access underlying directory
422396
command: |
423-
umount /tmp/myfs.img
424-
- name: Check status of mount point
397+
umount /tmp/mount_dest
398+
- name: Stat mount point directory
425399
stat:
426-
path: /tmp/myfs_mountpoint
400+
path: /tmp/mount_dest
427401
register: mount_point_stat
428402
- name: Assert that the mount point has right permission
429403
assert:
430404
that:
431405
- mount_point_stat['stat']['mode'] == '0000'
432-
- name: Cleanup directory
433-
file:
434-
path: /tmp/myfs_mountpoint
435-
state: absent
436-
437-
- name: Remount the FS to non existent directory with symbolic umask expression
438-
mount:
439-
path: /tmp/myfs_mountpoint
440-
src: /tmp/myfs.img
441-
fstype: ext3
442-
state: mounted
443-
umask: "u+rw,g-wx,o-rwx"
444-
when: ansible_system == 'Linux'
445-
- name: Remount the FS to non existent directory with symbolic umask expression(FreeBSD)
446-
mount:
447-
path: /tmp/myfs_mountpoint
448-
src: /tmp/myfs.img
449-
fstype: nullfs
450-
state: mounted
451-
umask: "u+rw,g-wx,o-rwx"
452-
when: ansible_system == 'FreeBSD'
453-
- name: Check status of parent directory of mount point
454-
stat:
455-
path: /tmp/foobar
456-
register: parent_dir_stat
457-
- name: Assert that the parent directory of the mount point has right permission
458-
assert:
459-
that:
460-
- parent_dir_stat['stat']['mode'] == '0640'
461-
- name: Unmount FS to access underlying directory
462-
command: |
463-
umount /tmp/myfs.img
464-
- name: Check status of mount point
465-
stat:
466-
path: /tmp/myfs_mountpoint
467-
register: mount_point_stat
468-
- name: Assert that the mount point has right permission
469-
assert:
470-
that:
471-
- mount_point_stat['stat']['mode'] == '0640'
472-
- name: Cleanup directory
473-
file:
474-
path: /tmp/myfs_mountpoint
475-
state: absent
476-
477-
- name: Remount the FS to non existent directory with symbolic umask expression
478-
mount:
479-
path: /tmp/myfs_mountpoint
480-
src: /tmp/myfs.img
481-
fstype: ext3
482-
state: mounted
483-
umask: "u=rw,g=r,o=r"
484-
when: ansible_system == 'Linux'
485-
- name: Remount the FS to non existent directory with symbolic umask expression(FreeBSD)
486-
mount:
487-
path: /tmp/myfs_mountpoint
488-
src: /tmp/myfs.img
489-
fstype: nullfs
490-
state: mounted
491-
umask: "u=rw,g=r,o=r"
492-
when: ansible_system == 'FreeBSD'
493-
- name: Check status of parent directory of mount point
494-
stat:
495-
path: /tmp/foobar
496-
register: parent_dir_stat
497-
- name: Assert that the parent directory of the mount point has right permission
498-
assert:
499-
that:
500-
- parent_dir_stat['stat']['mode'] == '0644'
501-
- name: Unmount FS to access underlying directory
502-
command: |
503-
umount /tmp/myfs.img
504-
- name: Check status of mount point
505-
stat:
506-
path: /tmp/myfs_mountpoint
507-
register: mount_point_stat
508-
- name: Assert that the mount point has right permission
509-
assert:
510-
that:
511-
- mount_point_stat['stat']['mode'] == '0644'
512406
- name: Remove the test FS
513407
file:
514-
path: '{{ item }}'
408+
path: /tmp/mount_dest
515409
state: absent
516-
loop:
517-
- /tmp/myfs.img
518-
- /tmp/myfs_mountpoint
410+
when: ansible_system not in ('MacOS')

0 commit comments

Comments
 (0)