Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py
  2. index 0f25c6c..e4b8cae 100644
  3. --- a/lib/ansible/module_utils/basic.py
  4. +++ b/lib/ansible/module_utils/basic.py
  5. @@ -2241,7 +2241,7 @@ class AnsibleModule(object):
  6. backupdest = '%s.%s.%s' % (fn, os.getpid(), ext)
  7.  
  8. try:
  9. - shutil.copy2(fn, backupdest)
  10. + self.atomic_copy(fn, backupdest)
  11. except (shutil.Error, IOError):
  12. e = get_exception()
  13. self.fail_json(msg='Could not make backup of %s to %s: %s' % (fn, backupdest, e))
  14. @@ -2256,6 +2256,25 @@ class AnsibleModule(object):
  15. e = get_exception()
  16. sys.stderr.write("could not cleanup %s: %s" % (tmpfile, e))
  17.  
  18. + def atomic_copy(self, src, dest):
  19. + """Copy a file with preserved ownership, permissions and context"""
  20. +
  21. + shutil.copy2(src, dest)
  22. +
  23. + if self.selinux_enabled():
  24. + context = self.selinux_context(src)
  25. + self.set_context_if_different(dest, context, False)
  26. +
  27. + try:
  28. + dest_stat = os.stat(src)
  29. + tmp_stat = os.stat(dest)
  30. + if dest_stat and (tmp_stat.st_uid != dest_stat.st_uid or tmp_stat.st_gid != dest_stat.st_gid):
  31. + os.chown(dest, dest_stat.st_uid, dest_stat.st_gid)
  32. + except OSError:
  33. + e = get_exception()
  34. + if e.errno != errno.EPERM:
  35. + raise
  36. +
  37. def atomic_move(self, src, dest, unsafe_writes=False):
  38. '''atomically move src to dest, copying attributes from dest, returns true on success
  39. 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