- TechOps Examples
- Posts
- How to Set Up Dynamic Inventory in Ansible
How to Set Up Dynamic Inventory in Ansible
TechOps Examples
Hey β It's Govardhana MK π
Along with a use case deep dive, we identify the remote job opportunities, top news, tools, and articles in the TechOps industry.
Before we begin... a big thank you to today's sponsor.
Simple, managed Kubernetes built for scale.
Try DIGITAL OCEAN β Free control plane, Free bandwidth allowance.
Loved by Akamai and 58,764+ customers.
IN TODAY'S EDITION
π§ Use Case
How to Set Up Dynamic Inventory in Ansible
π Top News
π Remote Jobs
GGWP is hiring a DevOps Engineer
Remote Location: India
Mentalyc is hiring a Senior DevOps Engineer (AWS + Kubernetes)
Remote Location: Worldwide
ποΈ Resources
π’ Reddit Threads
π οΈ TOOL OF THE DAY
notops - a CLI tool to build and maintain production-ready, cloud-native platforms on AWS with Kubernetes and GitOps.
Zero to Production on Day 1.
Automated Day 2 operations.
Cloud-native design cuts down your Kubernetes, AWS learning curve.
π§ USE CASE
How to Set Up Dynamic Inventory in Ansible
Static inventories are easy to set up and work fine for small, stable environments.
Inventory file lives at /etc/ansible/hosts
by default.
Unless you edit it manually, it will never update itself.
Example static inventory file:
[web]
10.20.30.40
10.20.30.41
But letβs face itβhow often does your infrastructure stay static in todayβs elastic cloud-native world?
With dynamic scaling, new instances popping up and going down all the time, static inventories quickly become a bottleneck.
Dynamic inventory, on the other hand, handles these changes effortlessly by fetching the latest infrastructure details on the fly.
This is how Dynamic Inventory works:
Ansible Management Node: Represents the machine running Ansible, which could be a laptop, desktop, or server.
Dynamic Inventory Script: A script (e.g., dynamic_inventory.py) or plugin that dynamically retrieves host details from an external source, such as a cloud provider or a CMDB.
Cloud Provider API/CMDB: The source of truth for host information, queried by the dynamic inventory script to fetch the current list of servers and their metadata.
Inventory File (Generated): The dynamic inventory script outputs the host list in JSON format for Ansible to consume.
Playbook: An Ansible playbook defining the tasks to execute on dynamically discovered target servers.
SSH Trust: Automatically set up SSH key-based authentication from the management node to each dynamically discovered target server.
Target Servers: Hosts dynamically retrieved from the cloud or CMDB, identified by their roles or tags.
Dynamic Filtering: Specific filters (e.g., by tag, region, or environment) applied within the inventory script to target appropriate servers.
Find more details about working with dynamic inventory here.
How to Set Up Dynamic Inventory:
1. Prerequisites
Ansible installed on the management node.
AWS or another cloud account with API credentials setup.
Python installed with
boto
orboto3
libraries for interaction.
2. Download Dynamic Inventory Script
Clone the dynamic inventory script repository:
git clone https://github.com/ansible/ansible.git
cd ansible/contrib/inventory
cp ec2.py /etc/ansible/
cp ec2.ini /etc/ansible/
3. Configure ec2.ini
Update the ec2.ini
file with filters, such as region and tags
regions = us-east-1
instance_filters = tag:Environment=Production
4. Make ec2.py
Executable
chmod +x /etc/ansible/ec2.py
5. Test the Dynamic Inventory
Run the script to ensure it fetches the correct resources
/etc/ansible/ec2.py --list
6. Use Dynamic Inventory in Playbooks
Specify the inventory script when running Ansible commands
ansible all -i /etc/ansible/ec2.py -m ping
Having set this up does not automatically eliminate the risk of:
π’ Wrong filters or tags
π’ Network connectivity issues or API rate limits
π’ Dynamic inventory configuration script typos
Be proactive:
π Validate regularly with ansible-inventory --list
to check accuracy.
π Use static inventory for stable, dynamic for elastic setups.
π Tag resources consistently to avoid filtering issues.
π Track inventory changes with Git for rollback.
π Log script outputs to identify errors quickly
You may even like: