How to create a directory using Ansible

DirectoryFilesystemsAnsible

Directory Problem Overview


How do you create a directory www at /srv on a Debian-based system using an Ansible playbook?

Directory Solutions


Solution 1 - Directory

You want the file module. To create a directory, you need to specify the option state=directory :

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

You can see other options at https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html

Solution 2 - Directory

You can even extend the file module and even set the owner,group & permission through it. (Ref: Ansible file documentation)

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

Even, you can create the directories recursively:

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

This way, it will create both directories, if they didn't exist.

Solution 3 - Directory

Additional for all answers here, there is lot of situations when you need to create more then one directory so it is a good idea to use loops instead creating separate task for each directory.

- name: Creates directory
  file:
    path: "{{ item }}"
    state: directory
  with_items:
  - /srv/www
  - /dir/foo
  - /dir/bar

Solution 4 - Directory

you can create using:

##Latest version 2<

- name: Create Folder
  file: 
    path: /srv/www/
    owner: user 
    group: user 
    mode: 0755 
    state: directory

##Older version

- name: Create Folder
  file: 
   path=/srv/www/
   owner=user 
   group=user 
   mode=0755 
   state=directory

Refer - http://docs.ansible.com/ansible/file_module.html

Solution 5 - Directory

Directory can be created using file module only, as directory is nothing but a file.

# create a directory if it doesn't exist
- file:
    path: /etc/some_directory
    state: directory
    mode: 0755
    owner: foo
    group: foo

Solution 6 - Directory

- file:
    path: /etc/some_directory
    state: directory
    mode: 0755
    owner: someone
    group: somegroup

That's the way you can actually also set the permissions, the owner and the group. The last three parameters are not obligatory.

Solution 7 - Directory

You can create a directory. using

# create a directory if it doesn't exist
- file: path=/src/www state=directory mode=0755

You can also consult http://docs.ansible.com/ansible/file_module.html for further details regaridng directory and file system.

Solution 8 - Directory

- name: Create a directory 
  ansible.builtin.file:
    path: /etc/some_directory
    state: directory
    mode: '0755'

Solution 9 - Directory

You can use the statement

