# Template and copy modules failing when using su

**URL:** <https://forum.ansible.com/t/template-and-copy-modules-failing-when-using-su/15284>\
**Category:** Ansible Developer\
**Created:** [May 21, 2014, 5:16am UTC](https://forum.ansible.com/t/template-and-copy-modules-failing-when-using-su/15284 "2014-05-21T05:16:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![George\_Simpson1](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@George\_Simpson1](https://forum.ansible.com/u/George_Simpson1)\
**Post date:** [May 21, 2014, 5:16am UTC](https://forum.ansible.com/t/template-and-copy-modules-failing-when-using-su/15284/1 "2014-05-21T05:16:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 25, 2014, 9:22pm UTC](https://forum.ansible.com/t/template-and-copy-modules-failing-when-using-su/15284/2 "2014-05-25T21:22:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![George\_Simpson1](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@George\_Simpson1](https://forum.ansible.com/u/George_Simpson1)\
**Post date:** [May 26, 2014, 8:03pm UTC](https://forum.ansible.com/t/template-and-copy-modules-failing-when-using-su/15284/3 "2014-05-26T20:03:29Z")

</div>

GitHub ticket opened - [https://github.com/ansible/ansible/issues/7553](https://github.com/ansible/ansible/issues/7553)
