Ansible Tutorial for Automation

What is Ansible ?

Ansible is an awesome automation tool for Linux sysadmins. It is an open source configuration tool which allows sysadmins to manage hundreds of servers from one centralize node i.e Ansible Server. Ansible is the preferred configuration tool when it is compared with similar tools like Puppet, Chef and Salt because it doesn’t need any agent and it works on SSH and python.

How to Install Ansible in CentOS 8 and RHEL 8

A) Ansible Installation in CentOS 8  

Ansible package is not available in default CentOS 8 package repository. so we need to enable EPEL Repository by executing the following command,

[ansible@amitmaheshwari.in ~] $ sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y

Once the epel repository is enabled, execute the following dnf command to install Ansible

[ansible@amitmaheshwari.in ~] $  sudo dnf install ansible

Once the ansible is installed successfully, verify its version by running the following command

[ansible@amitmaheshwari.in ~] $  sudo ansible --version

B) Ansible Installation in RHEL 8  

If you have a valid RHEL 8 subscription then use following subscription-manager command to enable Ansible Repo,

[ansible@amitmaheshwari.in ~] $ sudo subscription-manager repos --enable ansible-2.8-for-rhel-8-x86_64-rpms

Once the repo is enabled then execute the following dnf command to install Ansible,

[ansible@amitmaheshwari.in ~] $ sudo dnf install ansible -y

Once the ansible and its dependent packages are installed then verify ansible version by executing the following command,

[ansible@amitmaheshwari.in ~] $ sudo ansible --version

How to Use Ansible Automation Tool?

When we install Ansible using yum or dnf command then its configuration file, inventory file and roles directory created automatically under /etc/ansible folder.

So, let’s add a group with name “testserver” and under this group add 2 serevrs ip address  in /etc/ansible/hosts file

[ansible@amitmaheshwari.in ~] $ sudo vi /etc/ansible/hosts

[testserver]
192.168.1.10
192.168.1.20
:qw!
Save & exit file.

Once the inventory file (/etc/ansible/hosts) is updated then exchange your user’s ssh public keys with remote systems which are part of “testserver” group.

Let’s first generate your local user’s public and private key using ssh-keygen command,

[ansible@amitmaheshwari.in ~] $ ssh-keygen

Now exchange public key between the ansible server and its clients using the following command,

[ansible@amitmaheshwari.in ~] $ ssh-copy-id ansbile@192.168.1.10
[ansible@amitmaheshwari.in ~] $ ssh-copy-id ansbile@192.168.1.20

Now let’s try couple of Ansible commands, first verify the connectivity from Ansible server to its clients using ping module,

[ansible@amitmaheshwari.in ~] $ ansible -m ping "testserver"

Note: If we don’t specify the inventory file in above command then it will refer the default hosts file (i.e /etc/ansible/hosts)

In output if you see SUCCESS then ping was successful

That’s all from this article, we have successfully demonstrated on how to install and use Ansible on CentOS 8 and RHEL 8 System.

Download Ansible Documentation Guide