Updated April 12, 2023
Introduction to Ansible Create Directory
In Ansible, we have several modules that are used for various purposes. We can use those for our needs using various parameters available with them. Also, we can create one module in a scripting language like python and use it accordingly. In this topic, we are going to learn about Ansible Create Directory.
One of the basic and important modules is the file module, which is used for creating, removing, updating the permission of files and directories on remote hosts. Also, the same tasks can be done using the command module by giving commands as parameters. In this article, we will learn about it and using it to create directories on remote hosts.
Directory of Ansible Create
Creating a directory is a day to day operation. This needs to be performed when doing work like installing the application, taking backup and restoration, managing the user’s home directory, assigning a quota to a folder for a specific purpose. Thus, it is very important to get a good grip over on it using Ansible and understanding its different ways, available parameters, and acceptable parameters.
How to Create the Ansible Directory?
In Ansible we can create directory or directories on remote hosts by using below two methods: –
- Using the command module and give the command to create a directory in plain
- Using the file module and give the directory as the state, as its
Both of the above can be done in two ways:
- Using the ansible command and give all parameters on the command line. But the output is comprehensive, also it’s complex when you have tasks to manage multiple directories and you are going to use differently available
- Using ansible-playbook command and give a YAML file to it to interpret and execute on remote hosts. In this YAML file, you give all parameters and options to create a directory and manage it. This is more organized and easier to use once you understand
Examples of Ansible Create Directory
Now we use a few examples to understand some of the available parameters which you can use along with creating a directory.
But first, let me tell you about the lab environment which we are going to use, where we have one ansible control server named ansible-controller and two remotes hosts named host-one and host-two, all of these are Red Hat Enterprise Linux version 7 as the operating system.
- For creating a directory on remote hosts using the command module and pass the command to create a directory like below-using ansible: –
ansible all -m command -a "mkdir /tmp/sample_dir_1"
In the output, you may get a suggestion as a warning that there is a file module that you can use for the same purpose.
- For creating a directory on remotes hosts, using the file module on the ansible command line. Use below command
ansible all -m file -a 'path=/tmp/sample_directory state=directory'
You get an output like below if the directory successfully created on the path given as parameters.
- For creating a directory on remote hosts, create a YAML file, having contents like
---
name: This Play Creates Test Directory In /tmp hosts: all
tasks:
name: create a directory named test file:
path: /tmp/test state: directory
Then use ansible-playbook command to execute the plays and tasks in this file.
ansible-playbook create_dir.yaml
You will get an output like below after successful run
- For creating directory and assigning permissions at creation time, create a YAML file with contents like below
---
name: This Play Creates Test_1 Directory In /tmp and assign 777 permission to it hosts: all
tasks:
name: create a directory named TEST_1 and assign 777 permission file:
path: /tmp/test_1 state: directory
mode: "u=rwx,g=rwx,o=rwx"
Then use ansible-playbook command to execute the plays and tasks in this file.
ansible-playbook create_dir_do_permission.yaml
The output will look like below: –
- Create multiple directories using with_items, create a YAML file with contents like below
---
name: This Play Creates Multiple Directories Using with_items hosts: all
tasks:
name: create multiple directories file:
path: /tmp/{{ item }} state: directory with_items:
test1
test2
test3
test4
Then use ansible-playbook command to execute the plays and tasks in this file.
ansible-playbook create_multiple_dir.yaml
The output will look like below: –
- Create multiple directories using with_items and assigning different permissions to them, create a YAML file with contents like below
---
- name: This play creates multiple directories using with_items and assign different permission hosts: all
tasks:
- name: create three directories and assiging 0777, 0755, 0644 file:
path: /tmp/{{ item.path }} mode: “{{ item.mode }}” state: directory with_items:
- { path: '/tmp/test_1', mode: '0777'}
- { path: '/tmp/test_2', mode: '0755'}
- { path: '/tmp/test_3', mode: '0644'}
Then use ansible-playbook command to execute the plays and tasks in this file.
ansible-playbook create_multiple_dir_diff_perm.yaml
The output will look like below: –
You can check on host-one and host-two as below:
Log in to each host and check directories existence and permissions:
ls –ld /tmp/tmp/test_*
- Creating a local directory on the controller node in a playbook can be done by using local_action.
For this create a playbook with content like below:
---
name: This will create a local directory on Controller machine hosts: all
tasks:
name: create local directory named /var/tmp/local local_action:
module: file
path:
/var/tmp/local
state: directory
Then run this YAML file using the ansible-playbook command line.
ansible-playbook create_local_dir.yml
The output will look like below:
You can check whether a directory is created or not.
ls -ld /var/tmp/local
- Creating a directory with names having variable contents, for example, if you want to create a directory with name ends with the current timestamp. You can use ansible facts which are fetched by default if not
First, prepare a file like below: –
- hosts:
all tasks:
name: create a directory with using a fact file:
path: "/tmp/sample{{ansible_date_time.date}}" state: directory
mode: "u=rw,g=wx,o=rwx"
when executed like below
ansible-playbook create_dir_with_var_name.yml
Created directories can be checked on remote nodes
ls -ld /tmp/sample*
ls -ld /tmp/sample*
Conclusion
You might have noticed that working with the Ansible file module is not a complex task. Also using it for creating, updating permissions like operations on a directory is simpler. But as this is one of the basic modules which you should imbibe to work smoothly on Linux/UNIX systems. Also, remember to check always syntax and used options before running a playbook.
Recommended Articles
This is a guide to Ansible Create Directory. Here we discuss how to Create the Ansible Directory along with respective examples. You can also go through our other suggested articles to learn more –