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.

/etc/ansible/hosts
1 host1.example.com

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
host1.example.com | 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"
host1.example.org | CHANGED | rc=0 >> hello

Additionally, privilege escalation can be achieved:

$ ansible all -m ping -u devops --become
host1.example.org | 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:

./ini_simple_inventory
1 one.example.com
2 
3[webservers]
4 two.example.com
5 three.example.com

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

YAML Inventory

YAML syntax provides a more expressive way to define inventory:

./simple_yaml_inventory.yml
1 ---
2 all:
3 hosts:
4 one.example.com:
5 children:
6 webservers:
7 hosts:
8 two.example.com:
9 three.example.com:

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.

./ini_range_inventory
1 [webservers]
2 www[01:99].example.com
3
4 [databases]
5 db-[a-f].example.com

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:

./ini_hostinventory
1 [webservers]
2 localhost ansible_connection=local
3 one.example.com ansible_connection=ssh ansible_user=devops
4 two.example.com ansible_connection=ssh ansible_user=ansible

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

./ini_groupsvariables_inventory
1 [webservers]
2 one.example.com
3 two.example.com
4
5 [webservers:vars]
6 ntp_server=europe.pool.ntp.org

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:

./ini_variableinheriting_inventory
1 [asia]
2 host1.example.com
3
4 [europe]
5 host2.example.com
6
7 [webserver:children]
8 asia
9 europe
10
11 [webservers:vars]
12 ntp_server=europe.pool.ntp.org

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:

./ini_local_inventory
1 localhost ansible_connection="local"

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

Conclusion

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 2
Joined: 1 month ago
Comments (0)

    No comments yet

You must be logged in to comment.

Sign In / Sign Up