EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Ansible Create Directory

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Ansible Tutorial » Ansible Create Directory

Ansible Create Directory

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

Popular Course in this category
Ansible Training (1 Course, 4 Projects)1 Online Courses | 4 Hands-on Projects | 8+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (3,201 ratings)
Course Price

View Course

Related Courses
Linux Training Program (16 Courses, 3+ Projects)Kali Linux Training (3 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)
  • 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.

Ansible Create Directory output 1

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

Ansible Create Directory output 2

  • 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

Ansible Create Directory output 3

  • 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: –

Ansible Create Directory output 4

  • 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: –

Ansible Create Directory output 5

  • 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: –

output 6

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_*

output 7

  • 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:

output 8

You can check whether a directory is created or not.

ls -ld /var/tmp/local

output 9

  • 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

output 10

 Created directories can be checked on remote nodes

ls -ld /tmp/sample*

output 11

ls -ld /tmp/sample*

output 12

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 –

  1. Ansible Versions
  2. Ansible Loop
  3. Ansible Vault
  4. Ansible YAML

Ansible Training (1 Course, 4 Projects)

1 Online Courses

4 Hands-on Projects

8+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Ansible Tutorial
  • Ansible Basics
    • What is Ansible
    • Is Ansible free
    • How to Install Ansible
    • Ansible Versions
    • Ansible Architecture
    • Ansible Commands
    • Ansible ad-hoc Commands
    • Ansible Playbooks
    • Ansible Roles
    • Ansible Tags
    • Ansible Ping
    • Ansible Apt
    • Ansible Facts
    • Ansible Tower
    • Ansible Galaxy
    • Ansible Handlers
    • Ansible Loop
    • Ansible Block
    • Ansible Conditional
    • Ansible YAML
    • Ansible Debug
    • Ansible Synchronize
    • Ansible Template
    • Ansible group_vars
    • Ansible Lookup
    • Ansible File Module
    • Ansible Yum Module
    • Ansible Shell Module
    • Ansible lineinfile
    • Ansible Service Module
    • Ansible User Module
    • Ansible Windows Modules
    • ansible-doc
    • Ansible Filters
    • Ansible Add User to Group
    • Ansible Register
    • Ansible Set Fact
    • Ansible Hosts File
    • Ansible add_host
    • Ansible Collections
    • Ansible with_items
    • Ansible Replace Line in File
    • Ansible inventory_hostname
    • Ansible Dynamic Inventory
    • Ansible local_action
    • Ansible Firewalld
    • Ansible Unarchive
    • Ansible Sudo
    • Ansible Create Directory
    • Ansible Reboot
    • Ansible wait_for
    • Ansible run_once
    • Ansible Authorized_key
    • Ansible Interview Questions

Related Courses

Linux Training Course

Kali Linux Training

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Ansible Training (1 Course, 4 Projects) Learn More