The file module with state: directory is used to create a directory on a managed node (e.g. target system). Or, the local_action module can be used to invoke this module on the control node (that's your Ansible server).
The ansible-doc file command can be used to show the Ansible documention on the file module.
In this example, the /tmp/example directory will be created if it does not exist.
- name: "mkdir /tmp/example" file: path: "/tmp/example" state: "directory"
If the directory was successfully created, the play should indicate changed.
TASK [mkdir /tmp/example] changed: [server1.example.com]
Directory not created
If the directory was not created, the play should indicate ok. This is the expected behavior if the directory already exists. If the directory already exists, the directory will not be created, overwritten, or changed.
TASK [mkdir /tmp/example] ok: [server1.example.com]
The file module accepts the following parameters.
Here is an example of how these parameters would be used.
- name: "mkdir /tmp/example" file: path: "/tmp/example" state: "directory" owner: "foo" group: "foo" mode: "0644" seuser: "unconfined_u" serole: "object_r" setype: "httpd_sys_content_r" selevel: "s0"
If permission denied is returned when running the play, refer to our article on resolving permission denied.
You may also want to use x.stat.exists to determine if the directory exists, and x.stat.isdir to determine if the object is a directory.