Installing Ansible on CentOS 7.2

Installing Ansible on CentOS 7.2

In this article, we will cover the installation and configuration of Ansible on a CentOS 7.2 system. We will also cover how to configure the host list, set up SSH without a password, and execute an Ansible playbook.

Viewing Python Version

First, let’s check the version of Python installed on our system:

[Root @ ambari1 ~] # python -V
Python 2.7.5

Installing Ansible

To install Ansible, we need to install the required RPMs using Yum. Ansible supports EPEL 6, 7, and Fedora releases. However, the operating system version on the node may be earlier versions (e.g., EL5), but Python 2.4 or later must be installed.

Mounting a Fedora Users Can Be Installed Directly Ansible

Fedora users can install Ansible directly, but users of CentOS or RHEL need to configure the EPEL repository first.

Configuring the EPEL Repository

To configure the EPEL repository, we need to install the epel-release package:

[Note: If you install epel-release] can not be successful, then that CentOS-Base.repo in /etc/yum.repos.d might be set to bak or for other reasons

We can install the epel-release package using Yum:

yum install epel-release -y

Installing Ansible

Now that we have the EPEL repository configured, we can install Ansible using Yum:

yum install ansible -y

Viewing the Version Number of Ansible

To verify that Ansible has been installed correctly, we can view the version number:

[Root @ ambari1 yum.repos.d] # ansible --version
ansible 2.7.5
config file = /etc/ansible/ansible.cfg
configured module search path = [u '/root/.ansible/plugins/modules', u '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]

Ansible Profile Introduction

The Ansible profile is stored in the /etc/ansible/ directory. We can view the contents of the profile using the ll command:

[Root @ ambari1 yum.repos.d] # cd /etc/ansible/
[Root @ ambari1 ansible] # ll
total 24
-rw-r--r-- 1 root root 20277 14 2018 ansible.cfg
# 12 Yue profiles
-rw-r--r-- 1 root root 1016 14 2018 hosts
# 12 is dated host's file control
drwxr-xr-x 2 root root 6 14 2018 roles dated 12 is

Configuring the Host List

Ansible reads the host list from the /etc/ansible/hosts file. We can modify this file to specify the hosts that we want to manage. Here is an example of a host list:

# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
#
# Ex 1: Ungrouped hosts, specify before any group headers.
# green.example.com
# blue.example.com
# 192.168.100.1
# 192.168.100.10
#
# Ex 2: A collection of hosts belonging to the 'webservers' group
# [webservers]
# alpha.example.org
# beta.example.org
# 192.168.1.100
# 192.168.1.110
#
# If you have multiple hosts following a pattern you can specify
# Them like this:
# www[001:006].example.com
#
# Ex 3: A collection of database servers in the 'dbservers' group
# [dbservers]
# db01.intranet.mydomain.net
# db02.intranet.mydomain.net
# 10.25.1.56
# 10.25.1.57
#
# Here's another example of host ranges, this time there are no
# Leading 0s:
# db-[99:101].node.example.com
[Master]192.168.17.221
[Agent]192.168.17.222
192.168.17.223

Setting SSH Without Password

To set up SSH without a password, we need to generate a public-private key pair using the ssh-keygen command:

ssh-keygen -t rsa

We can then copy the public key to the remote host using the ssh-copy-id command:

ssh-copy-id root@192.168.199.130

Executing an Order

Once we have set up SSH without a password, we can execute an Ansible playbook using the ansible command:

[Root @ ambari1 .ssh] # ansible all -m ping
The authenticity of host '192.168.17.221 (192.168.17.221)' can not be established.
ECDSA key fingerprint is c5:76:ed:2e:c8:6b:85:25:0b:d7:b4:8f:12:66:72:1f.
Are you sure you want to continue connecting (yes/no)?

We can see that the 192.168.17.221 host is unreachable. We can add the following lines to the /etc/ansible/hosts file to specify the connection details:

## db-[99:101].node.example.com
[Master]192.168.17.221
ansible_connection = ssh
ansible_ssh_user = root
ansible_ssh_pass = 123456

This should resolve the issue and allow us to execute the Ansible playbook successfully.