Ansible is the most widely used DevOps tool for managing changes across your cloud or data center infrastructure. In this article, you will get an overview of how Ansible works and how you can get started with it.
Ansible is an automation platform tool used for easily deploying applications and system configurations on multiple servers. It can be used for starting Linux services, creating dozens of Linux users, updating passwords, and much more.
Ansible is an agentless solution that manages machines over the Secure Shell Host (SSH) protocol. It requires a database but it doesn’t use any background services (daemons).
Ansible uses built-in ad hoc commands and Ansible playbooks for deploying software or executing commands. Also, Ansible contains hundreds of modules for deploying or configuring remote nodes.
Now, you have a basic idea about what Ansible is. But for Ansible to work, you will need four components:
Working in IT, you’re likely doing the same tasks repeatedly. What if you could solve problems once and then automate your solutions in the future? Ansible is a configuration management tool that allows you to manage and deploy software on remote nodes easily and efficiently.
Ansible has hundreds of modules that can be used while managing remote servers. The Ansible modules are categorized into two parts:
? Ansible Galaxy refers to the Galaxy website where users can share roles, install, create, and manage Ansible modules.
If you ask me about the main use cases for Ansible, I cannot write an entire list as there are hundreds of things that Ansible can do. It starts from basic tasks, such as restarting services on remote nodes, to deploying Tomcat applications.
Let’s go through some of the use cases where Ansible proves to be useful:
Ansible is an easy-to-install tool that is supported on multiple operating systems. Let’s quickly look at which operating systems Ansible supports.
You cannot install Ansible on Windows? However, you can use it to manage Windows-based operating systems. Here are some possible use cases:
Before running your first Ansible command, you should know how to configure your Ansible Controller host, Ansible inventory, Ansible playbooks, etc.
The Ansible inventory file contains the list of all Ansible remote nodes or grouped remote notes, which Ansible uses while deploying or managing resources.
There are two different ways to store the Ansible inventory:
Also, there are two ways in which you can configure the Ansible inventory:
server1.abc.com [Group1] server3.abc.com server4.abc.com
Similar to what we explained above for the INI format code, two servers here (server3.abc.com and server4.abc.com) are members of group Group1.
all: hosts: server1.abc.com children: Group1: hosts: server3.abc.com server4.abc.com
If you need to perform a quick task such as restarting or starting a Linux service on an Ansible remote node, the Ansible Ad hoc command is here for you. Ad hoc commands are a quick and efficient way to run a single command on remote nodes.
For example, to ping all the Ansible remote nodes using the Ansible Ad hoc command, you will need to run the command below. Here, ‘all’ represents all the hosts configured in the Ansible inventory and -m is the flag used along with the ping module.
ansible all -m ping
When you need to execute multiple Ansible tasks simultaneously, you need to use Ansible playbooks.
Ansible playbooks allow you to deploy complex applications, and they offer reusable and simple configuration management. They also support multi-machine deployments, and they can be used to perform multiple tasks, multiple times.
Ansible playbooks are written in YAML format, and they contain multiple tasks executed in sequential order. An example of an Ansible playbook is shown below.
In this example, the Ansible playbook contains two Ansible tasks (1 and 2), which install Apache and Java on remote nodes.
# Playbook apache.yml --- - name: Ansible Playbook Examples hosts: servers # Define all the hosts remote_user: ubuntu # Remote_user is ubuntu # Defining the Ansible tasks tasks: - name: Ansible Task 1 apt: name: httpd state: latest tasks: - name: Ansible Task 2 apt: name: open-jdk state: latest
Although you now have a good idea about how Ansible works, what if you want to use it without having to manage or install it?
Ansible is hosted on a Linux machine. But if you wish to use the cloud version, without the headache of managing Ansible locally, then use Ansible Tower, which is a web-based solution. With the cloud version, you still use modules to configure or manage remote machines and execute Ansible commands or Ansible Playbooks.
Ansible Tower also allows you to control access for all users, taking care of SSH credentials and Ansible inventory. It also logs all of your jobs, integrates well with Lightweight Directory Access Protocol (LDAP), and has an amazing browsable REST API. Ansible Tower has the capability to scale servers and instances if you need to run multiple tasks on the remote nodes.
Ansible is a free tool with a whole lot of features. With this guide, you should now understand what’s possible with this feature-rich tool! From managing configurations, using various Ansible modules to executing Ad hoc commands, Ansible playbooks, and deploying dozens of software tools, Ansible is one of the most widely used automation tools.
Now that you have a good idea about how Ansible works, what do you plan to deploy with it?