Ansible Inventory Deep Dive

In this article, we are going to learn about Ansible Inventory basics and understand the various components involved.

Ansible Inventory File

An Ansible inventory file is a crucial component of Ansible, an open-source automation tool for configuration management, application deployment, and task automation. It serves as a structured list of target hosts or nodes that Ansible can manage or communicate with.

Purpose

The primary purpose of an Ansible inventory file is to define and organize the hosts and groups of hosts that Ansible will interact with during playbook execution. It specifies the hosts where Ansible will perform tasks or configurations.

Structure

The inventory file is typically written in plain text or YAML format, and it can include the following elements:

Hosts or IP

Each host entry consists of a hostname or IP address. You can specify a single host or a list of hosts, making it flexible for managing multiple systems.

webserver.example.com
appserver.example.com

192.168.1.20
192.168.1.21

Host Variables

You can define variables specific to a host in the inventory file. These variables can be used in Ansible playbooks to customize configurations for individual hosts.

webserver.example.com ansible_ssh_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa

Groups

You can organize hosts into groups based on their roles, functions, or other criteria. Groups allow you to target multiple hosts at once using Ansible playbooks.

[web]
webserver.example.com

[app]
appserver.example.com

Group Variables

Similar to host variables, you can define variables specific to groups. Group variables can be inherited by all hosts in that group, simplifying the management of configuration data.

[web]
webserver.example.com

[app]
appserver.example.com

[web:vars]
http_port=80

[app:vars]
app_port=8080

Nested Groups

Groups can be nested within other groups, enabling you to create a hierarchical structure for organizing hosts.

[production]
webserver.example.com
appserver.example.com

[development]
devserver.example.com

[all:children]
production
development

Default Inventory File Locations

Ansible looks for the inventory file in default locations. Common default locations include /etc/ansible/hosts for system-wide inventories and a project directory for project-specific inventories. You can specify a custom inventory file using the -i option when running Ansible commands.

Dynamic Inventories

In addition to static inventory files, Ansible supports dynamic inventories that can be generated by scripts or external sources, making it easier to manage large and dynamic infrastructures.

Example Inventory File

Here’s an example of an inventory file in YAML format:

# Inventory file for Ansible

[web]
webserver.example.com

[app]
appserver.example.com

[database]
dbserver.example.com

[web:vars]
http_port=80

[app:vars]
app_port=8080

[database:vars]
db_port=3306

[all:vars]
ansible_user=admin
ansible_ssh_private_key_file=~/.ssh/id_rsa

This inventory file defines three groups (“web,” “app,” and “database”) with associated hosts and group variables.

Ansible Inventory Best Practices

  • Use Inventory Groups
  • Separate Inventory per Environment E.g Dev and Prod
  • Use Dynamic Inventory for clouds

Conclusion

The Ansible inventory file is a crucial component for managing infrastructure and automating tasks across a variety of hosts, making it an essential tool for system administrators, DevOps engineers, and automation enthusiasts.

Ansible Tutorial on Youtube

We found out that some students or learners understand better through video examples. So, you can learn Ansible through our YouTube playlist here.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top