In this article, we are going to learn about Ansible Inventory basics and understand the various components involved.
Table of Contents
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.
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.
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.
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
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.
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.
Groups can be nested within other groups, enabling you to create a hierarchical structure for organizing hosts.
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.
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
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
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.