Where hosts can appear and disappear (e.g. with AWS ASGs) the EC2 external inventory script (ec2.py
) comes in useful.
All you need to do is to export your AWS keys as environment variables and ec2.py
is good.
ec2.ini options
The EC2 inventory output can become very large. To manage its size, you can configure which groups should be created using ec2.ini
options. E.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
group_by_instance_id = False group_by_region = False group_by_availability_zone = False group_by_ami_id = False group_by_instance_type = False group_by_key_pair = False group_by_vpc_id = False group_by_security_group = False group_by_tag_keys = True group_by_tag_none = True group_by_route53_names = True group_by_rds_engine = True group_by_rds_parameter_group = False |
Note on how these are created:
- format is
tag_KEY_VALUE
- special characters are changed to an underscore
E.g. with a NAME
of my instance name
we would get a tag of tag_NAME_my_instance_name
.
These ini
keys are read in ec2.py
. E.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# Configure which groups should be created. group_by_options = [ 'group_by_instance_id', 'group_by_region', 'group_by_availability_zone', 'group_by_ami_id', 'group_by_instance_type', 'group_by_instance_state', 'group_by_key_pair', 'group_by_vpc_id', 'group_by_security_group', 'group_by_tag_keys', 'group_by_tag_none', 'group_by_route53_names', 'group_by_rds_engine', 'group_by_rds_parameter_group', 'group_by_elasticache_engine', 'group_by_elasticache_cluster', 'group_by_elasticache_parameter_group', 'group_by_elasticache_replication_group', 'group_by_aws_account', ] for option in group_by_options: if config.has_option('ec2', option): setattr(self, option, config.getboolean('ec2', option)) else: setattr(self, option, True) |
See also:
https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.py#L482
and