AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible Dynamic Inventory: Complete Guide to AWS, Azure, GCP, and Custom Plugins

By Luca Berton · Published 2024-01-01 · Category: installation

Master Ansible dynamic inventory. Configure AWS EC2, Azure, GCP, Proxmox, VMware, and custom inventory plugins. Build dynamic inventory scripts, use host variables, groups, and filters for cloud-native automation.

What Is Dynamic Inventory?

Static inventory files work when your infrastructure is fixed — a known list of servers that rarely changes. But in cloud environments where instances scale up and down, containers spawn and die, and infrastructure is ephemeral, you need inventory that discovers hosts automatically.

Dynamic inventory queries external sources (AWS, Azure, VMware, a CMDB, a database) and builds the host list at runtime. No manual updates needed.

Inventory Plugins vs Scripts

Ansible supports two approaches:

| Feature | Inventory Plugin (recommended) | Inventory Script (legacy) | |---------|-------------------------------|--------------------------| | Format | YAML configuration file | Executable script (Python, Bash) | | Caching | Built-in | Manual | | Composable | Yes (combine with static) | Limited | | Maintained | Actively | Deprecated for most use cases | | Performance | Optimized | Varies |

Always use inventory plugins unless you have a unique data source with no existing plugin.

AWS EC2 Dynamic Inventory

Setup

Inventory Configuration

Test It

Azure Dynamic Inventory

GCP Dynamic Inventory

VMware Dynamic Inventory

Combining Multiple Inventory Sources

Create an inventory directory with multiple sources:

Custom Dynamic Inventory Script

When no plugin exists for your data source, write a custom script:

Inventory Plugin Development

For a reusable, cacheable plugin:

Best Practices

Use Caching

Tag Everything

Dynamic inventory grouping relies on tags/labels. Establish a tagging standard:

Use Compose for Connection Variables

FAQ

How does Ansible dynamic inventory work?

Ansible queries an external data source (cloud API, CMDB, database) at runtime to build the list of hosts and their variables. Instead of maintaining a static file, the inventory is generated dynamically every time you run a playbook.

Can I mix static and dynamic inventory?

Yes. Put both static YAML files and dynamic inventory plugin configs in the same directory. Ansible merges them automatically. Static inventory is great for network devices, bastion hosts, and other fixed infrastructure alongside dynamic cloud hosts.

How do I debug dynamic inventory issues?

Use ansible-inventory -i your_inventory.yml --list to see the full generated inventory as JSON. Add --graph for a tree view. Check ansible-inventory -i your_inventory.yml --host hostname for specific host variables.

Which inventory plugin should I use for AWS?

Use amazon.aws.aws_ec2 (the collection-based plugin). The older aws_ec2 plugin from community.aws is deprecated. Always use the FQCN: plugin: amazon.aws.aws_ec2.

How do I filter hosts in dynamic inventory?

Use the filters parameter in your plugin config. For AWS, filters match EC2 API filters. For all plugins, keyed_groups with Jinja2 expressions let you create groups from any host attribute, and groups with conditions include/exclude hosts.

Conclusion

Dynamic inventory is essential for cloud-native Ansible automation. Use inventory plugins (not scripts) for AWS, Azure, GCP, VMware, and Proxmox. Tag your infrastructure consistently, use compose for connection variables, enable caching for performance, and combine multiple inventory sources in a single directory. When no plugin exists for your data source, write a custom script or develop a reusable inventory plugin.

Related ArticlesInstall Ansible Complete GuideAnsible Documentation Complete GuideAnsible for Proxmox: Complete GuideAnsible VMware Dynamic InventoryAAP 2.6 Cloud Automation: AWS, Azure, GCPAnsible VMware Dynamic InventoryCreate Network Infrastructure on AWSCreating an Azure Virtual NetworkCreating an Azure VM Scale Set

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home