Lesson 23 / 26
Environments and Dynamic Inventory
Manage staging and production separately and pull hosts from cloud APIs.
Hosts change; inventory should follow
In the cloud, servers come and go, so hand-edited lists go stale. Inventory plugins (for AWS, Azure, GCP, VMware and others) query the provider and group hosts by tags, regions or instance types. A small YAML config file selects the plugin. Keep one inventory per environment and combine with group variables, so the playbook is identical and only the target and data differ. Always run a --limit or a check on a small subset first when pointing at a new inventory.
An AWS EC2 inventory plugin file
Illustrative; needs the amazon.aws collection and credentials. File names must end with aws_ec2.yml.
plugin: amazon.aws.aws_ec2
regions: [ap-south-1]
filters:
tag:Environment: production
keyed_groups:
- key: tags.Role
prefix: roleQuick check: Why use a dynamic inventory in the cloud?
- Servers change, so the host list should be generated from the provider
- It removes the need for SSH
- Static files are illegal
- It makes playbooks shorter automatically
Answer
Servers change, so the host list should be generated from the provider — Generated inventories stay accurate as instances are created and removed.