How to Create a Directory Using Ansible


If you need to create a directory using Ansible, then you can do the following:

Create a Directory in Ansible

You will need the file module, then to create a directory you simply specify the option state=directory:

- name: Creates a directory
  file:
    path: /src/www
    state: directory

Note that with state=directory, all the immediate subdirectories will be created if they don’t already exist.

Extending the file module

- name: Creates a directory
  file:
    path: /src/www
    state: directory
    owner: www-data
    group: www-data
    mode: 0775

Create the Directories Recursively

- name: Creates directory
  file:
    path: /src/www
    state: directory
    owner: www-data
    group: www-data
    mode: 0775
    recurse: yes

This is similar to the recursive argument used with mkdir -p