How do `keyed_groups`, `hostnames` and `compose` shape an `aws_ec2` inventory?
AdvancedConceptInventory
Answer
keyed_groupsbuilds groups from instance attributes, for example one group per tag value.hostnameschooses the inventory name, tried in order (tag:Name,private-ip-address,dns-name).composecreates host variables from Jinja2 expressions over the instance data.
plugin: amazon.aws.aws_ec2
regions: [ap-south-1]
keyed_groups:
- key: tags.Role
prefix: role # group role_web, role_db
- key: placement.availability_zone
prefix: az
hostnames:
- tag:Name
- private-ip-address
compose:
ansible_host: private_ip_address
env: tags.Env | default('unknown')
Now hosts: role_web:&az_ap_south_1a works without anyone maintaining a list.
More on Inventory
What is an inventory and why does Ansible need one?BeginnerWhat are the INI and YAML inventory formats and how do they differ?BeginnerHow do you create groups of groups?BeginnerExplain host patterns: `:`, `:&`, `:!` and wildcards.IntermediateWhich connection variables can you set per host in inventory?BeginnerWhat are `group_vars` and `host_vars` directories?Intermediate