Orchestrating digital Medley (Processes) by Automating with Ansible

Familiarizing with Ansible an orchestration engine to perform automation

PRADEEPA K
Bloggers Bay
7 min readOct 12, 2021

--

Automating Repetitive tasks using Ansible

My recent interest in automation drew my attention towards Ansible

Ansible Orchestration Engine

Ansible is an orchestration tool where it helps us to automate most of our repetitive manual tasks done with our systems.

Consider we have to deploy an application using any of the cloud providers what are the procedures we have to undergo,

Develop the application, code it connect it to a database, create an instance to publish, containerize the components and any of the packages involved, manage the resource groups, point to a URL or publish point, then comes the harder part, scale the application based on need, we have to do load balancing for utilizing the resources profitably and then get reports of the usage.

So having said these are some of the few steps involved (there might be sub steps too) imagine doing each and every time when we have to update or patch up few things in our existing application, desire to add on features itself will go off, even if the packages or libraries we are using in our application upgrades its version then we have to switch to the latest version in our application too, which is again tedious.

The second scenario would be let us consider we have a set of computers and we have to install a software in all of those, or do a batch update, check a particular URL is running in all of the regions, those all are boring tasks when asked to do individually for each system is again a headache to perform.

So here our Hero ansible comes for rescue, how so? We’ll come to that soon

What is Orchestration

So first what is orchestration, as the name makes us think of a group of people performing some musical performance and in front of them stands a Orchestrator directing them to make the performance at its best, now, the group of people might be good in each part, one might sing, other on keys, the other on trumpet, other on rap, lalalala (so on…)

Same way our set of tasks also have different parts to touch on which has to be managed on need so that our process won’t become a cacophony but when managed correctly will be a perfect digital medley, so here Orchestrator is our ansible and performing crew consists of various systems, processes which has to be handled

Ansible — An orchestration engine

Why ansible when there are a lot of options for automation like Robotic process automation, etc.,

Advantages of Ansible

A lot more use cases, usages ansible offers, which can be used based on need

Components of Ansible

Ansible has several parts to perform intended orchestration

1. Inventory

Where the set of hosts, IP addresses, domain URLs are all placed on which we intend to do automated tasks. Simply put it is a configuration file where all the host information is kept. Here we can group our hosts into different group also for batch processing

2. Playbook

Ansible playbook is YAML file with .yml extension, it is where the set of instructions are written to perform automation all the details like which host has to be chosen and what task is to be automated, triggers are all mentioned here

Usually a playbook is started with three hyphens and follows a syntax with indentations

Sub parts in playbook

i. Hosts:

Here either ‘all’ is mentioned to do the automation on all the hosts in the inventory, or ‘localhost’ for the currently using system, or specific group under which certain hosts are grouped

ii. Tasks:

The list of tasks which is automated is listed one by one with name, and service attributes for each task

iii. Name:

The name of the task to be performed is mentioned here

iv. Service or trigger handler or action:

Action to be done is mentioned here with its corresponding attributes. This may vary module to module

Finally the playbook is executed with the command

ansible-playbook playbook.yml

and corresponding parameters

3. Adhoc commands:

These are commands where simple task is executed for hosts mentioned in the command as parameters

Ansible into Action

So having wowed at ansible so much now let us look into some practical steps of execution

First we need to install ansible in our machine, linux environment is a better place to run and in windows some external tools like cygwin, can be used or using a virtual machine with linux os might help greatly for the operation

So just for experiment purpose I have run on a online linux terminal one such helpful website is ‘Linux Containers’

https://linuxcontainers.org/lxd/try-it/

In our online terminal first we have to update our packages using

‘apt-get update’ — try not to use with sudo as we use as a guest user in online terminals

apt-get update — command

Then simple command ‘apt-get install ansible’ or we can also try to install with pip but make sure python is of version 3 and above

apt-get install ansible — command
Installation

Once installed to confirm your installation, check with the command

anisble — version command

Now all set, we’ll start with a simple command to check the hosts we have connected respond to our server system using ping — pong

I have no additional hosts so for my localhost I am going to test ping command whether my system responds or not can be checked using this command

For this command we should get success and pong response

ping success message

Yeah we received so!!!

Next we’ll try to install NCDU a disk usage analyzer and we here keep a check that if the mentioned item already found we ask the script to not to modify anything when already present

ansible localhost -m yum -a ‘name=ncdu state=present’

We use the command

Installation Done

Done we installed NCDU and if hosts are connected we can use -host : all statement to install in all of them instead of doing manually each time

Now we’ll try to map our learning into a playbook as adhoc commands will be used to execute single task mostly we may often need playbook to perform orchestration, also we can separate tasks under different groups to perform grouped tasks for example we have to check a website is running in one region only then we can declare those hosts under one group in inventory and in our playbook we can use that host and write the script for executing this task

For this we’ll start with simple playbook again with ping

Playbooks start with — — —

So here it goes,

we can open a new file using the command, vi chkbk.yml

Playbook

Indentations are very important like python

So now we’ll execute our playbook using the command

After execution we can see this output as OK localhost is doing great and changed =0 as we didn’t install or modify any setup

Result

Once we’ll see how an inventory file will look like

Name this hosts.yml

Similarly will see how we can provision a EC2 instance from amazon web services

Note: this is not end to end we need to install few packages as prerequisite in order to work seamlessly

Similarly we can try to automate many tasks using various modules, ansible has various modules and this can be seen in official ansible documentation website with syntax

https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html

So that’s it from my end, ansible caught me off guard and gave me a jaw drop when it came along my automation path and hope it’s usages increasingly evolve with time.

End Note, shout out for all readers to try out few more automation using various modules from the ansible documentation and yeah any hurdles we can readily surf stackoverflow, that will always give us a hand to lend!

Happy Scripting!

--

--

PRADEEPA K
Bloggers Bay

I am a passionate software developer and Data Science Enthusiast, self motivated to solve real world problems