Permission denied may be displayed when attempting to do various tasks, such as using the file module to create a directory. In this example, permission denied is returned when user john.doe attempts to create the /opt/foo directory.
PLAY [all]
TASKS [mkdir /opt/foo]
fatal: [server1.example.com]: FAILED! => {"changed": false, "msg": "There was an issue creating /opt/foo as requested: [Errno 13] Permission denied: '/opt/foo'", "path": "/opt/foo"}
PLAY RECAP
server1.example.com : ok=0 changed=0 unreachable=0 failed=1
Let's say you are attempting to create the /opt/foo directory as john.doe.
---
- hosts: all
tasks:
- name: "mkdir /opt/foo"
file:
path: "/opt/foo"
state: "directory"
This could be done by setting john.doe as the ansible_user in the /etc/ansible/hosts file, like this.
ansible_user=john.doe
Or by setting remote_user: john.doe in the playbook, like this.
---
- hosts: all
remote_user: "john.doe"
tasks:
- name: "mkdir /opt/foo"
file:
path: "/opt/foo"
state: "directory"
Let's also say that only root has write access to the /opt directory on the managed node (e.g. target server), like this.
drwxr-xr-x 1 root root 4096 Sep 18 13:49
In this scenario, running the play will return Permission denied. This is the expected result, since john.doe does not have permission to write to the /opt directory. To resolve this, the become module can be used to become root.
PLAY [all]
TASKS [mkdir /opt/foo]
fatal: [server1.example.com]: FAILED! => {"changed": false, "msg": "There was an issue creating /opt/foo as requested: [Erroro 13] Permission denied: '/opt/foo'", "path": "/opt/foo"}
PLAY RECAP
server1.example.com : ok=0 changed=0 unreachable=0 failed=1
Did you find this article helpful?
If so, consider buying me a coffee over at