From fd2116e26ab6d239c78c0507817a34f106e567c1 Mon Sep 17 00:00:00 2001 From: Raul Mahiques Date: Fri, 19 Jul 2019 08:59:27 +0200 Subject: [PATCH] Added state "remount" which will remount the device (#52649) --- changelogs/fragments/mount.yml | 2 + lib/ansible/modules/system/mount.py | 15 ++++- test/integration/targets/mount/tasks/main.yml | 64 +++++++++++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/mount.yml diff --git a/changelogs/fragments/mount.yml b/changelogs/fragments/mount.yml new file mode 100644 index 00000000000..4c3847549ac --- /dev/null +++ b/changelogs/fragments/mount.yml @@ -0,0 +1,2 @@ +minor_changes: + - Added a parameter to allow remounting a filesystem diff --git a/lib/ansible/modules/system/mount.py b/lib/ansible/modules/system/mount.py index bc3bb94b311..1d14561581d 100644 --- a/lib/ansible/modules/system/mount.py +++ b/lib/ansible/modules/system/mount.py @@ -74,9 +74,12 @@ options: - C(absent) specifies that the device mount's entry will be removed from I(fstab) and will also unmount the device and remove the mount point. + - C(remounted) specifies that the device will be remounted for when you + want to force a refresh on the mount itself (added in 2.9). This will + always return changed=true. type: str required: true - choices: [ absent, mounted, present, unmounted ] + choices: [ absent, mounted, present, unmounted, remounted ] fstab: description: - File to use instead of C(/etc/fstab). @@ -597,7 +600,7 @@ def main(): passno=dict(type='str'), src=dict(type='path'), backup=dict(type='bool', default=False), - state=dict(type='str', required=True, choices=['absent', 'mounted', 'present', 'unmounted']), + state=dict(type='str', required=True, choices=['absent', 'mounted', 'present', 'unmounted', 'remounted']), ), supports_check_mode=True, required_if=( @@ -730,6 +733,14 @@ def main(): module.fail_json(msg="Error mounting %s: %s" % (name, msg)) elif state == 'present': name, changed = set_mount(module, args) + elif state == 'remounted': + if not module.check_mode: + res, msg = remount(module, args) + + if res: + module.fail_json(msg="Error remounting %s: %s" % (name, msg)) + + changed = True else: module.fail_json(msg='Unexpected position reached') diff --git a/test/integration/targets/mount/tasks/main.yml b/test/integration/targets/mount/tasks/main.yml index e8b7fdfbf8d..6bc126e37d5 100644 --- a/test/integration/targets/mount/tasks/main.yml +++ b/test/integration/targets/mount/tasks/main.yml @@ -276,3 +276,67 @@ - "' 0 0' in optional_fields_content.stdout" - "1 == optional_fields_content.stdout_lines | length" when: ansible_system in ('Linux') + +- name: Block to test remounted option + block: + - name: Create empty file + command: dd if=/dev/zero of=/tmp/myfs.img bs=1048576 count=20 + when: ansible_system in ('Linux') + + - name: Format FS + filesystem: + fstype: ext3 + dev: /tmp/myfs.img + when: ansible_system in ('Linux') + + - name: Mount the FS for the first time + mount: + path: /tmp/myfs + src: /tmp/myfs.img + fstype: ext2 + state: mounted + when: ansible_system in ('Linux') + + - name: Get the last write time + shell: "dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i last\ write\ time: |cut -d: -f2-" + register: last_write_time + when: ansible_system in ('Linux') + + - name: Wait 2 second + pause: + seconds: 2 + when: ansible_system in ('Linux') + + - name: Test if the FS is remounted + mount: + path: /tmp/myfs + state: remounted + when: ansible_system in ('Linux') + + - name: Get again the last write time + shell: "dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i last\ write\ time: |cut -d: -f2-" + register: last_write_time2 + when: ansible_system in ('Linux') + + - name: Fail if they are the same + fail: + msg: "Filesytem was not remounted, testing of the module failed!" + when: last_write is defined and last_write_time2 is defined and last_write_time.stdout == last_write_time2.stdout and ansible_system in ('Linux') + always: + - name: Umount the test FS + mount: + path: /tmp/myfs + src: /tmp/myfs.img + opts: loop + state: absent + when: ansible_system in ('Linux') + + - name: Remove the test FS + file: + path: "{{ item }}" + state: absent + loop: + - /tmp/myfs.img + - /tmp/myfs + when: ansible_system in ('Linux') +...