Overview of Ansible
Ansible is an information technology automation tool. By using Ansible, it gets our task done easily by automating it by removing the manual deployment. It can configure systems, deploy software, orchestrate more advanced it tasks such as continuous deployment, zero downtime while rolling and more. Here orchestrate means to run tasks in sequence and create a chain of events which must happen on several different servers or devices and Continuous Deployment means to deploy software over server after all continuous integration and continuous deployment.
Goals of Ansible
- Simplicity as it is relevant to all whether it is developers, IT managers etc.
- Ease of use.
- Strong focus on Security as Security is our major concern.
- Reality as all the work done should be reliable.
- Usage of openSSH for transport.
- As Ansible is appropriate for managing all environments from setups with limited instances to enterprise environments with many thousand of instances.
- As Ansible manages machine in agent less manner.
- As Ansible is decentralized, it relies on existing OS credentials to control access to remote machines.
- As Ansible uses openSSH and it one of the most peer reviewed open source components, security exposure is greatly reduced.
- Ansible manages remote machines.
How Ansible Works?
There are many use cases but this is one of the most common ways most people use Ansible. So here we have the non ansible way so we have a user and an admin and admin is maintaining four servers the three servers are web servers and one of the server is a database so, admin wants to install Tomcat then the admin will have to install Tomcat on server one, then install Tomcat on server two and install Tomcat on server three and then install MySQL on the database server, so basically the admin has to repeat all the steps again and the chances are here that he or she can make a mistake and the server's may not look same so, there comes ansible tool which makes life easier.
Admin instead of issuing commands to the server’s individually, will issue a command to one machine and that machine will communicate with the other machines so admin can just issue one command say install Tomcat on all the web servers and then this machine will actually go ahead and install Tomcat on the other machines in our infrastructure and then the admin can say issue a command call say install MySQL and then the machine will go ahead and install MySQL on the database server so here the task of admin is reduced the admin doesn’t have to maintain multiple machines.
Now imagine a case if a company has 40 web servers and you don't have to like install Tomcat on all 40 machines manually but you just have to issue a command once and then let ansible do the rest for you for that. Ansible uses two major files and to begin with there is an inventory file and there is a playbook.
So, inventory file as the name suggests contains an inventory list. This file consists of a group or a label and then IP addresses and then there is a group for database servers which contains the list of database servers. That way we have grouped our web servers and database servers so then using this inventory file we can issue command saying okay now install Tomcat on the web server, then Ansible will look in this inventory file and look at the web server IP addresses and then issue commands to those particular servers.Let’s look at playbook.
Now, the playbook consists of multiple plays and each play consists of tasks. It contains multiple plays. The playbook is in Yaml format. So it starts with three dashes always and then each play consists of a dash. So here we there is one play and then we have target over here which is the web server, So we are targeting this play on web servers and then a playbook consists of multiple tasks each task has a name. So over here we are going to run multiple tasks on our web servers and is at the latest version that’s one of the tasks and thenll run the commands for the tasks and then there is another another task which says write the Apache config file and then run the commands for that and then there is another task which says ensure Apache is running and then we'll write the commands of how will ensure that Apache is running so these are multiple tasks inside a play then we can write another play after this and which could be saying install mysql on the database servers and ensure it's up and running so basically to recap again a playbook consists of multiple plays and each play consists of multiple tasks.
Once you install Ansible, it will not add database automatically and there will be no daemons to be started or running. A Daemon is a computer program that runs as a background process rather than under the direct control of an interactive user. Ansible is written in python language, so its need to be installed on the remote host.
What Version to Pick?
As it does not require any installation on remote machines, users generally took development version. As release cycle of ansible is generally four months long , minor bugs get fixed in next release cycle and major bugs in more release cycle called as maintenance releases.
Cost of Ansible
Ansible is an open source software and its free to use and it runs on linux operating system, however it has a paid version which is an enterprise product called ansible tower.
The first tests – After the installation of the ansible, we have to install some hosts to the inventory file from which we have to connect with,typically /etc/ansible/hosts. By default a connection will be made with ssh keys. To test, we can just ping all or if we want to ping with a group, we can specify the group name.
Command to ping is –
ansible all -m ping
Here the all refers to all the host in your inventory.
Command to deploy is –
ansible all -m ping -u deploy
Here connection is made with a user and the connection name is deploy.
Features of Ansible
- Open Source
- Written in python, so it’s easy to read and extend
- Easy installation and configuration
- Highly Scalable
- Agent less client configuration
Requirements of Ansible
- SSH Client
Components of Ansible
Ansible Playbooks – The real strength of ansible is that it all lies in its playbooks. A playbook is like a recipe or an set of instructions which tells Ansible what work it has to do when it connect to new machine. Playbooks are written in YAML, which simply could be viewed as XML.
For Example – if we have a group of web servers, database servers and monitoring servers and if we decide to add a web server then we will fire our playbook which is set of instructions, ansible will then install and configure web server and will make sure that our database allows connection from the new server and then it will add new server to our network monitoring solution.
- Plays – playbook contain plays. Plays are generally a group of tasks that are performed on defined hosts to perform defined functions.
- Tasks – Tasks are action carried out by playbooks.
- Roles – A role is bundling automation content and making it reusable.
Roles are organizational components that can be assigned to set of hosts to organise tasks.
- Handlers – Handlers are similar to tasks except that a handler will be executed only when it is called by an event.
- Templates – Templates are files based on Python’s Jinja2 template engine and have .j2 extension.
Why developers should choose Ansible
- Great configuration
- Easy to Learn
Automation Use Cases
Ansible was used by the DevOps team to configure computing clusters. It address the challenges involved in managing the configuration of our computing infrastructure. It is an excellent complement to Terraform because Terraform manages the cloud infrastructure creation. Once the cloud infrastructure is created, Ansible can be used to configure the instances. Things like configuring the services on the instances as well as installing and configuring the software can be handled by Ansible.
An Approach to Automation
Ansible enables faster Installing and configuring software on instances as it Lightweight footprint i.e. it uses less space and has Predictable execution.To know more about Automation we recommend taking the following steps –