Automation with Ansible

Dipto Chakrabarty
6 min readFeb 1, 2020

Automation is the new growing trend in the market , companies are heavily relying on automation to bring agility and also make their tasks easier , not to mention that its also fun ….

What is Automation

Before Automation , let’s say you have multiple servers working in full production and suddenly you have to make changes on several servers , now if you were to do this manually you might face issues such as

  • Run commands at each server
  • Running your commands at each server individually will consume a lot of time
  • Along with that there can be issues of different commands and dependencies along with different OS versions or systems
  • Overall the entire process will require a huge amount of time and in case of any error due to unknown dependencies or commands there will be a greater time demand
  • Companies nowadays need agility as they need their services deployed continuously and always need to update their applications .

If you are a system administrator managing servers by hand manually was a real challenge. Installing software manually , configuring services ,setting up tasks and processes everything was done by system administrators.

As the services grew so did the number of servers required and it become more complex and time consuming to manage all the servers efficiently . There was a demand for agile and simple method to automate your tasks.

Automation is the process of minimizing human assistance for a task , automation is slowly gaining popularity due to the huge demand required in almost all companies.Automation is the use of instructions to create repeated instructions that replaces an IT professionals manual work . Automation in any field brings benefits of increased productivity and cost reduction .

In this post we will talk about Ansible a configuration management, and application-deployment tool. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

Why Ansible

  • Ansible is really fast , only depends on ssh
  • Simple and easiest to learn
  • Ansible is Agent less that means you only have to configure the master node unlike other languages where you have to configure the client nodes as well.
  • Collection of large number of modules that help in a wide array of tasks from web server configurations to launching ec2 instances and much more.
  • Large Community and fastest growing automation tool.
Ansible is the most easy to setup compared to other tools and also easy to learn

Before we dive into the workings of ansible we need to understand how ansible works , consider ansible as a manager for a office who manages different departments like finance , technical support etc . The manager manages the different departments , he assigns tasks to each department based on what is required and what he understands , the important point is that he himself does not perform the task.

Similarly ansible goes to the remote server and only tells what to do and how to do , the actual task is carried out by the different services for example to install a package ansible will tell apt-get (package installer for Ubuntu) or yum(package installer for Red Hat) what to install and itself not install it . The specialty of ansible lies in the fact that it will automatically know which service or manager to contact and the system administrators do not have to specify that ansible does that job for us.

How to install Ansible

Installing Ansible is simple just go to your command prompt and type

pip install ansible

another way to install ansible is to do this

  • sudo apt-add-repository ppa:ansible/ansible
  • sudo apt update
  • sudo apt install ansible

The first line adds a repository to your apt which contains ansible

Then you update your system

Then install ansible

Make sure you have pip installed first (if you are doing by first method), once done you can verify your install by

ansible — version

Ansible Configuration

Once you have installed ansible you have to configure it

There are two very important files required to configure ansible

Both the files are present in /etc/ansible/ directory

The first one is the hosts file , this file contains the ip address or host names of the servers we want to configure.

Take an example of this image , the controller node (ansible management node) is where you will be setting up ansible.

The hosts file will contain the ip addresses or host names of node 1, node 2 … node n. Whenever ansible wants to do some task it will go to the hosts file and check in which nodes it has to perform its tasks , it helps in identifying the managed nodes.

The second file is ansible.cfg file

This is the main configuration file for ansible , here we specify all the rules for our ansible controller .

I have uploaded both the files in a git hub repository which link is given at the end of the blog.

Let us see an example where Ansible can help us

Httpd web Server configuration

You want to deploy a httpd web server in a remotely

This is called a playbook , where we can write our codes and execute them.

To execute this code we write

ansible-playbook httpd.yml

You should see a similar output

What this playbook is doing is

  1. Using the package module to install httpd. Ansible will first check if httpd is installed or not , if installed it moves on to the next step or else it first says to install httpd to the servers respective package manager (yum or apt-get).
  2. It then copies a simple html web page which I have put in the remote nodes web server.
  3. We must start httpd process once this is done which is done using the service module.
  4. Once everything is done we configure the firewall to only allow access through port 80 (default httpd port).

Now to check if it worked , download web page to check its contents.

This is the same content that I uploaded the web server. The whole process took hardly less than half a minute if I would have done this manually the time required would be at least a minute now imagine doing much more complex tasks in multiple servers.

Ansible is the most rapidly growing automation tool currently , Ansible gives a steady domain for advancement and activity for coordination and along these lines, brings about smooth organisation. Along these lines, if you are a DevOps proficient, begin learning Ansible at this point .

If you want to try this out yourself go ahead and clone this repository and get started https://github.com/DiptoChakrabarty/Ansible-Example

If you liked this blog please give some claps :) .

--

--

Dipto Chakrabarty

Site Reliability Engineer , I talk about Devops Backend and AI. Tech Doctor making sure to diagnose and make your apps run smoothly in production.