How do you create a directory www at /srv on a Debian-based system using an Ansible playbook?
26 Answers
You want the ansible.builtin.file module. To create a directory, you need to specify the option state: directory:
- name: Creates directory
ansible.builtin.file:
path: /src/www
state: directory
You can see other options at https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html
6 Comments
state=directory, all immediate subdirectories will be created if they do not exist, since 1.7 they will be created with the supplied permissions.all immediate subdirectories is confusing, could you define that and give an example?You can even extend the file module and even set the owner,group & permission through it. (Ref: Ansible file documentation)
- name: Creates directory
ansible.builtin.file:
path: /src/www
state: directory
owner: www-data
group: www-data
mode: 0775
Even, you can create the directories recursively:
- name: Creates directory
ansible.builtin.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.
6 Comments
recursive argument makes this much like using mkdir -p (for those googling ansible mkdir -p).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.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, for better or worse.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.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
ansible.builtin.file:
path: "{{ item }}"
state: directory
loop:
- /srv/www
- /dir/foo
- /dir/bar
Comments
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
Comments
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.
Comments
enter code here
- name: creating directory in ansible
file:
path: /src/www
state: directory
owner: foo
you can refer to ansible documentation
Comments
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
Comments
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.
Comments
to create directory
ansible host_name -m file -a "dest=/home/ansible/vndir state=directory"
1 Comment
ansible -b -i /etc/ansible/inv/yaml/ -m file -a "path=/etc/mongod state=directory mode=700 owner=mongod group=mongod" mngyou 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 !!!
Comments
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
1 Comment
---
- 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.
1 Comment
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.
Iffile, the file will NOT be created if it does not exist, see the [copy] or [template] module if you want that behavior.
Iflink, the symbolic link will be created or changed. Usehardfor hardlinks.
Ifabsent, directories will be recursively deleted, and files or symlinks will be unlinked.Note that
filewill 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 waytouchworks from the command line).
Comments
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 Comment
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
2 Comments
/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.The use of file module has been told a lot in the previous comments. It creates the needed directory, along with the missing intermediate directories.
But the name /src/www suggests that it will be used as html source for httpd or nginx. Will AppArmor let you use this dir without further configuration? I ask because on RedHat-like systems, running SELinux, you have to register this non-standard directory as html source or the OS will refuse to use it.
Comments
here is easier way.
- name: create dir
command: mkdir -p dir dir/a dir/b
4 Comments
-p it is idempotentcommand module should only be used if no other module is sufficient for the desired task.