From db345391e378a8afb54fafa3867e2466daf5a093 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Mon, 24 Mar 2014 15:10:43 -0500 Subject: [PATCH] Fixing ownership when atomic_move is creating a file while sudo'ing Fixes #6647 --- lib/ansible/module_utils/basic.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index afdbdb84ac9..64d536d2627 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -977,6 +977,8 @@ class AnsibleModule(object): if self.selinux_enabled(): context = self.selinux_default_context(dest) + creating = not os.path.exists(dest) + try: # Optimistically try a rename, solves some corner cases and can avoid useless work, throws exception if not atomic. os.rename(src, dest) @@ -1008,6 +1010,9 @@ class AnsibleModule(object): self.cleanup(tmp_dest.name) self.fail_json(msg='Could not replace file: %s to %s: %s' % (src, dest, e)) + if creating and os.getenv("SUDO_USER"): + os.chown(dest, os.getuid(), os.getgid()) + if self.selinux_enabled(): # rename might not preserve context self.set_context_if_different(dest, context, False)