diff --git a/contrib/inventory/ec2.ini b/contrib/inventory/ec2.ini index 8acce912f1..f3171eac10 100644 --- a/contrib/inventory/ec2.ini +++ b/contrib/inventory/ec2.ini @@ -57,9 +57,14 @@ vpc_destination_variable = ip_address #destination_format_tags = Name,environment # To tag instances on EC2 with the resource records that point to them from -# Route53, uncomment and set 'route53' to True. +# Route53, set 'route53' to True. route53 = False +# To use Route53 records as the inventory hostnames, uncomment and set +# to equal the domain name you wish to use. You must also have 'route53' (above) +# set to True. +# route53_hostnames = .example.com + # To exclude RDS instances from the inventory, uncomment and set to False. #rds = False diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py index 0bb2eb410a..9ee967b437 100755 --- a/contrib/inventory/ec2.py +++ b/contrib/inventory/ec2.py @@ -274,6 +274,10 @@ class Ec2Inventory(object): # Route53 self.route53_enabled = config.getboolean('ec2', 'route53') + if config.has_option('ec2', 'route53_hostnames'): + self.route53_hostnames = config.get('ec2', 'route53_hostnames') + else: + self.route53_hostnames = None self.route53_excluded_zones = [] if config.has_option('ec2', 'route53_excluded_zones'): self.route53_excluded_zones.extend( @@ -809,9 +813,19 @@ class Ec2Inventory(object): else: hostname = getattr(instance, self.hostname_variable) + # set the hostname from route53 + if self.route53_enabled and self.route53_hostnames: + route53_names = self.get_instance_route53_names(instance) + for name in route53_names: + if name.endswith(self.route53_hostnames): + hostname = name + # If we can't get a nice hostname, use the destination address if not hostname: hostname = dest + # to_safe strips hostname characters like dots, so don't strip route53 hostnames + elif self.route53_enabled and self.route53_hostnames and hostname.endswith(self.route53_hostnames): + hostname = hostname.lower() else: hostname = self.to_safe(hostname).lower()