Template and copy modules failing when using su

Template and copy modules fail when using su with the message:

{“msg”: “Could not replace file: /tmp/ansible-tmp-1400646411.03-30275482183570/source to /tmp/ans_test/gsdummy.txt: [Errno 13] Permission denied: ‘/tmp/ansible-tmp-1400646411.03-30275482183570/source’”, “failed”: true}

The problem is in the basic.py atomic_move method. It is trying to move the source to dest using shutil.move. A move wont work for su since the source directory is owned by the remote user.

Line 1037 has a check for sudo(os,getenv(“SUDO_USER”)) and in the case of sudo does a shutil.copy2 instead of shutil.move.

I was able to get it working by always doing shutil.copy2.

I think the fix needs to be adding a check for su and using shutil.copy2 when su is true. I tried to do this, but it appears the environment variable for both SUDO_USER and SU_USER are ‘None’ at this point in the code.

If someone can give me an idea of the right thing to check to identify an su user in atomic_move, I will try to fix this and submit a PR.

Not immediately sure, but probably needs to be done more on a test/try-other-way basis rather than trusting the environment to tell you SUDO is in effect - I recall that being a bit OS specific.

Please make sure there’s a github ticket regardless.

GitHub ticket opened - https://github.com/ansible/ansible/issues/7553