How to Create a Directory using Ansible

0 min read 115 words

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

Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags