How to create a directory and a file using ansible-playbook on Ubuntu 21.04
To Create a Directory and a File Using Ansible Playbook on Ubuntu 21.04
Introduction:
Ansible is an open-source configuration management tool that is capable of running in many Unix-like systems and windows systems. It Automates the configuration by using simple yml code and this code is further deployed in N number of servers. Unlike Chef and Puppet, it is a Push type configuration management and the code is pushed from server or master systems to the node or client systems.
Installation Procedure:
Checking the installed OS version by using the following command
linuxhelp@linuxhelp:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 21.04
Release: 21.04
Codename: hirsute
Log in as Root user by using following command
linuxhelp@linuxhelp:~$ sudo -s
[sudo] password for linuxhelp:
Changing the directory by using following command
root@linuxhelp:/home/linuxhelp# cd /
Installing Ansible by using following command
root@linuxhelp:/# apt install ansible
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
ansible-base ieee-data python3-argcomplete python3-distutils python3-dnspython python3-ecdsa python3-jinja2 python3-jmespath
python3-kerberos python3-lib2to3 python3-libcloud python3-netaddr python3-ntlm-auth python3-packaging python3-pycryptodome
python3-pyparsing python3-requests-kerberos python3-requests-ntlm python3-selinux python3-winrm python3-xmltodict
Suggested packages:
cowsaysshpass python-jinja2-doc ipython3 python-netaddr-docs python-pyparsing-doc
The following NEW packages will be installed:
ansible ansible-base ieee-data python3-argcomplete python3-distutils python3-dnspython python3-ecdsa python3-jinja2
python3-jmespath python3-kerberos python3-libcloud python3-netaddr python3-ntlm-auth python3-packaging python3-pycryptodome
python3-pyparsing python3-requests-kerberos python3-requests-ntlm python3-selinux python3-winrm python3-xmltodict
The following packages will be upgraded:
python3-lib2to3
1
Get:5 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 python3-distutils all 3.9.5-0ubuntu3~21.04 [142 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu hirsute/main amd64 python3-dnspython all 2.0.0+really1.16.0-2ubuntu2 [96.9 kB]
Creating a playbook by using the following command
root@linuxhelp:/# vi test.yml
---
- hosts: localhost
tasks:
- name: "create a directory named test"
file:
path: /home/linuxhelp/test
state: directory
- name: "create a file on test directory"
file:
path: /home/linuxhelp/test/file.txt
state: touch
Checking the playbook syntax by using the following command
root@linuxhelp:/# ansible-playbook test.yml --syntax-check
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
playbook: test.yml
Run the playbook by using the following command
root@linuxhelp:/# ansible-playbook test.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *******************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************
ok: [localhost]
TASK [create a directory named test] ***********************************************************************************************
changed: [localhost]
TASK [create a file on test directory] *********************************************************************************************
changed: [localhost]
PLAY RECAP *************************************************************************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Changing the directory
root@linuxhelp:/# cd /home/linuxhelp/
Long Listing the directory to view created directory
root@linuxhelp:/home/linuxhelp# ls -la
total 84
drwxr-x--- 17 linuxhelplinuxhelp 4096 Oct 15 18:35 .
drwxr-xr-x 3 root root 4096 Sep 8 16:51 ..
-rw------- 1linuxhelplinuxhelp 97 Oct 15 18:23 .bash_history
-rw-r--r-- 1linuxhelplinuxhelp 220 Sep 8 16:51 .bash_logout
-rw-r--r-- 1linuxhelplinuxhelp 3771 Sep 8 16:51 .bashrc
drwx------ 12 linuxhelplinuxhelp 4096 Oct 6 13:57 .cache
drwx------ 12 linuxhelplinuxhelp 4096 Oct 6 14:01 .config
drwxr-xr-x 2linuxhelplinuxhelp 4096 Sep 8 17:00 Desktop
drwxr-xr-x 2linuxhelplinuxhelp 4096 Oct 6 14:00 Documents
drwxr-xr-x 2linuxhelplinuxhelp 4096 Sep 8 17:00 Downloads
drwx------ 2linuxhelplinuxhelp 4096 Oct 6 13:58 .gnupg
drwxr-xr-x 3linuxhelplinuxhelp 4096 Sep 8 17:00 .local
drwx------ 5linuxhelplinuxhelp 4096 Oct 6 13:58 .mozilla
drwxr-xr-x 2linuxhelplinuxhelp 4096 Sep 8 17:00 Music
drwxr-xr-x 2linuxhelplinuxhelp 4096 Sep 8 17:00 Pictures
-rw-r--r-- 1linuxhelplinuxhelp 807 Sep 8 16:51 .profile
drwxr-xr-x 2linuxhelplinuxhelp 4096 Sep 8 17:00 Public
drwx------ 2linuxhelplinuxhelp 4096 Oct 6 13:58 .ssh
-rw-r--r-- 1linuxhelplinuxhelp 0 Sep 8 11:37 .sudo_as_admin_successful
drwxr-xr-x 2linuxhelplinuxhelp 4096 Sep 8 17:00 Templates
drwxr-xr-x 2 root root 4096 Oct 15 18:35 test
drwxr-xr-x 2linuxhelplinuxhelp 4096 Sep 8 17:00 Videos
Long Listing the directory to view created file
root@linuxhelp:/home/linuxhelp/test# ls -la
total 8
drwxr-xr-x 2 root root 4096 Oct 15 18:35 .
drwxr-x--- 17 linuxhelplinuxhelp 4096 Oct 15 18:35 ..
-rw-r--r-- 1 root root 0 Oct 15 18:35 file.txt
By this creating a directory and a file using ansible playbook on Ubuntu 21.04 comes to end
Comments ( 0 )
No comments available