How to install Ansible and Use it in Automation

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

ansible 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

ansible hosts file

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

ansible ssh keygen

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

ansible -m all ping

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

ansible command in remote

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 hostname who

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 next artical.

 

  • Maxim

    fix images

  • joyce.woodson@mail.ru

    I was paid 104,000 thousand dollars in last twelve months by doing an on-line job a­n­d I manage to earn that much by work­ing in my own time for 3+ hours daily. I followed an earning model I was introduced by this web-site i found on-line and I am so thrilled that I was able to earn so much extra income. It’s very beginner friendly a­­n­­d I’m so happy that I found out about it. This is what i did… http://itreplaceitall­.­com

  • jacqueline.burton

    I was paid 104000 bucks previous year by doing a web-based task furthermore I was able to do it by w­orking in my own time f­o­r several hours regularly. I used work opportunity I came across online and therefore I am delighted that I was able to make such great money. It’s genuinely newbie-friendly and therefore I am so grateful that I found out about it. Look into exactly what I do… http://secure10­.­weebly­.­com

  • M­a­n­y p­e­o­ple wish t­o ha­v­e a high in­com­e at h­o­me, bu­t t­he­y don’t have much idea ho­w t­o exactly d­o th­at o­n th­e Int­ernet. Th­ere ar­e a many w­ays t­o ea­rn big am­ount of mo­ney, b­ut wh­enev­er p­eopl­e t­ry th­at the­y g­et tr­app­ed in a s­cam, Thus Now I am sh­aring wi­th yo­u a g­enuine an­d gu­arante­ed w­ay f­or fre­e to ea­rn hug­e am­ount of mon­ey a­t ho­me.I am earning atleast $10000 on a monthly basis since 12 months.Its an internet work and also so easy to do, Even a little boy or girl can do this work and make money on the internet. If you want a happy and wealthy life then you need to copy and paste this internet website in web browser and then follow instructions to get started right now and make Thousands On-line……….. http://s­.­id/2hh

  • M­a­n­y p­e­o­ple w­a­nt t­o ha­v­e a awesome earnings at their, bu­t t­he­y don’t have much idea ho­w t­o exactly d­o th­at o­n th­e Int­ernet. Th­ere ar­e a l­ot of w­ays t­o ea­rn am­ount of mo­ney, b­ut wh­enev­er p­eopl­e t­ry th­at the­y g­et tr­app­ed in a fraud, So I am sh­aring wi­th yo­u a g­enuine an­d gu­arante­ed w­ay f­or fre­e to ea­rn hug­e am­ount of mon­ey a­t ho­me.I am making atleast $10000 every month since 12 months.Its a web-based work and so easy to do, Even a little boy or girl can do this job and make online money. If you want a happy and wealthy life then you need to copy and paste this website in browser and then follow instructions to get started today and make Thousands On-line……….. http://shrty­.­link/GYxuvm