Bootstrap FreeKB - Ansible - Resolve "rmtree failed: [Errno 13] Permission denied"
Ansible - Resolve "rmtree failed: [Errno 13] Permission denied"

Updated:   |  Ansible articles

Let's say the following is being returned.

rmtree failed: [Errno 13] Permission denied: '/tmp/example'

 

This is often returned when the file module is used to remove a file or directory.

---
- hosts: localhost
  remote_user: john.doe
  tasks:
  - file:
      path: /tmp/example
      state: absent
...

 

In this scenario, you will first want to determine the ownership and permissions of the file or directory being removed. Notice in this example that john.doe is the owner of the directory and the remote_user parameter was used in the playbook so that john.doe is the remote user.

~]$ ls -ld /tmp/example
drwxrwxr-x 2 john.doe john.doe 6 Oct  6 00:15 /tmp/example

 

In this scenario, you'll want to determine if there are any files or directories below the directory being removed that are owned by some other user. Notice in this example that the .. directory is owned by root.

~]$ ls -lisa /tmp/example
 4195662  4 drwxrwxr-x. 24 john.doe john.doe 4096 Oct  4 21:08 .
      64  0 drwxr-xr-x. 19 root     root      263 Oct  6 01:00 ..

 

One solution here would be to set remote_user to root.

---
- hosts: localhost
  remote_user: root
  tasks:
  - file:
      path: /tmp/example
      state: absent
...

 

Or use become with the file module. In this scenario, you may also need to use the --ask-become-pass command line flag.

---
- hosts: localhost
  remote_user: john.doe
  tasks:
  - file:
      path: /tmp/example
      state: absent
     become: yes
     become_user: root
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 57eaf3 in the box below so that we can be sure you are a human.