From 4ea12c1b86915620b515fb5530cc9f8bb4f1de67 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Tue, 25 Mar 2014 13:00:38 -0500 Subject: [PATCH] Make sure stat of dest is available in atomic_move Fixes #6682 --- lib/ansible/module_utils/basic.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 2dcb9cd5458..4bdb3fc2cc5 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -966,11 +966,12 @@ class AnsibleModule(object): it uses os.rename to ensure this as it is an atomic operation, rest of the function is to work around limitations, corner cases and ensure selinux context is saved if possible''' context = None + dest_stat = None if os.path.exists(dest): try: - st = os.stat(dest) - os.chmod(src, st.st_mode & 07777) - os.chown(src, st.st_uid, st.st_gid) + dest_stat = os.stat(dest) + os.chmod(src, dest_stat.st_mode & 07777) + os.chown(src, dest_stat.st_uid, dest_stat.st_gid) except OSError, e: if e.errno != errno.EPERM: raise @@ -1005,9 +1006,8 @@ class AnsibleModule(object): if self.selinux_enabled(): self.set_context_if_different( tmp_dest.name, context, False) - # Reset owners, they are not preserved by shutil.copy2(), which - # is what shutil.move() falls back to. - os.chown(tmp_dest.name, st.st_uid, st.st_gid) + if dest_stat: + os.chown(tmp_dest.name, dest_stat.st_uid, dest_stat.st_gid) os.rename(tmp_dest.name, dest) except (shutil.Error, OSError, IOError), e: self.cleanup(tmp_dest.name)