Ansible – A Brief Introduction

What is Ansible

Ansible is an IT automation engine that makes managing, configuring and deploying on multiple numbers of systems a lot easier.Like all configuration management systems, it helps to bring the targeted infrastructure to the required state with minimal manual effort.
Where it excels, in comparison to other configuration management systems are

    • Agentless – You need to install ansible only on the control machine. Remote machines are connected via SSH.
    • Idempotent – Once the system is in the required state, even if you run the tasks again no state change happens.
    • Documentation – It has a very thorough and simple documentation.
    • YAML – Ansible uses YAML syntax in its playbook which has a shallow learning curve
    • Open source – Ansible is Open Source. You can make contributions at https://github.com/ansible/ansible.

Let’s take the case where you have to configure 100 machines in which you have to install some packages. If you intend to automate the task and are writing a bash/python script for this task then you have to write multiple functions to connect to the systems, to install the packages, to check if the system has the package already installed, to check if the package was installed correctly etc.

But with ansible, you can do all the above steps in a single task.

eg: apt:
     name: git
     state: latest
     update_cache: yes

The above task will connect to the machine, update the cache, check if the git package is already installed, whether it is the latest version. It will return a success message if it is successful. Or an error message with details for failure. If nothing is changed then it says nothing is changed.

And even if you run the same script again Ansible will understand what the current system status is automatically and wouldn’t run the installation again.

There are a number of modules like the apt module given above. These modules provide a lot of functionalities. You can also add your own modules if necessary.

The documentation provides details of all its modules at modules

Ansible can be run on any machine with Python 2.6 or 2.7 installed.

How ansible works

Ansible needs to be installed on just one machine. Let’s call that machine the control machine.You also need to specify the systems on which you are aiming to configure. These will be the remote machines. Ansible connects to the remote machines via SSH and executes the tasks via modules.

The only prerequisite before you run ansible is that you need to enable SSH connections to your remote machines from your control machines. Ansible internally uses OpenSSH or paramiko for SSH connection.