hitochan
hitochan

Reputation: 1068

Permission denied when deleting directory in Ansible

I have a task which simply creates directory in the home directory of user 'foo' as follows:

---
- name: Create bar directory
  become: yes
  become_user: foo
  file:
    path: "/home/foo/bar"
    state: directory
    owner: foo
    group: foo
    mode: 755

This works fine, but when I try to run a task to delete the directory created by above task, I got the following error:

TASK: [a task | Create bar directory]


failed: [192.168.50.4] => {"failed": true} msg: rmtree failed: [Errno 13] Permission denied: '/home/foo/bar'

FATAL: all hosts have already failed -- aborting

Below is the delete task.

---
- name: Create bar directory
  become: yes
  become_user: foo
  file:
    path: "/home/foo/bar"
    state: absent

I confirmed the created directory is owned by 'foo' so the directory should be able to be deleted by 'foo'. Why am I getting permission denied error?

Upvotes: 4

Views: 7257

Answers (1)

hitochan
hitochan

Reputation: 1068

Although the directory is owned by the correct user, I realized that the permission for the directory was not correctly set, i.e, drwxr-xr-x. The problem was mode: 755, which does not seem to be a problem at all. But I needed to prepend 0 before 755 in order for it to work as expected. Example in official document

Upvotes: 3

Related Questions