- name: webfolder - Creates web folder
  file: path=/srv/www state=directory owner=www-data group=www-data mode=0775`

Solution 10 - Directory

Just need to put condition to execute task for specific distribution

- name: Creates directory
  file: path=/src/www state=directory
  when: ansible_distribution == 'Debian'

Solution 11 - Directory

If you want to create directory in windows:

> - name: Create directory structure
> win_file:
> path: C:\Temp\folder\subfolder>
> state: directory

Solution 12 - Directory

enter code here 
- name: creating directory in ansible
  file:
   path: /src/www
   state: directory
   owner: foo

you can refer to ansible documentation

Solution 13 - Directory

We have modules available to create directory , file in ansible

Example

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

Solution 14 - Directory

you can use the "file" module in this case, there are so many arguments that you can pass for a newly created directory like the owner, group, location, mode and so on.....

please refer to this document for the detailed explanation on the file module...

https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module

remember this module is not just for creating the directory !!!

Solution 15 - Directory

---
- hosts: all
  connection: local
  tasks:
    - name: Creates directory
      file: path=/src/www state=directory

Above playbook will create www directory in /src path.

Before running above playbook. Please make sure your ansible host connection should be set,

"localhost ansible_connection=local"

should be present in /etc/ansible/hosts

for more information please let me know.

Solution 16 - Directory

You can directly run the command and create directly using ansible

ansible -v targethostname -m shell -a "mkdir /srv/www" -u targetuser

OR

ansible -v targethostname -m file -a "path=/srv/www state=directory" -u targetuser

Solution 17 - Directory

to create directory

ansible host_name -m file -a "dest=/home/ansible/vndir state=directory"

Solution 18 - Directory

Use file module to create a directory and get the details about file module using command "ansible-doc file"

Here is an option "state" that explains:

> If directory, all immediate subdirectories will be created if they do not exist, since 1.7 they will be created with the supplied permissions.
> If file, the file will NOT be created if it does not exist, see the [copy] or [template] module if you want that behavior.
> If link, the symbolic link will be created or changed. Use hard for hardlinks.
> If absent, directories will be recursively deleted, and files or symlinks will be unlinked.

>Note that file will not fail if the path does not exist as the state did not change.
> > If touch (new in 1.4), an empty file will be created if the path does not > exist, while an existing file or directory will receive updated file > access and modification times (similar to the way touch works from > the command line).

Solution 19 - Directory

Easiest way to make a directory in Ansible.

  • name: Create your_directory if it doesn't exist. file: path: /etc/your_directory

OR

You want to give sudo privileges to that directory.

  • name: Create your_directory if it doesn't exist. file: path: /etc/your_directory mode: '777'

Solution 20 - Directory

To check if directory exists and then run some task (e.g. create directory) use the following

- name: Check if output directory exists
    stat:
    path: /path/to/output
    register: output_folder

- name: Create output directory if not exists
    file:
    path: /path/to/output
    state: directory
    owner: user
    group: user
    mode: 0775
    when: output_folder.stat.exists == false

Solution 21 - Directory

Hello good afternoon team.

I share the following with you.

   - name: Validar Directorio
     stat:
       path: /tmp/Sabana
     register: sabana_directorio
   
   - debug:
       msg: "Existe"
     when: sabana_directorio.stat.isdir == sabana_directorio.stat.isdir

   - name: Crear el directorio si no existe.
     file:
       path: /tmp/Sabana
       state: directory
     when: sabana_directorio.stat.exists == false

With which you can validate if the directory exists before creating it

Solution 22 - Directory

I see lots of Playbooks examples and I would like to mention the Adhoc commands example.

$ansible -i inventory -m file -a "path=/tmp/direcory state=directory ( instead of directory we can mention touch to create files)

Solution 23 - Directory

You need to use file module for this case. Below playbook you can use for your reference.

    ---
     - hosts: <Your target host group>
       name: play1
       tasks: 
        - name: Create Directory
          files:
           path=/srv/www/
           owner=<Intended User>
           mode=<Intended permission, e.g.: 0750>
           state=directory 

Solution 24 - Directory

here is easier way.

- name: create dir command: mkdir -p dir dir/a dir/b

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGaurav AgarwalView Question on Stackoverflow
Solution 1 - DirectoryleucosView Answer on Stackoverflow
Solution 2 - DirectoryArbab NazarView Answer on Stackoverflow
Solution 3 - DirectoryAndrey BistrininView Answer on Stackoverflow
Solution 4 - DirectoryChathuranga AbeyrathnaView Answer on Stackoverflow
Solution 5 - Directoryuser3004224View Answer on Stackoverflow
Solution 6 - DirectoryqubsupView Answer on Stackoverflow
Solution 7 - DirectoryAli WarrichView Answer on Stackoverflow
Solution 8 - DirectorySamna NajeebView Answer on Stackoverflow
Solution 9 - DirectoryMaziView Answer on Stackoverflow
Solution 10 - DirectoryBhavesh VasoyaView Answer on Stackoverflow
Solution 11 - DirectoryAlexandru MuicaView Answer on Stackoverflow
Solution 12 - DirectoryAdityaView Answer on Stackoverflow
Solution 13 - DirectoryMohd Umar MubeenView Answer on Stackoverflow
Solution 14 - Directoryuser11345576View Answer on Stackoverflow
Solution 15 - DirectorySaurabhView Answer on Stackoverflow
Solution 16 - DirectoryRegexCrackerView Answer on Stackoverflow
Solution 17 - Directorysachin_urView Answer on Stackoverflow
Solution 18 - DirectoryPriyanka BhardwajView Answer on Stackoverflow
Solution 19 - DirectoryMuhammad AhmadView Answer on Stackoverflow
Solution 20 - DirectoryLevonView Answer on Stackoverflow
Solution 21 - DirectoryJavier GaticaView Answer on Stackoverflow
Solution 22 - DirectoryKandasamy MuruganView Answer on Stackoverflow
Solution 23 - DirectoryS.MANDALView Answer on Stackoverflow
Solution 24 - DirectoryEnder WanView Answer on Stackoverflow