AWS - Use Keyed Groups
Limitation of groups
A limitation of the groups is that new hosts that don’t match the conditions are not accounted for. If a new EC2 instance is deployed with a Linux operating system and the
Name tag doesn’t match it won’t be added to the correct group. Using name matching is a fairly fragile and complex solution, especially if there isn’t a naming standard.
Keyed groups offer another way to define and populate groups within a dynamic inventory. Instead of specifying the group name, keyed groups base the group name on the variable’s value, and the existence of the variable determines group membership. Groups using Jinja2 conditions make the group memberships dynamic, but keyed groups make the groups and the group memberships dynamic.
Another solution is to leverage keyed groups to both create the groups and to assign group memberships dynamically.
When you deployed the ec2 instances, a tag of
os was applied to them. The value was either
windows depending on the instance image. Using tags with keyed groups in this manner allows complete control over the groups and their assignments.
Let’s update the
hosts_aws_ec2.yml with a keyed group based on the
tag.os host variable.