LinuxUnity LinuxUnity

Posts / Ansible

[Ansible] Part 3: How to Create and Manage Inventory Effectively in Ansible

Learn what Ansible inventory is and how to create and organize it to manage remote hosts effectively.

11/07/2026 5 min read 9 views

Z Zedx

In this article

What is an inventory?

An inventory (also known as an inventory file) is a list of servers or devices (remote hosts) that Ansible manages and on which it performs tasks. An inventory contains the IP addresses or domain names of the servers, along with the groups and variables associated with each server. This enables Ansible to connect to these servers, configure them, and execute commands or playbooks on them.

An inventory can be defined in a text file (typically named hosts or inventory), or it can be dynamically retrieved (dynamic inventory) from other sources such as cloud services.

An Ansible inventory file is written in a plain-text configuration language, not a specific programming language. Its structure is very simple and usually follows the INI format (a text file containing groups and key-value pairs).

Alternatively, an inventory can be written in YAML, a human-readable data format commonly used in Ansible, particularly in playbooks.

An important note: If Ansible does not have an inventory file, it will not know which systems it can manage.

How do you build an inventory?

In the previous article, I briefly covered how to define an inventory for remote hosts that use SSH passwords. This article focuses on SSH keys.

  • For remote hosts that use SSH passwords:
text
<name_server> ansible_ssh_host=<IP> ansible_ssh_user=<user> ansible_ssh_port=<port SSH> ansible_ssh_pass=<password SSH>

–Guide to defining an inventory with an SSH password

  • For SSH keys:
text
<name_server> ansible_ssh_host=<IP> ansible_ssh_user=<user> ansible_ssh_port=<port SSH> ansible_ssh_private_key_file=<đường dẫn lưu file private key>

When using SSH keys, you must store the files containing the remote servers' private keys on the control server.

After running an Ansible command, you will need to enter the SSH key passphrase, if one is configured.

Here, I entered the wrong passphrase for server A, so Ansible could not connect to it and was only able to connect to server B.

ssh key

If each remote server uses a different private key, you need to specify the corresponding key path in the inventory. Additionally, when running Ansible commands, you will need to enter the correct passphrase for each server. Entering passphrases this way is inconvenient and prone to errors.

So, how can you avoid entering passphrases in Ansible?

Ansible does not provide an option to bypass passphrases. However, you can use an SSH feature to cache the passphrases for private keys on the control server, so Ansible will not prompt for them on subsequent runs.

First, start ssh-agent in the background:

text
eval "$(ssh-agent -s)"

Next, add the SSH private key to ssh-agent and enter its passphrase:

text
ssh-add <path_to_private_key>

hình ảnh về ssh-agent

ansible all -i inventory-lab -m ping

Organizing and managing inventory

Problem statement

Suppose you are managing a system with 50 servers, divided into three main categories: web servers, database servers, and test servers. You need to perform different configuration tasks on each server group and sometimes manage all servers simultaneously. To do this, you need to organize the Inventory using groups, variables and grouping groups.

Depending on your current requirements, you can divide the servers into different groups for management. For example, you can group servers by function or geographical location.

Below is a specific example. Here, the servers are divided into three main groups:

  • Webserver group: contains servers on which web servers are installed, deployed, and operated
  • Databaseserver group: manages database servers
  • Tester group: performs product testing tasks
text
[webserver] #Group webserver
web1 ansible_ssh_host=192.168.33.20 
web2 ansible_ssh_host=192.168.33.21 
web3 ansible_ssh_host=192.168.33.22 

[databaseserver] #Group databaseserver
db1 ansible_ssh_host=192.168.33.23 
db2 ansible_ssh_host=192.168.33.24 
db3 ansible_ssh_host=192.168.33.25 
db4 ansible_ssh_host=192.168.33.26 

[tester] #Group tester
test1 ansible_ssh_host=192.168.33.27 
test2 ansible_ssh_host=192.168.33.28 
test3 ansible_ssh_host=192.168.33.29

[datacenter:children] #Group datacenter
webserver
databaserver

[all:vars] #Thiết lập biến vars dùng chung cho các servers
ansible_ssh_user=root
ansible_ssh_port=2210
ansible_ssh_private_key_file=/root/.ssh/linux-unity.key

vars variables in the inventory

vars is used to define shared variables for servers or server groups in the inventory. This eliminates the need to repeat each variable for every server, making the inventory file more concise, readable, and manageable.

In the example above, these servers use the same SSH credentials, so we will create a var to define them globally.

text
[<name_of_group>:vars]
...

Dividing the inventory into groups

To divide the inventory into groups, use the following syntax:

text
[name_of_group]
<server 1>
...
<server n>

Here, we will run an Ansible command to retrieve information about the operating systems used by the tester group through the setup module.

text
ansible <name_of_group> -i <inventory> -m <module>

Grouping groups – combining groups

If you need to run tasks against two or more groups together, you can group them in the inventory using the syntax below.

text
[<name_of_group>:children]
<name_of_group1>
<name_of_group2>
...

In the example below, I need to retrieve OS information from the webserver and databaseserver groups, so I combine them into the datacenter group and run the Ansible command.

The servers in both the webserver and databaseserver groups now return the expected results.

We have now covered Inventory, a fundamental and essential component of Ansible. For more in-depth information, see: Ansible Inventory Guide.

Series: Ansible · Part 3/8

  1. Intro Automation in Linux Configuration Management
  2. Part 1 [Ansible] Part 1: Introduction to Ansible
  3. Part 2 [Ansible] Part 2: Installing and Getting Started with Ansible
  4. Part 3 [Ansible] Part 3: How to Create and Manage Inventory Effectively in Ansible
  5. Part 4 [Ansible] Part 4: Exploring Commonly Used Modules in Ansible
  6. Part 5 [Ansible] Part 5: A Beginner’s Guide to Writing Simple, Effective Playbooks
  7. Part 6 [Ansible] Part 6: Writing an Ansible Playbook to Install WordPress on a LAMP Stack
  8. Part 7 [Ansible] Part 7: Install Ansible CMDB and Create System Reports in Minutes

Related posts