Skip to content

Commit

Permalink
Fixing ownership when atomic_move is creating a file while sudo'ing
Browse files Browse the repository at this point in the history
  • Loading branch information
jimi-c committed Mar 24, 2014
1 parent a4af31e commit db34539
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/ansible/module_utils/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,8 @@ def atomic_move(self, src, dest):
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)
Expand Down Expand Up @@ -1008,6 +1010,9 @@ def atomic_move(self, src, dest):
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)
Expand Down

0 comments on commit db34539

Please sign in to comment.