What is Ansible?

What is Ansible?

In this blog, we’ll introduce you to Ansible, a versatile automation tool that finds extensive use in IT operations and software development. Ansible simplifies tasks with its user-friendly code, allowing you to manage servers, configure software, and automate workflows easily. It’s highly valued by DevOps and system administrators for its ability to enhance productivity while reducing manual labor.

What is Ansible?

To understand Ansible in an easy way, let us look at its past and see how it has helped system administrators to be more agile at work. 

On one hand, the developers were agile and were building and deploying code very frequently. The system administrators. On other hand, were often lagged behind due to the constantly configuring systems. Although there were many configuration management tools like Puppet, Chef, Saltstack, Juju, CFEngine, none did the heavy lifting.

If the system administrator needed to install 8 servers out of which 4 servers were web servers and the remaining were database servers then they had to install Tomcat to web servers and MySQL to database servers. The installation of multiple types of servers to perform different operations was an exhaustive task. The entire installation process also consisted of installing media players and involved a lot of intricate steps. 

This is where the prowess of Ansible comes into the picture and solves one of the major problems of multiple installations of servers. Using Ansible, it was easy to communicate the configurations in a single system and the server. 

Elevate your career with our expert-led courses!
Unlock seamless cloud management with Ansible while mastering essential DevOps tools.
quiz-icon

Here is a brief about the 3 types of tasks that Ansible automates:

Provisioning – It is a tool that can be used to set up servers in the target infrastructure.

Application deployment – By automating the deployment to your production systems of internally developed applications DevOps is made way easier.

Configuration management – It is used to alter the configuration of an application or device, install or update applications, implement a security policy, OS, device, start and stop services. These are only some of the configuration tasks that can be done.

Irrespective of whether Ansible is hosted on the traditional servers, cloud, or virtualization platforms, it is highly efficient to automate IT environments. Ansible configuration is also possible across various platforms like storage devices, firewalls, databases, etc. One of the interesting features that Ansible offers is that the user does not have to know the commands to perform a particular task. Only specifying what state a user needs to attain is enough!

Preparing for job interviews? Have a look at our blog on Ansible Interview Questions and excel your job interview!

Ansible architecture

Ansible architecture

In the world of Ansible, the architecture is gracefully simple yet powerful. At its basis, there are two key elements, the control node and the managed node. The control node is where you organize your Ansible tasks. It contains the inventory, which is a list of managed nodes, and the playbook, a file where you define the tasks and configurations to be executed on these nodes.

When you run an Ansible playbook, it sends these tasks to the managed nodes, which then execute the instructions and send back the results. Ansible doesn’t require any agent software on the managed nodes, making it efficient and easy to set up.

To learn  more about Ansible, check out this Ansible Tutorial!

What are the playbooks in Ansible?

Playbooks in Ansible are like a choreographer’s script for your automation tasks. They are the backbone of Ansible, designed to make automation accessible and efficient. Imagine playbooks as a set of instructions written in a language you can easily understand, telling Ansible what to do and how to do it. These instructions are structured in a step-by-step format, making it simple to follow along. A playbook typically consists of ‘plays,’ each representing a particular action that Ansible will perform on your target systems. These plays can be as diverse as configuring software, managing user accounts, or coordinating complex tasks across multiple servers. By using playbooks, you can ensure that your automation tasks are repeatable, consistent, and maintainable, saving you time and effort in the long run.

Ansible vs Puppet

Let’s look into how these robust configuration management (CM) tools differ from one another in this tutorial on Ansible.

  Ansible Puppet
Push vs pull Follows push workflow The client software in Puppet nodes periodically check Puppet master server to pull resource definitions
Server nodes No special master agent of special agent executables to install There are more than 1 puppetmaster servers along with special agent packages installed on each client node
Language extensibility Built on Python Built on Ruby and Ruby ecosystem is used to test Puppet applications
Syntax Ansible playbooks are YAML files Puppet’s domain specific language is a subset of Ruby.
Template language Based on Jinja2 which is a subset of Django’s templating language Based on ruby’s ERB
Resources and ordering Playbooks are applied top-to-bottom as they appear in file Resources defined in Puppet are usually not applied in order of their appearance

Also check our blog on Ansible vs. Jenkins to find out the best performing tool.

Chef vs Ansible

In this Ansible tutorial let’s look into how the tool compares with yet another CM tool called Chef.

  Ansible Chef
Workflow management Seamless. What one sees on disk is what one deploys. Relatively difficult.
Maintenance Known for the least maintenance Server and client components have to be periodically upgraded
Inventory Uses inventory scripts against EC2, Rackspace to put out a list of hosts and groups Nodes are registered with the server and will be part of the search
Simplicity Simplest CM tool Relatively sophisticated CM tool

Important Terms in Ansible

Let’s get a good grounding on various key concepts of Ansible.

Templates

A template file in Ansible is nothing but a text file with a .j2 extension. They are those files that are used to make the configuration files more dynamic.

Modules

Ansible works by connecting nodes and running modules directly on remote hosts or through playbooks. Modules control system resources like packages, services, files, and commands.

  • Service Module: Manages services on remote hosts.
  • Command Module: Executes commands on nodes without using a shell.
  • User Module: Manages user accounts and attributes.
  • Shell Module: Executes shell commands with arguments.
  • File Modules: Include Acl, archive, copy, fetch, file, find, iso_extract, lineinfile, patch, replace, stat, synchronize, tempfile, template, unarchive, xattr, xml.

