diff --git a/lib/ansible/modules/system/mount.py b/lib/ansible/modules/system/mount.py index 1d14561581d..d77a1ee5bef 100644 --- a/lib/ansible/modules/system/mount.py +++ b/lib/ansible/modules/system/mount.py @@ -662,8 +662,12 @@ def main(): if not os.path.exists(args['fstab']): if not os.path.exists(os.path.dirname(args['fstab'])): os.makedirs(os.path.dirname(args['fstab'])) - - open(args['fstab'], 'a').close() + try: + open(args['fstab'], 'a').close() + except PermissionError as e: + module.fail_json(msg="Failed to open %s due to permission issue" % args['fstab']) + except Exception as e: + module.fail_json(msg="Failed to open %s due to %s" % (args['fstab'], to_native(e))) # absent: # Remove from fstab and unmounted. @@ -706,6 +710,9 @@ def main(): changed = True elif state == 'mounted': + if not os.path.exists(args['src']): + module.fail_json(msg="Unable to mount %s as it does not exist" % args['src']) + if not os.path.exists(name) and not module.check_mode: try: os.makedirs(name)