Commit db345391 by James Cammarata

Fixing ownership when atomic_move is creating a file while sudo'ing

Fixes #6647
parent a4af31e5
...@@ -977,6 +977,8 @@ class AnsibleModule(object): ...@@ -977,6 +977,8 @@ class AnsibleModule(object):
if self.selinux_enabled(): if self.selinux_enabled():
context = self.selinux_default_context(dest) context = self.selinux_default_context(dest)
creating = not os.path.exists(dest)
try: try:
# Optimistically try a rename, solves some corner cases and can avoid useless work, throws exception if not atomic. # Optimistically try a rename, solves some corner cases and can avoid useless work, throws exception if not atomic.
os.rename(src, dest) os.rename(src, dest)
...@@ -1008,6 +1010,9 @@ class AnsibleModule(object): ...@@ -1008,6 +1010,9 @@ class AnsibleModule(object):
self.cleanup(tmp_dest.name) self.cleanup(tmp_dest.name)
self.fail_json(msg='Could not replace file: %s to %s: %s' % (src, dest, e)) 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(): if self.selinux_enabled():
# rename might not preserve context # rename might not preserve context
self.set_context_if_different(dest, context, False) self.set_context_if_different(dest, context, False)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment