Repository: ansible/ansible-modules-core

Issue:
system/mount.py
##### ISSUE TYPE

Bug Report
##### COMPONENT NAME

mount module
##### ANSIBLE VERSION

N/A
##### SUMMARY

This ticket is opened because #8377 on ansible core is closed (and this is a more appropriate repo)

This issue is still present as recently as 1.9.4 (cannot test on newer versions).

It looks as though the mount module calls -o remount blindly instead of calling umount followed by mount.

Example:

```
- name: mount a mount
  mount:
    state: "mounted"
    fstype: "nfs"
    name: "/mnt/foo"
    src: "fizz:/buzz"
    opts: "rsize=8192,wsize=8192,timeo=14,intr,nosuid"
```

Will happily mount your mount, but when you run with

```
- name: mount a mount
  mount:
    state: "mounted"
    fstype: "nfs"
    name: "/mnt/foo"
    src: "fizz:/buzz"
    opts: "timeo=14,intr,nosuid"
```

It will fail with an unsupported mount option error, because remount is not a supported mount option for mount.nfs



Discussion:
[module: system/mount.py]

@jasonritzke Thanks for submitting this bug report.

@ansible/core team -- thanks in advance for taking a look at this bug report and for your ongoing work in maintaining this module.

@grossws, ping. This issue is still waiting on your response.
[click here for bot help](https://github.com/jctanner/ansibullbot/blob/ISSUE_TRIAGE_CLASS/ISSUE_HELP.md)

<!--- boilerplate: issue_renotify_maintainer --->

@grossws, ping. This issue is still waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_renotify_maintainer --->

@ansible, ping. This issue is still waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_renotify_maintainer --->

@ansible, ping. This issue is still waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_renotify_maintainer --->

@ansible, ping. This issue is still waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_renotify_maintainer --->


Resolving patch:
diff --git a/system/mount.py b/system/mount.py
--- a/system/mount.py
+++ b/system/mount.py
@@ -308,6 +308,15 @@ def unset_mount(module, args):
 
     return (args['name'], changed)
 
+def _set_fstab_args(args):
+    result = []
+    if 'fstab' in args and args['fstab'] != '/etc/fstab':
+        if get_platform().lower().endswith('bsd'):
+            result.append('-F')
+        else:
+            result.append('-T')
+        result.append(args['fstab'])
+    return result
 
 def mount(module, args):
     """Mount up a path or remount if needed."""
@@ -317,13 +326,9 @@ def mount(module, args):
     cmd = [mount_bin]
 
     if ismount(name):
-        cmd += ['-o', 'remount']
+        return remount(module, mount_bin, args)
 
-    if args['fstab'] != '/etc/fstab':
-        if get_platform() == 'FreeBSD':
-            cmd += ['-F', args['fstab']]
-        elif get_platform() == 'Linux':
-            cmd += ['-T', args['fstab']]
+    cmd += _set_fstab_args(args)
 
     cmd += [name]
 
@@ -348,6 +353,30 @@ def umount(module, dest):
     else:
         return rc, out+err
 
+def remount(module, mount_bin, args):
+    ''' will try to use -o remount first and fallback to unmount/mount if unsupported'''
+    msg = ''
+    cmd = [mount_bin]
+
+    # multiplatform remount opts
+    if get_platform().lower().endswith('bsd'):
+        cmd += ['-u']
+    else:
+        cmd += ['-o', 'remount' ]
+
+    cmd += _set_fstab_args(args)
+    cmd += [ args['name'], ]
+    try:
+        rc, out, err = module.run_command(cmd)
+    except:
+        rc = 1
+    if rc != 0:
+        msg = out+err
+        if ismount(args['name']):
+            rc,msg = umount(module, args)
+        if rc == 0:
+            rc,msg = mount(module, args)
+    return rc, msg
 
 # Note if we wanted to put this into module_utils we'd have to get permission
 # from @jupeter -- https://github.com/ansible/ansible-modules-core/pull/2923
@@ -650,8 +679,7 @@ def main():
         elif 'bind' in args.get('opts', []):
             changed = True
 
-            if is_bind_mounted(
-                    module, linux_mounts, name, args['src'], args['fstype']):
+            if is_bind_mounted( module, linux_mounts, name, args['src'], args['fstype']):
                 changed = False
 
             if changed and not module.check_mode:


Test patch: