close

Home

DevOps

What is Ansible – The Ultimate Guide

Author avatar - Sagar

Sagar

|
DevOps code

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.

What is Ansible?

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.

How Ansible works

Now, you have a basic idea about what Ansible is. But for Ansible to work, you will need four components:

  1. The Ansible Control Node, also known as the Ansible Controller host, is the server where Ansible is installed. This node executes all the Ansible ad hoc commands and Ansible playbooks to deploy or manage the configurations or software on the remote nodes. 
  2. Ansible remote nodes, also known as Ansible managed nodes, are the servers or network devices where you deploy applications or configurations using Ansible ad hoc commands or an Ansible playbook. These are also known as Ansible hosts.
  3. The Ansible inventory is a file on the Ansible Controller host or Control Node which contains a list of all the remote hosts or managed nodes.
  4. Ansible modules, also known as Ansible core modules, are the code plugins or libraries plugins that can be used from the command line or a playbook task. Ansible executes each module, usually on the remote-managed node.

Why use Ansible?

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 core modules (or ansible-core) which we mentioned earlier, are the main building blocks and architecture for Ansible. They are owned and managed by the core Ansible team and will always ship with Ansible itself.
  • An Ansible collection is a distribution format for Ansible that you can use to package and distribute playbooks, roles, modules, and plugins. A typical collection addresses a set of related use cases. You can create a collection and publish it to Ansible Galaxy.

💡 Ansible Galaxy refers to the Galaxy website where users can share roles, install, create, and manage Ansible modules.

Uses cases for Ansible

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:

  • If you need to execute tasks and playbooks on different systems with a single command, you can do that with Ansible variables.
  • To retrieve data from multiple systems, you can use Ansible facts. Ansible facts are data gathered from target nodes and returned back to controller nodes.
  • The Ansible vault can be used for accessing sensitive data like passwords, or deploying applications with secure passwords.
  • Ansible has a lookups functionality for accessing databases and APIs to retrieve data from an external data store.

Ansible installation options

Ansible is an easy-to-install tool that is supported on multiple operating systems. Let’s quickly look at which operating systems Ansible supports.

Can Ansible run on Windows?

You cannot install Ansible on Windows? However, you can use it to manage Windows-based operating systems. Here are some possible use cases:

  • Gathering Ansible facts on Windows hosts. 
  • Installing and uninstalling MSIs.
  • Enabling/disabling Windows features.
  • Starting, stopping, and managing Windows services.

Ansible configuration management

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:

  1. By default, the Ansible inventory is stored in the /etc/ansible/hosts file.
  2. You can also use the customized path by specifying the -I <path> option at the command line.

Also, there are two ways in which you can configure the Ansible inventory:

  • INI format: An INI file is a configuration file that consists of group names in brackets to allocate hosts in different groups. In the example below, server1.abc.com has no groups allocated; however, server3.abc.com and server4.abc.com are part of Group1.
server1.abc.com

[Group1]

server3.abc.com

server4.abc.com
  • YAML format: Another way of declaring the Ansible inventory is using YAML, which is a superset of JSON, another data serialization language. The syntax of the YAML format is defined below.

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

Ansible Ad hoc commands

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 
Using an Ansible Ad hoc command to ping all Ansible remote nodes
Using an Ansible Ad hoc command to ping all Ansible remote nodes

What is an Ansible playbook?

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

Ansible Tower: A web-based solution

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.

Conclusion

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?

Related Articles:


More in DevOps

DevOps code

How to Combine Branches with Git Merge

Jun 17, 2022 | Robert Ralph

DevOps code

What is a DevOps Pipeline?

May 31, 2022 | Sukesh Mudrakola

DevOps code

What is Datadog – The Ultimate Guide

May 12, 2022 | Sagar

DevOps code

How to Use Git Bash on Windows

Apr 25, 2022 | Sagar

DevOps code

What is Ansible – The Ultimate Guide

Apr 7, 2022 | Sagar

DevOps code

What is Grafana: The Ultimate Guide

Mar 30, 2022 | Sagar

Most popular on petri

Article saved!

Access saved content from your profile page. View Saved