Ansible Pilot

Create a directory in Linux - Ansible module file

Create a directory - Ansible module file

How to create an “example” directory on Linux filesystem using Ansible Automation.

How to create a directory with Ansible?

I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible create a directory

Today we’re talking about the Ansible module file. The full name is ansible.builtin.file, which means that is part of the collection of modules “builtin” with ansible and shipped with it. It’s a module pretty stable and out for years. It works in a different variety of operating systems. It manages files and file properties. For Windows targets, use the ansible.windows.win_file module instead.

Parameters

This module has some parameters to perform any tasks. The only required is “path”, where you specify the filesystem path of the file you’re going to edit. The state defines the type of object we are modifying, the default is “file” but for our use case, we need the “directory” option.

Demo

Let’s jump into a real-life playbook on how to create a directory with Ansible.

---
- name: file module demo
  hosts: all
  vars:
    mydir: "~/example"
  tasks:
    - name: Creating a directory
      ansible.builtin.file:
        path: "{{ mydir }}"
        state: directory
        owner: devops
        group: users
        mode: '0644'

execution

$ ansible-playbook -i demo/inventory create\ directory/file.yml
PLAY [file module demo] *********************************************************************
TASK [Gathering Facts] **********************************************************************
ok: [demo.example.com]
TASK [Creating a directory] *****************************************************************
changed: [demo.example.com]
PLAY RECAP **********************************************************************************
demo.example.com           : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

before execution

$ ssh [email protected]
[[email protected] ~]$ ls -al
total 16
drwx------. 4 devops wheel  111 Nov 14 13:43 .
drwxr-xr-x. 5 root   root    50 Nov  8 17:07 ..
drwx------. 3 devops wheel   17 Sep  6 05:53 .ansible
-rw-------. 1 devops wheel 2849 Nov 14 13:43 .bash_history
-rw-r--r--. 1 devops wheel   18 Dec  4  2020 .bash_logout
-rw-r--r--. 1 devops wheel  141 Dec  4  2020 .bash_profile
-rw-r--r--. 1 devops wheel  376 Dec  4  2020 .bashrc
drwx------. 2 devops wheel   94 Sep 16 10:09 .ssh
[[email protected] ~]$

after execution

$ ssh [email protected]
[[email protected] ~]$ ls -al
total 16
drwx------. 5 devops wheel  126 Nov 14 13:45 .
drwxr-xr-x. 5 root   root    50 Nov  8 17:07 ..
drwx------. 3 devops wheel   17 Sep  6 05:53 .ansible
-rw-------. 1 devops wheel 2861 Nov 14 13:44 .bash_history
-rw-r--r--. 1 devops wheel   18 Dec  4  2020 .bash_logout
-rw-r--r--. 1 devops wheel  141 Dec  4  2020 .bash_profile
-rw-r--r--. 1 devops wheel  376 Dec  4  2020 .bashrc
drwx------. 2 devops wheel   94 Sep 16 10:09 .ssh
drw-r--r--. 2 devops users    6 Nov 14 13:45 example

code with ❤️ in GitHub

Recap

Now you know how to create a directory in Linux with Ansible. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

Donate

Want to keep this project going? Please donate

Follow me

Subscribe not to miss any new releases

November 17, 2021