Ansible Custom Inventory – How I Organize and Manage My Servers

 While working with Ansible, one of the most essential things I learned was how to create and manage a custom inventory.

The inventory is basically the place where I list all my servers — and tell Ansible how to connect to them.

Once I understood this clearly, working with multiple EC2 instances became much easier.

In this blog, I’ll explain the concept of custom inventory the way I understood it and show the formats I used in my hands-on practice.


What Is an Inventory? (My Understanding)

An inventory is a file where I define the servers (managed nodes) that Ansible should control.

It contains:

  • server names

  • IP addresses

  • SSH usernames

  • private key file paths

  • group names

For me, the inventory acts like:

“A phonebook for Ansible — it tells Ansible which server to call and how to connect.”


Types of Inventories I Used

Ansible supports different inventory formats, but these are the two I practiced:


1️⃣ Static Inventory (INI Format)

This is the simplest format and the one I used most often.

Example: inventory.ini

[webservers] web1 ansible_host=54.123.45.10 ansible_user=ec2-user ansible_ssh_private_key_file=mykey.pem web2 ansible_host=54.123.45.11 ansible_user=ec2-user ansible_ssh_private_key_file=mykey.pem [dbservers] db1 ansible_host=54.123.45.20 ansible_user=ubuntu ansible_ssh_private_key_file=dbkey.pem

What each field means:

  • ansible_host → actual IP address

  • ansible_user → default login user

  • ansible_ssh_private_key_file → SSH key path

This format is great for quick labs or small setups.


2️⃣ Static Inventory (YAML Format)

YAML is more structured and cleaner for large inventories.

Example: inventory.yml

all: children: webservers: hosts: web1: ansible_host: 54.123.45.10 ansible_user: ec2-user ansible_ssh_private_key_file: mykey.pem web2: ansible_host: 54.123.45.11 ansible_user: ec2-user ansible_ssh_private_key_file: mykey.pem dbservers: hosts: db1: ansible_host: 54.123.45.20 ansible_user: ubuntu ansible_ssh_private_key_file: dbkey.pem

I personally found YAML easier to read when managing many hosts.


Using the Inventory With Ansible

To run ad-hoc commands:

ansible all -i inventory.ini -m ping

To run playbooks:

ansible-playbook -i inventory.yml site.yml

If inventory is in the same folder as the playbook, using -i is optional.


Group Variables and Host Variables

I also learned that I can define variables at:

Group level:

[webservers:vars] ansible_user=ec2-user ansible_ssh_private_key_file=mykey.pem

Host level:

web1 ansible_host=54.12.34.56 ansible_port=2222

This avoids repeating values and makes the inventory cleaner.


Dynamic Inventory (For Cloud Automation)

Even though I didn’t use dynamic inventory extensively, I understood the idea.

Dynamic inventory automatically discovers servers from cloud providers like:

  • AWS EC2

  • Azure

  • GCP

Example command:

ansible-inventory -i aws_ec2.yaml --list

This is useful when servers keep changing (autoscaling, dynamic IPs, etc.).


Why Custom Inventory Is Important (My Realization)

After working with inventories in multiple playbooks, I realised:

✔ Inventory decides where Ansible runs
✔ Good inventory organization = easy automation
✔ Grouping servers simplifies operations
✔ Inventory variables reduce repetition
✔ YAML format is better for large setups
✔ Dynamic inventory is powerful for cloud environments

Without a proper inventory, automation becomes messy.


Final Thoughts

Learning how to write my own inventory file was a big improvement in my Ansible practice.
Instead of hardcoding IPs everywhere, I could manage everything from one place.

With ad-hoc commands and playbooks, a clean inventory made my setups faster, simpler, and more professional.

This completes the core fundamental topics I learned in Ansible:

  • What is Ansible

  • Architecture

  • Modules

  • Installation & Setup

  • Ad-Hoc Commands

  • Playbooks

  • Custom Inventory

Next topics I plan to cover soon:

  • Ansible Roles

  • Handlers

  • Variables & Facts

  • Loops, Conditions, Tags

  • Complete automation project

Stay tuned for the next blog in my DevOps journey!

Comments

Popular posts from this blog

Ansible Playbooks – How I Learned to Automate Complete Tasks

Understanding Ansible Modules

“What is Ansible? – My Understanding as a DevOps Learner”