Configuration Management and Continuous Deployment

Fundamentals of Ansible

Anilkumar Patel


What is Ansible?

Ansible is a simple, reliable and secure configuration management & automation tool. Like Puppet, Chef and Salt, it can configure the server in a very efficient way. It allows us to write the templates once and use them across our entire infrastructure. Ansible playbook is written in Python (YAML) format which makes it easy to read and understand the code.

Why Automation?

Manual configuration is a very tedious job where we have to login into each server and configure it manually. If any error occurs, then we first need to identify the cause of the problem on that server and then correct it. There are several other challenges in manual configuration, such as

  • Very time consuming:- Without any configuration management tool it is hard to configure multiple servers. We need to login into each server manually & configure the servers and we have to ensure that all the things are configured in the right manner.
  • Error-prone:- Manual configuration is error prone. If we are configuring multiple servers manually then the possibility of error is more.
  • Hard to maintain consistency across servers:- For e.g.: if we want to change the Java version from 1.7 to 1.8 then we have to remove Java 1.7 completely from all servers and then again reinstall Java 1.8 in all servers, so it becomes very hard to maintain all servers at the same time.

To mitigate the above problems, we carry out automation using configuration management tools like Ansible, Puppet, Chef, and Salt. These tools are capable of managing and maintaining the servers inefficient way.

Tools for automation


Why Ansible

Ansible is a simple & reliable configuration management tool that makes an application very easy to deploy. It supports configuration management with examples as below.

  • Application deployment:- Ansible helps us to deploy your application as fast as possible. All we need to do is list the application in terms of tasks in our Ansible playbook and sit back while Ansible executes these tasks in order.
  • Configuration of servers:- With the help of Ansible we can configure multiple servers at a time, without any user intervention.
  • Continuous testing of installed applications:- For e.g.:- we develop an application that is built in Java, and it has some prerequisite requirements like having JDK & JRE installed. For this, we will need Java installed on your computer. Ansible ensures that the required packages are downloaded and installed.
  • Automation of tasks:- For e.g.:- If we want to install many applications like Hadoop, Zookeeper, Pig, Hive, Kafka etc., we have to configure each application in our task and write the steps that are needed for installing the above to our task file. After that Ansible parallelly executes all our task in all server machines one by one and ensures that all are correctly installed.
  • Write once:- We need to write tasks only once and we can use it anywhere.
  • High flexibility:- Highly flexible configuration management of systems because a large number of ready-to-use modules for system management are already in-built in Ansible. Custom modules can be added if needed & Configuration roll-back in case of error. For e.g.:-

Here is what I have done in the above code:

  • I have used Ansible register variable result, for script execution and output into result variable and ignore errors, if any.
  • By default the Playbook is executed in a linear fashion. So, the “command” task is completed on all the hosts (all the host IPs are mentioned in ansible inventory file), before the next task (“debug”) is executed. Also, we have “result” register for every host.
  • Now the next step would be to rollback all the host IPs that have failed to execute the file. For this to happen, we would extract the “result” register, which has IPs that have failed and executed with their return code. We will then add “result” register values to a list with their return codes. And lastly, we will compare the return codes that have value greater than 0.
  • Rollback task will be executed for all hosts which is failed result.

You have to add this task after rollback task:

This is the way you have to rollback your tasks done and also mark problem hosts as failed.

Ansible Architecture

As we can see in the diagram above, the Ansible mgmt node is the Ansible server which is responsible for configuring playbook over the server (Ubuntu/trusty64 or centos). A user needs to write scripts in a playbook and mention the IP Address in the inventory host. Just by running the ansible command, the user will be able to install scripts which are written in the playbook in each server and ensure that all the servers have passwordless ssh.

Concepts and Terminology

  • Ansible mgmt node:- Ansible management node is the machine where the Ansible is installed & which is responsible for configuration of all server machines.
  • Inventory:- Inventory is a file which consists of IP Address of all servers on which configuration is to be done.
  • Playbook: Playbook is used to manage configuration which can be deployed on the remote machines. Playbook is designed in simple human-readable format, so that it becomes easy to understand the code.
  • Task:- Task is the file where a user defines the steps that he needs to perform on the Playbook.
  • Module:- Ansible has in-built modules that can be executed directly on remote hosts or through Playbooks. Users can also write their own modules. These modules can control system resources, like services, packages, or files or handle execution of system commands.
  • Role: Role is used for organizing Playbooks and other files in order to facilitate sharing and reusing portions of a provisioning.
  • Play: A provisioning executed from start to finish is called a Play.
  • Facts:Used to get the system information while running the playbook, like network interfaces or operating system.
  • Handlers: Handlers which are similar to a task, can be used to restart or to stop a service.

I have shared a GitHub Link on how to install Java using Ansible.

Feel free to share and clap. To know more about what we do and how we can help you, please visit



Anilkumar Patel

Big Data And DevOps Engg @ Formcept. Currently interested into Devops and machine learning.