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

πŸ“šοΈ 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:
  1. Ansible Management Node: Represents the machine running Ansible, which could be a laptop, desktop, or server.

  2. 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.

  3. 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.

  4. Inventory File (Generated): The dynamic inventory script outputs the host list in JSON format for Ansible to consume.

  5. Playbook: An Ansible playbook defining the tasks to execute on dynamically discovered target servers.

  6. SSH Trust: Automatically set up SSH key-based authentication from the management node to each dynamically discovered target server.

  7. Target Servers: Hosts dynamically retrieved from the cloud or CMDB, identified by their roles or tags.

  8. 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 or boto3 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:

Looking to promote your company, product, service, or event to 30,000+ TechOps Professionals? Let's work together.