diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ansible/module_utils/basic.py | 38 | ||||
-rw-r--r-- | lib/ansible/modules/net_tools/basics/get_url.py | 2 |
2 files changed, 20 insertions, 20 deletions
diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index adb95e9706..a531f3032c 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -228,6 +228,15 @@ _literal_eval = literal_eval # is an internal implementation detail _ANSIBLE_ARGS = None + +def env_fallback(*args, **kwargs): + ''' Load value from environment ''' + for arg in args: + if arg in os.environ: + return os.environ[arg] + raise AnsibleFallbackNotFound + + FILE_COMMON_ARGUMENTS = dict( # These are things we want. About setting metadata (mode, ownership, permissions in general) on # created files (these are used by set_fs_attributes_if_different and included in @@ -255,7 +264,7 @@ FILE_COMMON_ARGUMENTS = dict( regexp=dict(), # used by assemble delimiter=dict(), # used by assemble directory_mode=dict(), # used by copy - unsafe_writes=dict(type='bool'), # should be available to any module using atomic_move + unsafe_writes=dict(type='bool', default=False, fallback=(env_fallback, ['ANSIBLE_UNSAFE_WRITES'])), # should be available to any module using atomic_move ) PASSWD_ARG_RE = re.compile(r'^[-]{0,2}pass[-]?(word|wd)?') @@ -641,14 +650,6 @@ def _load_params(): sys.exit(1) -def env_fallback(*args, **kwargs): - ''' Load value from environment ''' - for arg in args: - if arg in os.environ: - return os.environ[arg] - raise AnsibleFallbackNotFound - - def missing_required_lib(library, reason=None, url=None): hostname = platform.node() msg = "Failed to import the required Python library (%s) on %s's Python %s." % (library, hostname, sys.executable) @@ -2357,8 +2358,7 @@ class AnsibleModule(object): if e.errno not in [errno.EPERM, errno.EXDEV, errno.EACCES, errno.ETXTBSY, errno.EBUSY]: # only try workarounds for errno 18 (cross device), 1 (not permitted), 13 (permission denied) # and 26 (text file busy) which happens on vagrant synced folders and other 'exotic' non posix file systems - self.fail_json(msg='Could not replace file: %s to %s: %s' % (src, dest, to_native(e)), - exception=traceback.format_exc()) + self.fail_json(msg='Could not replace file: %s to %s: %s' % (src, dest, to_native(e)), exception=traceback.format_exc()) else: # Use bytes here. In the shippable CI, this fails with # a UnicodeError with surrogateescape'd strings for an unknown @@ -2368,14 +2368,13 @@ class AnsibleModule(object): error_msg = None tmp_dest_name = None try: - tmp_dest_fd, tmp_dest_name = tempfile.mkstemp(prefix=b'.ansible_tmp', - dir=b_dest_dir, suffix=b_suffix) + tmp_dest_fd, tmp_dest_name = tempfile.mkstemp(prefix=b'.ansible_tmp', dir=b_dest_dir, suffix=b_suffix) except (OSError, IOError) as e: error_msg = 'The destination directory (%s) is not writable by the current user. Error was: %s' % (os.path.dirname(dest), to_native(e)) except TypeError: # We expect that this is happening because python3.4.x and - # below can't handle byte strings in mkstemp(). Traceback - # would end in something like: + # below can't handle byte strings in mkstemp(). + # Traceback would end in something like: # file = _os.path.join(dir, pre + name + suf) # TypeError: can't concat bytes to str error_msg = ('Failed creating tmp file for atomic move. This usually happens when using Python3 less than Python3.5. ' @@ -2419,11 +2418,12 @@ class AnsibleModule(object): self._unsafe_writes(b_tmp_dest_name, b_dest) else: self.fail_json(msg='Unable to make %s into to %s, failed final rename from %s: %s' % - (src, dest, b_tmp_dest_name, to_native(e)), - exception=traceback.format_exc()) + (src, dest, b_tmp_dest_name, to_native(e)), exception=traceback.format_exc()) except (shutil.Error, OSError, IOError) as e: - self.fail_json(msg='Failed to replace file: %s to %s: %s' % (src, dest, to_native(e)), - exception=traceback.format_exc()) + if unsafe_writes: + self._unsafe_writes(b_src, b_dest) + else: + self.fail_json(msg='Failed to replace file: %s to %s: %s' % (src, dest, to_native(e)), exception=traceback.format_exc()) finally: self.cleanup(b_tmp_dest_name) diff --git a/lib/ansible/modules/net_tools/basics/get_url.py b/lib/ansible/modules/net_tools/basics/get_url.py index 3c84cb60e9..2c9e95e415 100644 --- a/lib/ansible/modules/net_tools/basics/get_url.py +++ b/lib/ansible/modules/net_tools/basics/get_url.py @@ -620,7 +620,7 @@ def main(): if backup: if os.path.exists(dest): backup_file = module.backup_local(dest) - module.atomic_move(tmpsrc, dest) + module.atomic_move(tmpsrc, dest, unsafe_writes=module.params['unsafe_writes']) except Exception as e: if os.path.exists(tmpsrc): os.remove(tmpsrc) |