# Permission denied while copy with sudo (Related to issue 2837)

**URL:** <https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158>\
**Category:** Ansible Project\
**Created:** [July 18, 2013, 5:25pm UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158 "2013-07-18T17:25:46Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vincent\_Hardion](https://avatars.discourse-cdn.com/v4/letter/v/da6949/32.png) [@Vincent\_Hardion](https://forum.ansible.com/u/Vincent_Hardion)\
**Post date:** [July 18, 2013, 5:25pm UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/1 "2013-07-18T17:25:46Z")

</div>

Hi everybody,

Another newbie in Ansible …  
And I have a problem of permission with the copy task…

Problem when I launch the playbook :  
ansible-playbook -k -K -i dev all.yml -vvv  
…  
failed: [acomputer.se] =\> {“failed”: true, “item”: “”}

msg: Could not replace file: /home/xxx/.ansible/tmp/ansible-1374162746.23-59345204944449/source to /etc/ntp.conf: [Errno 13] Permission denied: ‘/home/xxx/.ansible/tmp/ansible-1374162746.23-59345204944449/source’

My configuration is:

# role/common/tasks/main.yml

- name: ensure NTP point out to the ntp server  
copy: src=ntp.conf dest=/etc/ntp.conf owner=root group=root mode=0644

# all.yml

- hosts: all  
sudo: yes  
roles:
- common

My investigation shows me some relationship with this issues ([https://github.com/ansible/ansible/issues/2837](https://github.com/ansible/ansible/issues/2837)).

In my case the copy task move (instead of copy) the source file to /etc/.ntp… but the source file is read only for root !!!

I could reproduce the problem with this linux command :

sudo mv /home/xxx/.ansible/tmp/ansible-1374162746.23-59345204944449/source /etc/.ntp.conf.bug

mv: cannot remove `/home/xxx/.ansible/tmp/ansible-1374162746.23-59345204944449/source’: Permission denied

Of course the mod of the file doesn’t allow the write access :

~% ll /home/xxx/.ansible/tmp/ansible-1374162746.23-59345204944449  
total 44  
-rw-r–r-- 1 xxx yyy 38960 Jul 18 18:32 copy  
-rw-r–r-- 1 xxx yyy 1860 Jul 18 17:52 source

Questions :

1. Is there a possibility to change the mod of the source file somehow ?
2. Is there any reason to move the file instead of always copy it (see the patch [fixed case in which move fails after remote user copies file and sudo… · bcoca/ansible@bdeb370 · GitHub](https://github.com/bcoca/ansible/commit/bdeb370d7942119944ee4d04e94391fe6ffe2ff3)) ? because in any case the tempdir will be deleted (I try to patch with success).
3. for myself : Am I the only one ?  
Thank you to have read this long topic.

Best regards,  
Vincent

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 18, 2013, 9:02pm UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/2 "2013-07-18T21:02:00Z")

</div>

Before we dive into questions, as is a good case with all of these kind of reports, please share the version of Ansible you are using.

Sounds like your user might not own ~/.ansible here or something.

Anyway, there’s an outstanding patch from Brian to tweak the code to do renames in a few cases that may fix you up here, but I’m curious about what is unique about your system – not a frequent report I’ve heard about.

---

<div class="post-metadata">

**Author:** ![Vincent\_Hardion](https://avatars.discourse-cdn.com/v4/letter/v/da6949/32.png) [@Vincent\_Hardion](https://forum.ansible.com/u/Vincent_Hardion)\
**Post date:** [July 19, 2013, 8:26am UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/3 "2013-07-19T08:26:37Z")

</div>

Thanks for your answer.  
Effectively with the version of ansible is better. So I tried first with 1.2 (rpm from epel) but it happens with release1.2.1 and even the development branch.

I’m the owner of ~/.ansible. But now you talk about “unique” maybe one thing: the home directory is nfs mount. So when I launch ansible from computer A to computer B, ansible use the same “drive” space.

This pull request can be more explicit : [https://github.com/ansible/ansible/pull/3587](https://github.com/ansible/ansible/pull/3587).

Best regards,  
Vincent

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 19, 2013, 11:59am UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/4 "2013-07-19T11:59:19Z")

</div>

Is NFS mounted with root squash?

Brian Coca

---

<div class="post-metadata">

**Author:** ![Vincent\_Hardion](https://avatars.discourse-cdn.com/v4/letter/v/da6949/32.png) [@Vincent\_Hardion](https://forum.ansible.com/u/Vincent_Hardion)\
**Post date:** [July 19, 2013, 3:05pm UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/5 "2013-07-19T15:05:23Z")

</div>

I’m not enough expert to say yes or no immediately.  
Just let me google how to check that.

BTW Is there a way to change the temp dir?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 19, 2013, 3:07pm UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/6 "2013-07-19T15:07:37Z")

</div>

run mount and it should show the options with which nfs was set up, it is probably the cause of this issue as root generally can do anything on a local box and should (selinux baring) never get a permission denied error.

yes, changing the temp dir will solve this case, there is a env var and an ansible.cfg setting.

---

<div class="post-metadata">

**Author:** ![Vincent\_Hardion](https://avatars.discourse-cdn.com/v4/letter/v/da6949/32.png) [@Vincent\_Hardion](https://forum.ansible.com/u/Vincent_Hardion)\
**Post date:** [July 19, 2013, 3:21pm UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/7 "2013-07-19T15:21:31Z")

</div>

From the exports table the NFS is exported with root\_squash (default behaviour)

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 19, 2013, 3:23pm UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/8 "2013-07-19T15:23:48Z")

</div>

Well, that explains the failure.

As a workaround defining the ansible tmp dir outside of the NFS mount, which you should do anyways. You really wan’t to keep the ansible tmp files local to the machine, specially if you are doing servers in parallel with the same home mounted.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 19, 2013, 3:54pm UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/9 "2013-07-19T15:54:14Z")

</div>

Just for clarification, have you tried the development branch as of about 20 minutes ago?

Some code changes in that regard and want to make sure you’re trying latest before I try to replicate things.

–Michael

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 19, 2013, 3:54pm UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/10 "2013-07-19T15:54:53Z")

</div>

But yes, Brian is correct. The ansible remote temp path should probably be configured and forced to /tmp in this case.

---

<div class="post-metadata">

**Author:** ![Vincent\_Hardion](https://avatars.discourse-cdn.com/v4/letter/v/da6949/32.png) [@Vincent\_Hardion](https://forum.ansible.com/u/Vincent_Hardion)\
**Post date:** [July 19, 2013, 4:30pm UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/11 "2013-07-19T16:30:53Z")

</div>

I have just pull the last version but it is the same behaviour.

~/projects/cfg-maxlab-ansible/etc/ansible% ansible-playbook --version  
ansible-playbook 1.3 (devel f4b1e426ea) last updated 2013/07/19 18:08:52 (GMT +200)

TASK: [ensure NTP point out to the ntp server] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [computerA.se] =\> {“failed”: true, “item”: “”}  
msg: Could not replace file: /home/xxx/.ansible/tmp/ansible-1374250532.29-44727094344629/source to /etc/ntp.conf: [Errno 13] Permission denied: ‘/home/xxx/.ansible/tmp/ansible-1374250532.29-44727094344629/source’

By changing the temp directory it works.

Thank you Michael and Brian.

Vincent

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 19, 2013, 6:23pm UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/12 "2013-07-19T18:23:06Z")

</div>

Ok, thanks, good data. I think we can improve the error message quality and also _possibly_ tech the system to retry at the other location.

---

<div class="post-metadata">

**Author:** ![Vamsi\_krishna1](https://avatars.discourse-cdn.com/v4/letter/v/cc9497/32.png) [@Vamsi\_krishna1](https://forum.ansible.com/u/Vamsi_krishna1)\
**Post date:** [March 9, 2018, 1:03am UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/13 "2018-03-09T01:03:59Z")

</div>

Hi vincent,

mee to facing the same issue…  
changing the tmp directory means, what you mean??

you mean to change that in our hosts???

can u explain litttle bit clear…

---

<div class="post-metadata">

**Author:** ![Vamsi\_krishna1](https://avatars.discourse-cdn.com/v4/letter/v/cc9497/32.png) [@Vamsi\_krishna1](https://forum.ansible.com/u/Vamsi_krishna1)\
**Post date:** [March 9, 2018, 1:05am UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/14 "2018-03-09T01:05:55Z")

</div>

When i am creating directory i am getting permission denied…

ansible version 2.4.3

“msg”: "There was an issue creating /etc/xxxx as requested: [Errno 13] Permission denied: b’/etc/xxxx’

---

<div class="post-metadata">

**Author:** ![tkuratom](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@tkuratom](https://forum.ansible.com/u/tkuratom)\
**Post date:** [March 9, 2018, 5:13am UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/15 "2018-03-09T05:13:46Z")

</div>

There’s a configuration option, remote\_tmp. Setting that to a system directory (like /tmp or /var/tmp ) can fix this error.

---

<div class="post-metadata">

**Author:** ![Vamsi\_krishna1](https://avatars.discourse-cdn.com/v4/letter/v/cc9497/32.png) [@Vamsi\_krishna1](https://forum.ansible.com/u/Vamsi_krishna1)\
**Post date:** [March 9, 2018, 6:06pm UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/16 "2018-03-09T18:06:28Z")

</div>

Hello @Toshio ,

Thanks for your response, i tried and i changed in the ansible.cfg file to remote\_tmp: /var/tmp and still same error…

Yaml file i am using…  
name: using  
hosts: all  
tasks:

- name: creates directory  
file:  
path: /etc/xxxx  
state: directory  
owner: root  
group: root  
mode: 0755

Error i am getting…

“msg”: "There was an issue creating /etc/xxxx as requested: [Errno 13] Permission denied: b’/etc/xxxx’

Any help…

thanks

---

<div class="post-metadata">

**Author:** ![tkuratom](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@tkuratom](https://forum.ansible.com/u/tkuratom)\
**Post date:** [March 10, 2018, 12:28am UTC](https://forum.ansible.com/t/permission-denied-while-copy-with-sudo-related-to-issue-2837/13158/17 "2018-03-10T00:28:54Z")

</div>

Your having a different problem than the person you replied to. Your issue is that you don’t have permission on the directory that you are asking ansible to create (/etc/xxxx ). The original poster was having problems with an internal ansible temporary directory.

For your issue, you need the user you are logging into the remote machine as to have permission to create that directory _or_ you need to use ansible’s become facilities to become a user with sufficient permissions to create the directory.

For instance, on my machines ansible will log into the remote machine as an unprivilrged user and then use sudo with password to become root. Using your playing, I’d invoke ansible with a commandline like this:

ansible-playbook --become --become-user=root -K test.yml