Users can also write custom modules.

Copy file

To copy a file from the local or remote machine to a location on the remote machine copy module is used. The fetch module copies files from remote locations to the local box.

Shell

The command name is taken by the shell module followed by some space-delimited arguments. It is quite similar to the command module but it runs the commands through the command module through a shell on the remote node. Win_shell module is used for windows targets.

Handlers

They are those lists of tasks that are referenced by a globally unique name and are notified by notifiers. The handler will not run if nothing notifies it. Irrespective of how many tasks notify the handler it runs only once and that too after all tasks completed in a particular play.

Inventory

This CM tool can be made to run in one’s infrastructure against multiple systems at the same time. To accomplish this, portions of systems listed in Ansible’s inventory are selected which by default gets saved in location /etc/ansible/hosts. A different inventory file can be specified using the -I <path> on the command line.

Playbook

In Ansible, a playbook is like a roadmap for automation, organizing the instructions for Ansible to follow.

  • They are the central element in Ansible automation, guiding the process step by step.
  • Think of it as a recipe book, where each step outlines the tasks to be performed.
  • Playbooks are written in plain language, making them easy to understand, even for those without coding expertise.

Tasks

In Ansible, tasks are the core components of automation, similar to individual steps in a recipe. They define specific actions to be performed, such as installing software, creating user accounts, or editing configuration files.

  • Tasks are written in an easy-to-understand format, making them accessible to both beginners and experts.
  • They represent the actions Ansible will carry out in your automation process.

Roles

Roles in Ansible are organized directories containing predefined tasks, variables, and handlers, designed for reusability and consistency.

  • They follow a structured format for clarity and efficiency.
  • Example: A ‘web-server’ role can manage software installation, configuration, and services.
  • Roles promote modularity and collaboration, allowing easy sharing within the Ansible community.

They simplify complex tasks, making automation more streamlined and scalable.

Variables

In Ansible, variables act as dynamic placeholders for customizing automation. They store and manipulate data in playbooks and roles, enabling flexibility across different systems and configurations.

  • Variables can hold simple values (numbers, strings) or complex data structures.
  • They define settings like software versions or server configurations.

Variables can be global (accessible to all playbooks) or local (restricted to specific playbooks or roles).

Want to gain an in-depth understanding of DevOps? Avail this Certification in Cloud & Devops course offered by Intellipaat.

Installation in Linux

Compared to other configuration management tools the installation and setup of Ansible are really easy. Quite a number of distributions have a package in their 3rd party repositories that can be easily installed. Creating a user and all notifying needs of Ansible can all be achieved with ease.

The apt command which follows is enough to install Ansible.

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

APT expanded is an Advanced packaging tool which is the preferred Ansible package management toolset in Ubuntu. Packages can be installed, updated and also removed through this management toolset.

Read the difference between Terraform and Ansible in our comparison blog on Terraform vs Ansible.

Commands

There are a plethora of commands in Ansible but we’ll just give you a sample of a generic command.

  • ansible-playbook playbook.yml: Execute an Ansible playbook defined in playbook.yml.
  • ansible -m ping all: Check if all target hosts are responsive.
  • ansible -i hosts.ini -m shell -a “uptime”: Run a shell command (uptime) on hosts defined in hosts.ini.
  • ansible-vault encrypt secrets.yml: Encrypt a file with sensitive data using Ansible Vault.
  • ansible-galaxy install author.role: Download and install Ansible roles from the Ansible Galaxy community.
Kickstart Your DevOps Career with a Free Course
Free DevOps Training for Future Experts
quiz-icon

Ansible advantages

In the CM world, there are alternatives for Ansible like Puppet, Chef, Saltstack,Saltstack and many others. But let’s see what benefits Ansible deployment really gives out.

  • Ansible has fewer dependencies, making it stable and fast to implement.
  • It is agentless, allowing quick task execution without installing additional software on remote systems.
  • No database is required, making it portable and eliminating the need for a DBA.
  • Once playbooks run, the software used is removed, leaving no residual software.
  • Upgrading Ansible is easy, as it requires only updating code on the control system.

Learn about the important terminologies in our blog on Ansible Cheat Sheet!

To learn more about Ansible, watch this Youtube video by Intellipaat!

Video Thumbnail

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

The Ansible tool as specified came in 2012 and became known for its simplicity. It is one of the defining points of this tool. Of all the documentation on Puppet, Chef, and Ansible, the last tool is the easiest to follow. Puppet and Chef require Ruby, which is relatively hard to learn as compared to YAML used by Ansible. This is the reason why it is a great favorite among systems admins which you have known from this tutorial on Ansible

If you wish to be among the elite DevOps workforce, enroll in Intellipaat’s DevOps Training and gain your certificate.

Our Devops Courses Duration and Fees

Program Name
Start Date
Fees
Cohort starts on 14th Jan 2025
₹22,743
Cohort starts on 21st Jan 2025
₹22,743
Cohort starts on 14th Jan 2025
₹22,743

About the Author

Senior Cloud Computing Associate

Rupinder is a distinguished Cloud Computing & DevOps associate with architect-level AWS, Azure, and GCP certifications. He has extensive experience in Cloud Architecture, Deployment and optimization, Cloud Security, and more. He advocates for knowledge sharing and in his free time trains and mentors working professionals who are interested in the Cloud & DevOps domain.