Ansible for Beginners: Part-2

Ansible for Beginners: Part-2
4 min read
14 February

In the changing world of DevOps, where speed, scalability and efficiency're crucial automation has become a key factor for success. Among the automation tools ansible automation platform has gained significant popularity among DevOps professionals worldwide. In this guide we will explore the ins and outs of Ansible including its architecture, core concepts and how it can transform the automation landscape for DevOps experts.

Understanding Ansibles Architecture

At the heart of Ansible lies its architecture that revolves around two components; the control node and the managed nodes. The control node acts as a hub where Ansible is installed and takes responsibility for managing a fleet of nodes known as managed nodes. These managed nodes can encompass operating systems like Linux, Mac, Windows or various network equipment.

What sets Ansible apart is its utilization of the SSH protocol to manage connections without requiring any client on the target machine. This stands in contrast to some competitors that mandate installing agents on managed nodes. Ansibles brilliance lies in its simplicity. It only requires a username and certificate, for SSH access.

When it comes to Windows targets Ansible smoothly integrates with WinRM technology. Relies on PowerShell as the interpreter. This demonstrates its ability to work well in environments, with characteristics.

Getting Started with Ansible

Basic Inventory Setup

The inventory, a catalog of managed hosts, serves as a linchpin in Ansible's operation. Stored in /etc/ansible/hosts by default, the inventory provides essential information about hosts and their groupings based on roles or functionalities.


Running Your First Ansible Commands

Ansible commands, often referred to as modules, are executed on managed hosts. A simple ping command exemplifies Ansible's capability to validate connectivity:

$ ansible all -m ping | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

This output signifies successful communication with the managed host, highlighting Ansible's proficiency in connecting via SSH and executing commands.

Ad-Hoc Commands and Privilege Escalation

Ad-hoc commands facilitate quick task execution. For instance, running the echo command on all hosts:

$ ansible all -a "/bin/echo hello" | CHANGED | rc=0 >> hello

Additionally, privilege escalation can be achieved:

$ ansible all -m ping -u devops --become | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

This example demonstrates executing the ping module as the root user after logging in with the "devops" user.

Ansible Inventory: Organising Managed Hosts

Simple INI Inventory

Ansible supports INI-style inventories, where hosts are listed under groups:


This example showcases a simple inventory with ungrouped and grouped hosts.

YAML Inventory

YAML syntax provides a more expressive way to define inventory:

1 ---
2 all:
3 hosts:
5 children:
6 webservers:
7 hosts:

Here, the same inventory is expressed using YAML, offering flexibility and readability.

Advanced Inventory Management

Inventory management becomes crucial as infrastructures scale. Ansible allows for advanced techniques like defining ranges of hosts and organising hosts into multiple groups.

1 [webservers]
2 www[01:99]
4 [databases]
5 db-[a-f]

This example illustrates using ranges for hosts in the "webservers" and "databases" groups.

Host and Group Variables

Customizing connections and user variables for specific hosts or groups enhances flexibility:

1 [webservers]
2 localhost ansible_connection=local
3 ansible_connection=ssh ansible_user=devops
4 ansible_connection=ssh ansible_user=ansible

This inventory defines different connection types and users for specific hosts.

1 [webservers]
5 [webservers:vars]

Here, the "ntp_server" variable is defined for all hosts in the "webservers" group.

Inheriting Variable Values

Combining hosts and groups allows for inheriting variable values:

1 [asia]
4 [europe]
7 [webserver:children]
8 asia
9 europe
11 [webservers:vars]

In this example, the "webserver" group inherits variables from the "asia" and "europe" groups, showcasing hierarchical organization.

Advanced Ansible Usage

Multiple Inventory Sources

Ansible supports using multiple inventory files for execution, enabling versatility in managing diverse environments:

$ ansible-playbook playbook.yml -i production -i development

This command executes a playbook against both "production" and "development" inventories.

Localhost Inventory

Handling localhost requires specifying the connection type as "local" to ensure proper execution:

1 localhost ansible_connection="local"

This special case ensures Ansible uses the "local" connection for tasks on the localhost.


Ansible, with its community-driven roots and Red Hat's enterprise support, has become an indispensable tool for DevOps engineers. Its simplicity, versatility, and powerful features make it a go-to solution for automating infrastructure management. From basic ad-hoc commands to advanced inventory organization, Ansible empowers DevOps professionals to streamline their workflows and focus on innovation.

 For any  custom software development ,digital transformation services solutions visit our websites.

In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.
Aman 88
Joined: 4 months ago
Comments (0)

    No comments yet

You must be logged in to comment.

Sign In / Sign Up