Creating directories with the File Module
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.
- Web Browser:
- "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
tasks:
- name: "Task1 - Collection facts from the localhost"
ansible.builtin.setup:
filter:
- "ansible_date_time" (Parsing date and time)
register: output
- Now, verify the present working directory and make sure that there's no
directory name "Backups" created.
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.
- so, here, we're checking if the directory is created within the Backup
directory named with today's date.
- 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 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