Understanding Inventories in Ansible

Introduction

Ansible is a powerful automation tool that simplifies the management of IT infrastructure. One of the core components of Ansible is Inventories. Inventories in Ansible refer to a collection of hosts or machines that Ansible can manage. It allows administrators to define and group servers for automation tasks like configuration management, software deployment, and orchestrating infrastructure.

What is an Inventory in Ansible?

In Ansible, an inventory is a file that contains a list of nodes (machines or hosts) that can be managed by Ansible. This inventory file serves as the map to the servers or devices where Ansible commands are executed. Inventories can be static (defined in a file) or dynamic (generated by external scripts).

An inventory can also be divided into groups to categorize hosts based on common characteristics or functions, such as web servers, database servers, etc. By using inventories, you can define which hosts will be affected by the playbooks you run.

Types of Inventories in Ansible

There are two main types of inventories in Ansible:

  1. Static Inventory
  2. Dynamic Inventory

Let’s take a closer look at each type.

1. Static Inventory

A Static Inventory is a simple file, usually in INI or YAML format, where all the hostnames or IP addresses of the machines are listed. These inventories do not change unless manually updated. It is the most common type of inventory in Ansible, suitable for small-scale environments or when the set of managed hosts does not change frequently.

Example of a Static Inventory (INI Format):

[webservers]
webserver1.example.com
webserver2.example.com

[dbservers]
dbserver1.example.com
dbserver2.example.com

[all_servers:children]
webservers
dbservers

In this example:

The [webservers] and [dbservers] sections group the respective hosts.
The [all_servers:children] section groups both webservers and dbservers into one category.


Example of a Static Inventory (YAML Format):

webservers:
  hosts:
    webserver1.example.com:
    webserver2.example.com:

dbservers:
  hosts:
    dbserver1.example.com:
    dbserver2.example.com:

all_servers:
  children:
    - webservers
    - dbservers

2. Dynamic Inventory

A Dynamic Inventory is used in more complex environments where the list of hosts changes frequently or is pulled from an external source such as a cloud provider (AWS, Azure, etc.), a database, or a CMDB. Instead of manually editing an inventory file, dynamic inventories automatically generate the list of hosts at runtime.

Dynamic inventories are typically implemented using scripts or plugins provided by Ansible. For example, you could use a dynamic inventory script to fetch EC2 instances from AWS or VMs from VMware.

Example of a Dynamic Inventory
In the case of AWS, Ansible provides a plugin that dynamically pulls inventory data from your AWS environment:

ansible-inventory -i aws_ec2.yml --list

This command would pull the list of EC2 instances directly from AWS, without needing to manually edit an inventory file.

Advantages of Dynamic Inventories:

  • Reduces manual updates as hosts are automatically discovered.
  • Supports dynamic environments like cloud infrastructures where machines come and go frequently.
  • Can query different sources (e.g., cloud services, databases) for inventory data.

How to Use Inventories in Ansible

To use an inventory with Ansible, you typically pass the inventory file using the -i flag followed by the path to the file when running an Ansible command or playbook.

Example Command for Running a Playbook with a Static Inventory:

ansible-playbook -i inventory.ini playbook.yml

This command will run the playbook against all hosts defined in inventory.ini.

Example Command for Running a Playbook with a Dynamic Inventory:

ansible-playbook -i aws_ec2.yml playbook.yml

In this case, aws_ec2.yml would be a dynamic inventory file pulling data from AWS.

Best Practices for Managing Inventories in Ansible

  1. Organize Hosts with Groups: Group hosts by functionality (e.g., webservers, dbservers) to manage them more effectively and run targeted tasks.
  2. Use Variables for Groups: Assign variables to groups to make configurations easier. For instance, you can set different configurations for webservers vs dbservers.
  3. Combine Static and Dynamic Inventories: You can use both static and dynamic inventories in Ansible. For instance, you may use a static inventory for your internal servers and a dynamic inventory for cloud servers.Organize Hosts with Groups: Group hosts by functionality (e.g., webservers, dbservers) to manage them more effectively and run targeted tasks.

Ashutosh Dixit

I am currently working as a Senior Technical Support Engineer with VMware Premier Services for Telco. Before this, I worked as a Technical Lead with Microsoft Enterprise Platform Support for Production and Premier Support. I am an expert in High-Availability, Deployments, and VMware Core technology along with Tanzu and Horizon.

Leave a Reply