Knowledgebase

How to install Ansible and Use it in Automation  Print this Article

In this artical we will known about how to install Ansible, Before going through that we would known about what is Ansible and what’s the uses of it.

ANSIBLE

Ansible is a  automation tool which is used  manage the server(‘s) for you. We just need to configure the Ansible to  installing a package or configuring a server application or even restarting a service.

But the question is why we need automation tools especially Ansible, the answer is so simple. The maintenance of single server is not always easy, but if we have to manage more than one server, the goes very hard without automation tools. Ansible is agent less which is the major advantage when compared to the other Automation tools like puppet, chef,salt etc. we do not need to have anything installed on the client’s end.In Ansible  both push and pull mode are supported. Ansible is a security focused tool it uses Open SSH . Ansible scripts are commonly known as playbooks and they are easy to read.

Installation of Ansible Automation Tool

On RHEL/CENTOS Machines

To install Ansible in RHEL/CENTOS, we should install and enable the epel repository by using the following command.

root@localhost ~]# yum install epel-release

The install the packages python-pip, python-devel and git

[root@localhost ~]# yum install python-pip python-devel git

After the completion of these packages install Ansible Package as follows

[root@localhost ~]# yum install ansible

otherwise you can also install by using pip command as follows

[root@localhost ~]#pip install ansible

To install in fedora just replace yum with dnf, even yum also work fine with fedora.

check the version ansible by using the command –version

 

Inventory File

Ansible works against multiple systems in your infrastructure at the same time. It does this by selecting portions of systems listed in Ansible’s inventory file, which defaults to being saved in the location /etc/ansible/hosts.

The format for /etc/ansible/hosts is as follows

 

A group can have multiple server and one server can be a part of multiple groups.
Name of group is enclosed in square brackets []. Server names can be their dns names or ip addresses.

Here i used the servers 10.0.0.6 and 10.10.16.137.

[Web Servers]
10.0.0.6
10.10.16.137

 SSH-keygen and SSH-copy-id

To run any module or playbook  from local host to remote host, we need to create and copy ssh keys to remote hosts by using ssh-keygen and ssh-copy-id.

First we create ssh key by usng ssh-keygen command

 

Then copy the ssh key to remote host by using ssh-copy-id command.

[root@localhost ~]# ssh-copy-id root@10.0.0.6
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), 
to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- 
if you are prompted now it is to install the new keys
root@10.0.0.6's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@10.0.0.6'"
and check to make sure that only the key(s) you wanted were added.
 [root@localhost ~]# ssh-copy-id root@10.10.16.137
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s),
 to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- 
if you are prompted now it is to install the new keys
root@10.10.16.137's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@10.10.16.137'"
and check to make sure that only the key(s) you wanted were added.

Ansible Modules

Ansible ships with a number of modules (called the ‘module library’) that can be executed directly on remote hosts or through Playbooks.

Users can also write their own modules. These modules can control system resources, like services, packages, or files (anything really), or handle executing system commands.

We can ping all the hosts in Inventory file by using the following command

we can also run the command in all remote hosts or particular group in inventory file by using ansible

Here i run the command module ls, it lists out all the files in remote hosts home directory.

we can also know the host name, up time, who logged in remote hosts as follows.

 

Ansible Playbooks

Playbooks are Ansible configuration,deployment of policies we want to apply or enforce in the remote hosts they are in YAML begins with “–“. It consists of a listing of modules  and the arguments.

Example:

---
hosts: localhost
- name: install pip
yum: pkg=python-pip state=installed

The above code will install package pip in remote hosts. we will cover in detail about Ansible playbooks in another article.

Was this answer helpful?

Also Read

How Do I Find The Largest Top 10 Files and Directories On a Linux / UNIX / BSD?
ow do I find the largest top files and directories on a Linux or Unix-like operating systems?...
Create passwordless login with SSH keys and PUTTY
Benefits of passwordless login with SSH keys If VPS/Dedicated server is visible over the...
Changing the SSH Port for Your Linux Server
You can change the default SSH port for your Linux server as an added security measure NOTE!...
IPTABLES examples
#1: Displaying the Status of Your Firewall Type the following command as root: # iptables -L -n...
25 Most Frequently Used Linux IPTables Rules Examples
In the below examples we are using ETH0 as network interface, which would be the interface you...