From 8f778a83dfaaea142663b878bafd84be3afee3f6 Mon Sep 17 00:00:00 2001 From: Till Maas Date: Thu, 20 Mar 2014 12:30:55 +0100 Subject: [PATCH] module_utils/atomic_move(): Restore owner/group Manually restore owner and group if both src and dest are on a separate file system. --- lib/ansible/module_utils/basic.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 42b9d3d669b..c589f92612a 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -977,11 +977,16 @@ class AnsibleModule(object): try: # leaves tmp file behind when sudo and not root if os.getenv("SUDO_USER") and os.getuid() != 0: # cleanup will happen by 'rm' of tempdir - shutil.copy(src, tmp_dest) + # copy2 will preserve some metadata + shutil.copy2(src, tmp_dest) else: shutil.move(src, tmp_dest) if self.selinux_enabled(): - self.set_context_if_different(tmp_dest, context, False) + self.set_context_if_different( + tmp_dest, context, False) + # Reset owners, they are not preserved by shutil.copy2(), which + # is what shutil.move() falls back to. + os.chown(tmp_dest, st.st_uid, st.st_gid) os.rename(tmp_dest, dest) except (shutil.Error, OSError, IOError), e: self.cleanup(tmp_dest)