0% found this document useful (0 votes)
6 views

Creating directories with the File Module

Uploaded by

comp
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Creating directories with the File Module

Uploaded by

comp
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

CREATING DIRECTORIES WITH THE FILE MODULE:

- Now, what we're going to do is, we're going to create a directory and
within that directory we're going to create a backup.

- So, we're going to work with the file module:

- So, how to create directories using the "File Module".

- Web Browser:

- "Ansible file' in google search

- "Ansible.builtin.file module"

- Scroll down a bit, and you can see that it can set the attributes of
files, directories.

- If you scroll down a bit again, you will find a lot of parameters
that you can work with.

- "Path" parameter

1. WRITING A PLAYBOOK:
[root@localhost My-Automation-Stuff2]# pwd
/home/trainonic/My-Automation-Stuff2

[root@localhost My-Automation-Stuff2]# cat example12-file_module.yml


---

- name: "Testing Playbook"


hosts: localhost

tasks:
- name: "Task1 - Collection facts from the localhost"
ansible.builtin.setup:
filter:
- "ansible_date_time" (Parsing date and time)
register: output

- name: "Task2 - Recording Variable" (Similar to "register"


variable module, assigning date and time.)
set_fact:
TD: "{{ ansible_date_time.date }}" (TD= today's date)

- name: "Task3 - Creating Directories"


file:
path: "Backups/{{ TD }}" #or path could be
"/home/trainonic/My-Automation-Stuff-2/Backups/{{ TD }}"
state: directory (To create directory)
run_once: true (If the directory is already
created, then it's going to ignore it.)

- Now, verify the present working directory and make sure that there's no
directory name "Backups" created.

2. RUNNING THE PLAYBOOK:


[root@localhost My-Automation-Stuff2]# ansible-playbook example12-file_module.yml

PLAY [Testing Playbook]


**************************************************************************

TASK [Task1 - Collection facts from the localhost]


***********************************************
ok: [localhost]

TASK [Task2 - Recording Variable]


****************************************************************
ok: [localhost]

TASK [Task3 - Creating Directories }}]


***********************************************************
changed: [localhost]

PLAY RECAP
***********************************************************************************
****
localhost : ok=3 changed=1 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0

- Here, you can see that something has been changed within our present
working directory.

3. VERIFYING THE PRESENT W0RKING DIRECTORY:


[root@localhost My-Automation-Stuff2]# ls -l
total 84
-rw-r--r--. 1 root root 768 Jan 24 16:12 ansible.cfg
-rw-r--r--. 1 root root 131 Jan 25 18:11 arista1-facts.yml
-rw-r--r--. 1 root root 120 Jan 29 17:37 arista1-ip.yml
-rw-r--r--. 1 root root 219 Jan 29 19:18 arista1-vlans.yml
drwxr-xr-x. 3 root root 24 Jan 30 13:55 Backups
(Folder created successfully.)
-rw-r--r--. 1 root root 512 Jan 29 19:12 example10-tshoot-Napalm-VLANs-
validation.yml
-rw-r--r--. 1 root root 324 Jan 30 12:35 example11-setup_module.yml
-rw-r--r--. 1 root root 521 Jan 30 13:52 example12-file_module.yml
-rw-r--r--. 1 root root 486 Jan 25 20:06 example1.yml
-rw-r--r--. 1 root root 532 Jan 25 20:06 example1.yml.ori
-rw-r--r--. 1 root root 451 Jan 25 13:59 example2.yml
-rw-r--r--. 1 root root 472 Jan 25 14:43 example3.yml
-rw-r--r--. 1 root root 512 Jan 25 18:17 example4.yml
-rw-r--r--. 1 root root 453 Jan 29 14:24 example5-tshoot-Napalm-Interface_ip-
validation.yml
-rw-r--r--. 1 root root 488 Jan 29 14:24 example6-tshoot-Napalm-validation.yml
-rw-r--r--. 1 root root 509 Jan 29 17:40 example7-tshoot-Napalm-validation.yml
-rw-r--r--. 1 root root 445 Jan 29 18:10 example8-tshoot-Napalm-VLANs-
validation.yml
-rw-r--r--. 1 root root 472 Jan 29 18:31 example9-tshoot-Napalm-VLANs-
validation.yml
drwxr-xr-x. 2 root root 72 Jan 25 14:17 group_vars
drwxr-xr-x. 2 root root 75 Jan 24 15:10 host_vars
-rw-r--r--. 1 root root 102 Jan 25 18:25 ios1-facts.yml
-rw-r--r--. 1 root root 111 Jan 29 17:34 ios1-ip.yml
-rw-r--r--. 1 root root 708 Jan 29 19:51 ios1-vlans.yml
drwxr-xr-x. 6 root root 4096 Jan 24 16:04 napalm-ansible

4. VERIFYING THE CONTENTS ON THE BACKUPS DIRECTORY:


[root@localhost My-Automation-Stuff2]# ls -l Backups/
total 0
drwxr-xr-x. 2 root root 6 Jan 30 13:55 2024-01-30

- so, here, we're checking if the directory is created within the Backup
directory named with today's date.

5. VERIFYING THE CONTENT IN "2024-01-30" DIRECTORY:


[root@localhost My-Automation-Stuff2]# ls -l Backups/2024-01-30/
total 0

- So, here, we can see that there are no contents within the directory, as of
now.
6. RE-RUNNING THE PLAYBOOK: (You'll see that no changes are going to be made,
because the directories are already created in our PWD):
[root@localhost My-Automation-Stuff2]# ansible-playbook example12-file_module.yml

PLAY [Testing Playbook]


**************************************************************************

TASK [Task1 - Collection facts from the localhost]


***********************************************
ok: [localhost]

TASK [Task2 - Recording Variable]


****************************************************************
ok: [localhost]

TASK [Task3 - Creating Directories }}]


***********************************************************
ok: [localhost]

PLAY RECAP
***********************************************************************************
****
localhost : ok=3 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0

- Here, you can see that nothing has been changed in our Present working
directory because the "Backup" directory does already
exists in our Present working directory.

- Try to delete the BACKUP directory (#rm -rf Backups) and re-run the
playbook again and then you'll find that the script gets
executed successfully and the directory also get created successfully.

- So, now we know how to create a folder based on date, though the folder is
empty. So, we're going to create a backup and
include on that specific directory, which is going to be covered on the
very next topic.
***********************************************************************************
****************************************************
X-X-X-X-X

You might also like