summaryrefslogtreecommitdiff
path: root/lib/ansible/module_utils/basic.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ansible/module_utils/basic.py')
-rw-r--r--lib/ansible/module_utils/basic.py38
1 files changed, 19 insertions, 19 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)