Ansible

Install Ansible on CentOS7

Ansible is a zero configuration server management utility. It is used to manage many servers from a central computer. It makes every system administrative tasks easy.

In this article, I will show you how to install Ansible on CentOS 7. Let’s get started.

Installing Ansible

Ansible is not available on the official repository of CentOS 7. But it is available in the epel repository.

So first, you have to enable epel repository in CentOS 7. The easiest way to do that is to install epel-release package using yum.

Install epel-release package with the following command:

$ sudo yum install epel-release

Press ‘y’ and then press <Enter> to continue.

epel-release package should be installed.

Now you can install Ansible with the following command:

$ sudo yum install ansible

Now press ‘y’ and then press <Enter> to continue.

Ansible should be installed.

Now check that Ansible is installed with the following command:

$ ansible --version

As you can see from the screenshot below, the version of Ansible installed in my machine is 2.4.2

Testing Ansible

To manage servers with Ansible, you first have to install SSH server software in the servers. This is the only requirement.

You can install SSH server software on Ubuntu or Debian server with the following commands:

$ sudo apt-get update
$ sudo apt-get install openssh-server -y

You can install SSH server on Red Hat Enterprise Linux or CentOS 7 with the following command:

$ sudo yum install openssh-server -y

The servers I will manage with Ansible installed on my CentOS 7 operating system are all Ubuntu 16.04 Xenial Xerus servers.

I will create 2 Ubuntu 16.04 LTS server virtual machines. The hostnames are server1 and server2.

First I will update the package repository cache of server1 and server2 as follows.

server1 $ sudo apt-get update

server2 $ sudo apt-get update

Now I will install SSH server on both server1 and server2 as follows:

server1 $ sudo apt-get install openssh-server -y

SSH server on server1 should be installed.

server2 $ sudo apt-get install openssh-server -y

SSH server on server2 should be installed.

Now I will check whether SSH server is running on server1 and server2

server1 $ sudo systemctl status sshd

SSH server is running on server1 as you can see from the screenshot below.

server2 $ sudo systemctl status sshd

SSH server is running on server2 as you can see from the screenshot below.

If in case the SSH server is not running on Ubuntu or CentOS server, you can start it with the following command:

$ sudo systemctl start sshd

Now let’s check the IP address of server1 with the following command:

server1 $ ip a

As you can see from the marked section of the screenshot below, the IP address of server1 is 192.168.10.94

Now let’s check the IP address of server2 with the following command:

server2 $ ip a

As you can see from the marked section of the screenshot below, the IP address of server2 is 192.168.10.96

Now on your CentOS 7 machine where you installed Ansible, run the following command to open /etc/ansible/hosts file:

$ sudo nano /etc/ansible/hosts

You should see the following window as shown in the screenshot below.

Now add the IP addresses or hostnames of the servers that you want to manage with Ansible in that file.

I will add 192.168.10.94 and 192.168.10.96 on the file as shown in the screenshot below. These are the IP addresses of my Ubuntu 16.04 LTS servers that I want to manage with Ansible.

Once you’ve added the IP addresses of the servers, save the file.

You still have to do one more thing before you can get started. That is, you have to copy a piece of SSH key of your CentOS 7 machine where you installed Ansible to the server that you want to manage. That way you won’t have to login to the servers with password manually every time.

First generate a SSH key with the following command:

$ ssh-keygen

Now run the following commands to copy the ssh key:

$ ssh-copy-id shovon@192.168.10.94

$ ssh-copy-id shovon@192.168.10.96

Now you can ping the servers to see whether they are online or not with the following command:

$ ansible -m ping all

As you can see from the screenshot below, the ping succeeded.

If you’re also using Ubuntu server for the demo, and the Ansible command fails, then you may try to install python2 on your Ubuntu server with the following command:

$ sudo apt-get install python -y

Then everything should work.

So that’s how you install and test Ansible on CentOS 7. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.