Sunday, 16 January 2022

ANSIBLE - HOW TO DELETE FOLDER FROM REMOTE SERVERS

How to delete folder from Remote Servers

I have created 3 Ubuntu servers with the names:-

Server1 - Installed Ansible

Server2 - Client Node1

Server3- Client Node2

Note:- I installed ansible on Server1.

I have created a file :- vi remove-dir.yml

This will delete the folder /oracle from the app group [Server2 and Server3]



ANSIBLE - HOW TO GET THE DISK DETAILS FROM REMOTE SERVERS

 How to get the disk details from Remote Servers

I have created 3 Ubuntu servers with the names:-

Server1 - Installed Ansible

Server2 - Client Node1

Server3- Client Node2

Note:- I installed ansible on Server1.

I have created a file :- vi disk-details.yml

This will gather the disk details from the app group [Server2 and Server3]


Print the disk space details from the servers













ANSIBLE - HOW TO CREATE A FOLDER AND CREATE A FILE NAMED WELCOME USING ANSIBLE FILE/COPY MODULE

How to create a folder using Ansible File Module and create a file named welcome

I have created 3 Ubuntu servers with the names:-

Server1 - Installed Ansible

Server2 - Client Node1

Server3- Client Node2

Note:- I installed ansible on Server1.

I have created a file :- vi create-file.yml

This will create the directory oracle on the / partition and welcome file under it 

for app group [Server2 and Server3]




Now we will run the playbook with the command:- ansible-playbook create-file.yml



The above task has been completed successfully. We can validate the folder and file creation.
 
Server2




Server3



ANSIBLE - HOW TO RUN THE ANSIBLE PLAYBOOK IN SIMULATION MODE

How to run the ansible playbook in Simulation Mode.

In order to run the playbook in simulation mode, we can run the command - ansible-playbook  -C [Name of the playbook].

In my example, I am running it for the playbook - create-dir-oracle.yml



ANSIBLE - HOW TO CHECK THE SYNTAX/ERRORS IN ANSIBLE PLAYBOOK

 How to check the syntax/errors in Ansible Playbook

In order to check the syntax for the playbook, we can run the command - ansible-playbook  --syntax-check [Name of the playbook].

In my example, I am checking the errors for the playbook - create-dir-oracle.yml.



After executing the check, no errors were found in the playbook.

ANSIBLE - HOW TO CREATE A FOLDER USING FILE MODULE

 How to create a folder using File Module

I have created 3 Ubuntu servers with the names:-

Server1 - Installed Ansible

Server2 - Client Node1

Server3- Client Node2

Note:- I installed ansible on Server1.

I have created a file :- vi create-dir-oracle.yml


This will create the directory oracle on the / partition


Now we will run the playbook with the command:- ansible-playbook create-dir-oracle.yml



The above task has been completed successfully.

We can validate the folder creation by checking the / partition of server2 and server3.

Server2


Server3




Note:- To check the syntax - ansible-playbook  --syntax-check create-dir-oracle.yml


      
We can also run the ansible-playbook in an simulation mode - ansible-playbook  -C  [Name of the playbook]. This will not execute the desired task mentioned in the playbook.








ANSIBLE - HOW TO SETUP ANSIBLE ENVIRONMENT

 Ansible - How to setup Ansible Environment

In my example, I will should you how to setup your ansible environment and run the ping module.

1) I have created 3 Ubuntu servers with the names:-

Server1 - Installed Ansible

Server2 - Client Node1

Server3- Client Node2

Note:- I installed ansible on Server1 by running the below commands on ubuntu:-

sudo apt-add-repository ppa:ansible/ansible

sudo apt update

sudo apt install ansible

Once ansible is installed on the server1, we can check it by running the command:- ansible --version


2) Then I generated ssh-keys on the server1 by running the command:= ssh-keygen -t rsa


3) Then I copied the keygen to all servers by running the command:- ssh-copy-id <IP Address>
For Example:-

ssh-copy-id 192.168.181.11
ssh-copy-id 192.168.181.12
ssh-copy-id 192.168.181.13

Server1


Server2



Server3


This is to make sure, we can run the ansible playbooks from the ansible server. I will be using root user to execute the playbooks.

4) Then I created a folder scripts which will be used for keeping all my playbooks, inventory files etc.



5) Then I created my inventory file in the same folder as inv


I defined hosts in my inv file with 2 groups:- all and app

The all group is having all the 3 hosts - server1, server2 and server3 and the app group is having 2 hosts:- server2 and server3 as shown below


6) I also created a file ansible.cfg in the same folder - scripts and defined the name of my inventory file.
In my example, it is inv




7) Now we can check the connectivity between the servers with the help of ping module.
Command:- ansible -m ping [Name of the group defined in inventory file - inv)
In my example, I am running it on app group 


Now I am running the ping module for the all group defined in the inv file.


The above screenshots refers successfully connectivity between all the servers from the ansible server -server1.