Thursday, 13 May 2021

Ansible - Part 2

 Ansible Folder structure

Please find below , once you install ansible , you will see below 3 files under /etc/ansible

  • ansible.cfg : Main configuration file for ansible, you can add all the settings for your exection purpose.
  • hosts : this file holds hots / server name or DNS names 
  • roles : This folder allows you to create folders for each server role , web/app.db etc.
if you want to check ping module (used for connectivity)with multiple servers use the below command

ansible ip1:ip2 -m ping

There will be scenario , where you have 100 servers , in that 20 DB 20 app servers , for all of those , you need to check the connectivity.

So , to give 20 or 30 server IP address is tedious task, to acheive this we have to group all those servers according to the type of servers in hosts(inventory) file

[web-servers]
ip1
ip2
ip3
[app-servers]
ip1
ip2

So, we can easily check the connectivty just by giving the group name with ping module as below

ansible app-servers -m ping

or

if you want to check app-server and web-servers connectivity , you can use the below

ansible app-servers:web-servers -m ping

As of now we used default hosts file which we got it from ansible installation.

What if multiple developers are working with multiple servers and which they would like to add their servers with their interested name

you can create inventory file name like sample.ini o any name with extension .ini and add your server names to that with group names and check the connectivity as below

ansible -i sample.ini app -m ping

here sample.ini : is the inventory file name
app : group name

Since we have many inventory files and its bit confusing everytime we need to run command with inventory file.

So, to avoid this we can update inventory file name in ansible.cfg file as below




Now lets see about ansible.cfg file



We can create our own ansible.cfg file in different folder and update that file path in default ansible.cfg file. So that when we execute ansible.cfg , it will take our customized cfg file details and act accordingly.



then export the same .
So, that it won't take default ansible.cfg setting for your ansible execution , it will take customized ansile.cfg file details.

Since we didn't write our custom config file , when we test as below , it won't perform any action , instead it will give error .

So, you can create inventory file as we defined above and add Ip address to check the connectivity.


You will get success message .

Inventory:
Static : IP address/DNS names/ Host names which are static and no change at any time
Dynamic: It will change regularly or very often.

There is scenario, where you have n number of servers which IP address will change dynamically when you start and stop the instances, in that case we need to get server IPs automatically by using inventory. That is called Dynamic inventory.

Usually we will use scripting language (python/golang/php etc) to achieve this.

Below is the simple python script.





When you connect with servers now, we need to update host checking to false , so that it won't check to connect., this we need to update in default ansible.cfg file.




SO, if you set host checking false , it won't ask yes/no for confirmation, 


Now you can execute and install softwares by using yum command