What is Ansible?

I am writing this article on the basic understanding on what is Ansible, uses, benefits and how to use it.

What is Ansible?

Ansible is an open source configuration management tool and most popular in the market from many years

  • It is used for configuration management
  • It can solve wide range of automation challenges
  • This software developed by Michael DiHaan.
  • Ansible named after a fictional communication device, first used by Ursula K. LeGuin in her novel Rocannon’s World in 1966
  • Red hat has acquired Ansible in 2015.

Why Ansible?

If you search Ansible in google trends, it will show its popularity graph than other configuration management tools

Ansible Trend

Advantages of Ansible:

  • Easy to learn
  • Written in Python
  • Easy installation and configuration steps
  • No need to install ansible on Slave
  • Highly scalable

 

Popularity of Ansible:

Many companies and big organization uses Ansible for configuration management.

Ansible Popularity

 

How Ansible Work?

Ansible is written in Python language. Ansible is using Ansible playbook, which are written in a very simple language, YAML.

We take an example of Josh who runs an enterprise and he wants to install new version of Apache Tomcat in all the systems

CM test case

For other configuration management solution, he needs to install plugins on all the systems to interact with central server or need to login to each system and update the Apache Tomcat.

However with the help of Ansible, Instead of going to each systems, Josh can use the Ansible to automate the installation using Ansible Playbooks.  He also does not need Ansible plugins on other slave systems.

Ansible Use

Ansible Architecture:

This below diagram is basic Ansible architecture in which each component is doing important role to update the slave systems.

Ansible architecture

Playbook

  • It describe the tasks to be executed
  • Written in simple YAML language
  • Playbook are like instruction manuals

Inventories

  • It contains list of hosts
  • It has information from where the playbook tasks will be operated

Modules

  • Modules are like tools
  • Can control system resources like services, packages, etc.
  • There are 500+ core modules available
  • It also allows custom modules for use

 

The case study for Ansible

XYZ company wants to move its 65+ applications from traditional hardware based data Center to Cloud based environment for better agility and cost saving.

Ansible_CaseStudy1

They can use Ansible to manage and schedule the cloud environment

Ansible_CaseStudy2

Now benefits for XYZ company after migration

  • It provide better operations and security to its clients
  • Increased team efficiency
  • Patching updates went from a multi-day process to 45 minutes with the help of Ansible

 

Creation of Ansible Playbook

Ansible Playbook is an organized unit of scripts which defines the work. All server configuration written in YAML file. The file extension is .yml.

Please find the Ansible Playbook Structure in below diagram

Ansible Playbook Structure

  • Playbooks are the number of Plays
  • Plays contains the tasks information
  • Tasks calls core or custom modules
  • Handlers gets triggered from Notify and executed at the end only once.

Example of creation of Ansible Playbook

We want to create two plays and each play are executing two different tasks

Ansible Playbook

Then playbook will be created as shown below

Ansible Playbook Ø  Start YAML file with three – (—)

Ø  Single ‘-’ indicates an item in the list

Ø  ‘hosts’ can have one host or group of hosts from the inventory file /etc/ansible/hosts

Ø  Each play like a dictionary in the name: Play1 and it contain name , hosts , tasks and order doesn’t matter

Ø  So we can say playbook is a list of dictionaries

Ø  Similarly tasks is denoted by (-)

Ø  For tasks ordered collection, position of entry matters

Ø  First entry of tasks get performed first

 

 

Create a test_script.sh using ‘sudo nano #filename#’ and create playbook as show in the example and save the file.

Ansible Script

Syntax check and execute ansible playbook using ansible-playbook <playbookname>

Ansible Script run

Ansible Roles

  • Ansible roles simplifies writing complex playbooks
  • Roles allows you to reuse the common configuration steps between different types of servers
  • Roles are flexible and can be easily modified.

Structure of an Ansible roles consists of below given components

  • Defaults: Store data about the role, also store default variables
  • Files: Store files that needs to be pushed to the remote machine
  • Handlers: Tasks that get triggered from some actions
  • Meta: Information about the Author, supported platforms and dependencies.

Ansible Role

Leave a Comment