499

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

freginold
  • 3,946
  • 3
  • 13
  • 28
Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166

26 Answers26

853

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

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
leucos
  • 17,661
  • 1
  • 44
  • 34
  • 48
    If `state=directory`, all immediate subdirectories will be created if they do not exist, since 1.7 they will be created with the supplied permissions. – Alex Oct 06 '16 at 10:05
  • 2
    @Alex `all immediate subdirectories` is confusing, could you define that and give an example? – Jamie Jackson Sep 13 '18 at 20:34
  • 15
    @JamieJackson There is a mistake, should be "all intermediate subdirectories". – Alex Sep 14 '18 at 05:27
  • 3
    @Alex Wouldn't it be more clear to say "parent directories"? – Carolus Sep 06 '20 at 09:12
  • 3
    This answer is minimalist and correct, but some consider it a best practice to explicitly declare owner, group and mode. One of the reasons to do this - even when it seems unnecessary - is because over time your current assumptions fail: a distro/release changes and with it come different umask defaults, or ticket databases can be migrated+deleted (losing track of what command-line arguments deployments/installs declared), and maybe you're not available to answer questions anymore. – Scott Prive Sep 16 '20 at 16:36
257

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.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
Arbab Nazar
  • 22,378
  • 10
  • 76
  • 82
  • 53
    The `recursive` argument makes this much like using `mkdir -p` (for those googling _ansible mkdir -p_). – Micah Elliott Jan 20 '16 at 21:02
  • 4
    I found that it changes child files permissions also... almost like mkdir -p /foo/bar && chmod -R 0775 /foo/bar.. has anyone else observed this with Ansible 2.0.0.2 – ThePracticalOne May 11 '16 at 14:04
  • 8
    The `recurse` parameter doesn't like `mkdir -p`. It recursively set the specified file attributes (applies only to state=directory). If `state=directory`, all immediate subdirectories will be created if they do not exist, since 1.7 they will be created with the supplied permissions. – Alex Oct 06 '16 at 10:03
  • @ThePracticalOne - Yes... using "recurse" behaves exactly the same way `chmod -R` does. That is, if `path` already exists as a dir, and there are files inside it, the `recurse` option will (sometimes unfortunately) also apply those same permissions to the files. This is [by design](https://github.com/ansible/ansible/issues/7839), for better or worse. – Dale C. Anderson Oct 12 '17 at 00:30
  • 1
    To further expand, if `recurse` option is specified with `owner` or `group`, `chmod -R` (equiv.) will be applied. This will become an issue if there is lots of file. For me, I had ~200 application versions in a folder, each with ~35k files, which adds up to ~7M files. The chmod will take so long that it timed out the shared SSH connection. – Eric Wong Aug 26 '20 at 17:45
  • What @ThePracticalOne one says is still an issue as of Ansible 2.9: the mode for all *files* inside the directory will be set to the mode you specify as well. This makes `recurse: yes` practically useless to me, as I want to specify a directory mode with +x, but I don't want all files inside the directory to be made executable. – EM0 Nov 09 '20 at 17:16
32

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 multiple directories in one task
  file:
    path: "{{ item }}"
    state: directory
  loop:
    - /srv/www
    - /dir/foo
    - /dir/bar
Kevin C
  • 4,851
  • 8
  • 30
  • 64
20

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

mikl
  • 23,749
  • 20
  • 68
  • 89
17

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
assylias
  • 321,522
  • 82
  • 660
  • 783
Jayesh.c
  • 179
  • 1
  • 5
10

- name: Create a directory 
  ansible.builtin.file:
    path: /etc/some_directory
    state: directory
    mode: '0755'
Samna Najeeb
  • 181
  • 2
  • 9
9
- 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.

qubsup
  • 1,241
  • 5
  • 15
  • 23
8

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.

Ali Warrich
  • 129
  • 2
  • 6
3

You can use the statement

- name: webfolder - Creates web folder
  file: path=/srv/www state=directory owner=www-data group=www-data mode=0775`
assylias
  • 321,522
  • 82
  • 660
  • 783
Mazi
  • 39
  • 2
3

Just need to put condition to execute task for specific distribution

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

If you want to create a directory in windows:

- name: create folder in Windows
  win_file:
    path: C:\Temp\folder\subfolder
    state: directory

See the win_file module for more information.

Kevin C
  • 4,851
  • 8
  • 30
  • 64
3
enter code here 
- name: creating directory in ansible
  file:
   path: /src/www
   state: directory
   owner: foo

you can refer to ansible documentation

Aditya
  • 340
  • 2
  • 10
3

You can do it as one of the following ways:

Example 1: If Parent Directory already exists:

- name: Create a new directory www at given path 
  ansible.builtin.file:
    path: /srv/www/
    state: directory
    mode: '0755'

Example 2: If Parent Directory does not exist:

- name: Create a new directory www at given path recursively
  ansible.builtin.file:
    path: /srv/www/
    state: directory
    mode: '0755'
    recurse: yes

Here in Example 2, it will recursively create both directories if they are not present.

You can see the Official Documentation for further info on file_module

samnoon
  • 1,340
  • 2
  • 13
  • 23
2

to create directory

ansible host_name -m file -a "dest=/home/ansible/vndir state=directory"
sachin_ur
  • 2,375
  • 14
  • 27
2

We have modules available to create directory , file in ansible

Example

- name: Creates directory
  file:
    path: /src/www
    state: directory
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
2

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 !!!

2

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
Levon
  • 10,408
  • 4
  • 47
  • 42
  • There normaly is no need to check if directory exists, ansible will run the same.Ansible is designed to be indempotent, meaning that repeating same action should lead to same state. Due to that if directory does not exists it will be created, if run a second time it will be kept. – philippe lhardy Jan 28 '23 at 07:34
1
---
- 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.

Tim Groeneveld
  • 8,739
  • 3
  • 44
  • 60
Saurabh
  • 19
  • 3
1

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
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
RegexCracker
  • 53
  • 1
  • 1
  • 6
1

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).

David
  • 5,882
  • 3
  • 33
  • 44
1

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'
1

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

  • Welcome to SO. The question is about creating `/srv/www` path, and your example is about `/tmp/Sabana`, You should try to match your examples to question when possible, and avoid repeating any of the 22 answers. – seshadri_c Sep 24 '20 at 02:56
  • This test is only for trace purpose, as documented in https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html : If directory, all intermediate subdirectories will be created if they do not exist. – philippe lhardy Jan 28 '23 at 07:30
0

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)

0

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 
S.Mandal
  • 172
  • 1
  • 10
-2

To create a directory using Ansible, you can use the file module with the state: directory parameter. Here's an example of how you can do this:


- name: Create a directory
  hosts: localhost
  tasks:
    - name: Create directory
      file:
        path: /path/to/directory
        state: directory

In the above playbook, replace /path/to/directory with the actual path of the directory you want to create.

You can run this playbook using the ansible-playbook command followed by the playbook file name:

ansible-playbook playbook.yml

This playbook will create the specified directory on the localhost. If you want to create a directory on a remote host, replace localhost with the appropriate inventory name or IP address, and make sure Ansible is configured to communicate with that host.

Keep in mind that the file module also allows you to set additional parameters such as ownership (owner and group) and permissions (mode) for the directory. You can check the Ansible documentation for the file module to see all available options.

Sunil
  • 707
  • 1
  • 7
  • 13
-8

here is easier way.

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

Ender Wan
  • 119
  • 5