From cfff8a3806b0c592f449162a82d7d2c3ce4a9388 Mon Sep 17 00:00:00 2001 From: Hideki Saito Date: Sat, 29 May 2021 20:57:27 +0900 Subject: [PATCH] Modify boot option handling on Linux systems * Address the issue #28 * Modified behavior to set noauto option if boot is 'no' on Linux system * Modified integration test to use filesize module instead of dd Signed-off-by: Hideki Saito --- .../fragments/196_boot_opt_for_linux.yml | 4 ++ plugins/modules/mount.py | 42 +++++++++++++++---- .../integration/targets/mount/tasks/main.yml | 42 ++++++++++++++++++- 3 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/196_boot_opt_for_linux.yml diff --git a/changelogs/fragments/196_boot_opt_for_linux.yml b/changelogs/fragments/196_boot_opt_for_linux.yml new file mode 100644 index 0000000..62afecf --- /dev/null +++ b/changelogs/fragments/196_boot_opt_for_linux.yml @@ -0,0 +1,4 @@ +--- +minor_changes: + - mount - Change behavior of ``boot`` option to set ``noauto`` on Linux nodes + (https://github.com/ansible-collections/ansible.posix/issues/28). diff --git a/plugins/modules/mount.py b/plugins/modules/mount.py index dd7a5c0..5c4970d 100644 --- a/plugins/modules/mount.py +++ b/plugins/modules/mount.py @@ -93,7 +93,10 @@ options: boot: description: - Determines if the filesystem should be mounted on boot. - - Only applies to Solaris systems. + - Only applies to Solaris and Linux systems. + - For Solaris systems, C(true) will set C(yes) as the value of mount at boot + in I(/etc/vfstab). + - For Linux systems, C(true) will add C(noauto) to mount options in I(/etc/fstab). type: bool default: yes backup: @@ -169,6 +172,15 @@ EXAMPLES = r''' opts: rw,sync,hard,intr state: mounted fstype: nfs + +- name: Mount NFS volumes with noauto according to boot option + ansible.posix.mount: + src: 192.168.1.100:/nfs/ssd/shared_data + path: /mnt/shared_data + opts: rw,sync,hard,intr + boot: no + state: mounted + fstype: nfs ''' @@ -227,7 +239,7 @@ def _set_mount_save_old(module, args): old_lines = [] exists = False changed = False - escaped_args = dict([(k, _escape_fstab(v)) for k, v in iteritems(args)]) + escaped_args = dict([(k, _escape_fstab(v)) for k, v in iteritems(args) if k != 'warnings']) new_line = '%(src)s %(name)s %(fstype)s %(opts)s %(dump)s %(passno)s\n' if platform.system() == 'SunOS': @@ -673,7 +685,8 @@ def main(): opts='-', passno='-', fstab=module.params['fstab'], - boot='yes' if module.params['boot'] else 'no' + boot='yes' if module.params['boot'] else 'no', + warnings=[] ) if args['fstab'] is None: args['fstab'] = '/etc/vfstab' @@ -683,7 +696,9 @@ def main(): opts='defaults', dump='0', passno='0', - fstab=module.params['fstab'] + fstab=module.params['fstab'], + boot='yes', + warnings=[] ) if args['fstab'] is None: args['fstab'] = '/etc/fstab' @@ -700,14 +715,27 @@ def main(): linux_mounts = get_linux_mounts(module) if linux_mounts is None: - args['warnings'] = ( - 'Cannot open file /proc/self/mountinfo. ' - 'Bind mounts might be misinterpreted.') + args['warnings'].append('Cannot open file /proc/self/mountinfo.' + ' Bind mounts might be misinterpreted.') # Override defaults with user specified params for key in ('src', 'fstype', 'passno', 'opts', 'dump', 'fstab'): if module.params[key] is not None: args[key] = module.params[key] + if platform.system().lower() == 'linux': + # Linux has 'noauto' as mount opts to handle mount on boot + # So boot option should manage 'noauto' in opts + # TODO: We need to support other system like *BSD that 'noauto' option available + opts = args['opts'].split(',') + if 'noauto' in opts: + args['warnings'].append("Ignore the 'boot' due to 'opts' contains 'noauto'.") + elif not module.params['boot']: + args['boot'] = 'no' + if 'defaults' in opts: + args['warnings'].append("Ignore the 'boot' due to 'opts' contains 'defaults'.") + else: + opts.append('noauto') + args['opts'] = ','.join(opts) # If fstab file does not exist, we first need to create it. This mainly # happens when fstab option is passed to the module. diff --git a/tests/integration/targets/mount/tasks/main.yml b/tests/integration/targets/mount/tasks/main.yml index 7eacc59..44e120b 100644 --- a/tests/integration/targets/mount/tasks/main.yml +++ b/tests/integration/targets/mount/tasks/main.yml @@ -227,11 +227,13 @@ - name: Block to test remounted option block: - name: Create empty file - command: dd if=/dev/zero of=/tmp/myfs.img bs=1048576 count=20 + community.general.filesize: + path: /tmp/myfs.img + size: 20M when: ansible_system in ('Linux') - name: Format FS when: ansible_system in ('Linux') - community.general.system.filesystem: + community.general.filesystem: fstype: ext3 dev: /tmp/myfs.img - name: Mount the FS for the first time @@ -294,3 +296,39 @@ - /tmp/myfs.img - /tmp/myfs when: ansible_system in ('Linux') + +- name: Block to test boot option for Linux + block: + - name: Create empty file + community.general.filesize: + path: /tmp/myfs.img + size: 20M + - name: Format FS + community.general.filesystem: + fstype: ext3 + dev: /tmp/myfs.img + - name: Mount the FS with noauto option + mount: + path: /tmp/myfs + src: /tmp/myfs.img + fstype: ext3 + state: mounted + boot: no + opts: rw,user,async + register: mount_info + - name: assert the mount without noauto was successful + assert: + that: + - mount_info['opts'] == 'rw,user,async,noauto' + - name: Unmount FS + mount: + path: /tmp/myfs + state: absent + - name: Remove the test FS + file: + path: '{{ item }}' + state: absent + loop: + - /tmp/myfs.img + - /tmp/myfs + when: ansible_system in ('Linux')