How to Install and Configure Ansible Automation on RHEL, CentOS and Ubuntu

How to Install and Configure Ansible Automation on RHEL, CentOS and Ubuntu

How to Install and Configure Ansible Automation on RHEL, CentOS and Ubuntu

What is Ansible?

Ansible is an open source simple and powerful agentless IT automation tool, it can remove hard manual work from your life. As most of the IT Automation tools runs as a agent in remote host but it just need a SSH connection and Python to be installed on Linux remote servers to do the actions.

What Ansible can Automate?

It can use to automate three types of tasks:

  1. It can use in Provisioning of your infrastructure to setup and provision various servers.
  2. Configuration Management: It can use to start and stop various services, changes can be done to application configuration files, OS and devices. Install and configure any applications, it can perform wide range of configuration tasks.
  3. Application Deployment: DevOps teams can use to to automate the deployment of many applications.

How it works?

Ansible is a simple IT automation engine. Its designed for multi-tier deployments since day one. It uses no agent and no additional custom security infrastructure, so it's  easy to deploy. It uses a very simple languages called YAML which we called it as "Ansible Playbooks". As we know their are many similar Automation tools available like Chef, Puppet, Space Walk, etc... But it's simple to use.

Ansible will have 2 server types Controlling Machine and nodes. Control Node is where Ansible is installed and the Managed Nodes are managed by Ansible Controlling Node over SSH to perform our tasks. Ansible works by connecting to your nodes machines and pushing out small programs which Ansible called as "Ansible Modules" to them. These programs are written to be resource models of the desired state of the system. When Ansible runs these modules and removes them when finished.

Control Node

Control node can be any machine with Python 2.7 or Python 3 (version 3.5 and higher), we can use or Laptop or Destktop too for Control Node which have any version of Linux. Currently Windows is not supported for control node. Here in this tutorial we create Ansible Control Node on RHEL, CentOS and Ubuntu.

My Control Node: 172.16.125.150 - control.linuxproguru.com

Installing Ansible on RHEL, CentOS and Ubuntu

We need to Enable some repositories before installing Ansible on RHEL and CentOS

Enable Ansible Engine Repository on RHEL 8, run the following command:
$ subscription-manager repos --enable=ansible-2.9-for-rhel-8-x86_64-rpms
Enable Ansible Engine Repository on RHEL 7, run the following command:
$ subscription-manager repos --enable=ansible-2.9-for-rhel-7-x86_64-rpms

RPMs for currently version for RHEL, CentOS are available from EPEL as well.

Install on RHEL 7, RHEL 8 and CentOS

$ yum install ansible

Installing On Ubuntu/Debian

$ sudo apt-get update
$ sudo install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update 
$ sudo apt-get install ansible

Managed node

We need a SSH communication from Control Node to Managed. We also need Python 2(version 2.6 or later) or Python 3 (version 3.5 or later) to be installed on Managed node.

Communication between Control and Managed nodes we need to prepare SSH passwordless access.

Preparing SSH keys to Managed Nodes:

We need to create and copy the ssh keys from Control node to Managed nodes. We will create a sudo user on all Managed nodes, and then from Control node we will copy ssh key to this newly created user on Managed nodes. In every Managed nodes I am create user name as linuxproguru (in your case it can be different user).

Create SSH key using below command:

$ ssh-keygen -t rsa -b 4096

Copy SSH key to Managed Nodes:

$ ssh-copy-id linuxproguru@172.16.125.160
$ ssh-copy-id linuxproguru@172.16.125.161
$ ssh-copy-id linuxproguru@172.16.125.162

Test SSH connection without password:

$ ssh linuxproguru@172.16.125.160

Building an Ansible Inventory

An inventory defines a collection of hosts that Ansible will manage. Inventory file holds the information of all your managed hosts which we needs to connects. Default inventory location is /etc/ansible/hosts, we can change the inventory location in configuration file called ansible.cfg.
In inventory file we can organize managed hosts into host groups. Using Host groups we can effectively run Ansible againts collection of systems by calling the Host group. Host group can created by using square brackets at the start “[“ and to the end “]”.

Host group exampe:

[web-server]
here “[web-server]” is our host group for our webservers, under this we can add our web-server IP addresses.

Here we are using default inventory file, I will explain how we can change inventory later.

Add our Managed hosts IP addresses or hostname to Inventory file using your favorite editor.

$ vim /etc/ansible/hosts

Adding our three hosts IP addresses:

[web-servers]
172.16.125.160
172.16.125.161
172.16.125.162

Hosts can be in multiple groups. Recommended practice is to organize your hosts into multiple groups to organized hosts dependending on the role of the hosts. We can create another host group of “Database-servers” or we can define it with physical location, or whether it’s in production or development, and so on. This will allow you to more easily apply Ansible plays to specific hosts.

Testing the Inventory:
$ ansible web-servers –list-hosts

Ping Managed nodes from Control Node:

We will try to ping all 3 managed nodes from control node. We will perform this action using Ansible Ad-hoc command, we will use command “ansible host-group -m ping” - here ansible is command, and -m (ansible module)

$ ansible host-group -m ping

Here host-group in our example is “web-servers” - which we have created in above steps for “Building ansible inventory”

$ ansible web-servers -m ping

 

Leave a Reply

Your email address will not be published. Required fields are marked *