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)