Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py
- index 0f25c6c..e4b8cae 100644
- --- a/lib/ansible/module_utils/basic.py
- +++ b/lib/ansible/module_utils/basic.py
- @@ -2241,7 +2241,7 @@ class AnsibleModule(object):
- backupdest = '%s.%s.%s' % (fn, os.getpid(), ext)
- try:
- - shutil.copy2(fn, backupdest)
- + self.atomic_copy(fn, backupdest)
- except (shutil.Error, IOError):
- e = get_exception()
- self.fail_json(msg='Could not make backup of %s to %s: %s' % (fn, backupdest, e))
- @@ -2256,6 +2256,25 @@ class AnsibleModule(object):
- e = get_exception()
- sys.stderr.write("could not cleanup %s: %s" % (tmpfile, e))
- + def atomic_copy(self, src, dest):
- + """Copy a file with preserved ownership, permissions and context"""
- +
- + shutil.copy2(src, dest)
- +
- + if self.selinux_enabled():
- + context = self.selinux_context(src)
- + self.set_context_if_different(dest, context, False)
- +
- + try:
- + dest_stat = os.stat(src)
- + tmp_stat = os.stat(dest)
- + if dest_stat and (tmp_stat.st_uid != dest_stat.st_uid or tmp_stat.st_gid != dest_stat.st_gid):
- + os.chown(dest, dest_stat.st_uid, dest_stat.st_gid)
- + except OSError:
- + e = get_exception()
- + if e.errno != errno.EPERM:
- + raise
- +
- def atomic_move(self, src, dest, unsafe_writes=False):
- '''atomically move src to dest, copying attributes from dest, returns true on success
- it uses os.rename to ensure this as it is an atomic operation, rest of the function is
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement