Install Ansible on CentOS7

28/12/2020
Chưa phân loại
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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

How to Change Screen Resolution on Ubuntu

Screen resolution is an important factor in enjoying your system. For allowing us, humans, to perform tasks, we have to...
29/12/2020

Install Jenkins on Ubuntu

How To Install Jenkins on Ubuntu Jenkins is an open source automation server. It is used for building software or apps automatically,...
12/02/2020

Markdown Tutorial — From Setup to Syntax

Markdown is an easy to read and easy to learn markup language. Unlike HTML, XML or other markup languages, where the insane...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024