Skip to content

Commit

Permalink
Feature: adding route53_hostnames option to set the hostnames from ro…
Browse files Browse the repository at this point in the history
…ute 53 (ansible#20909)

* adding route53_hostnames option to set the hostnames from route 53

* checking whether the route53_hostnames option is present as suggested by @s-hertel

* setting route53_hostnames to None when config option not present

* skip the to_safe only when using route53_hostnames option, as suggested by @ryansb

* skipping the to_safe strip only for the hostnames that came from route53 as suggested by @ryansb
  • Loading branch information
eonwhite authored and ryansb committed Feb 14, 2017
1 parent 32b92b5 commit 80bc704
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion contrib/inventory/ec2.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions contrib/inventory/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ def read_settings(self):

# 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(
Expand Down Expand Up @@ -809,9 +813,19 @@ def add_instance(self, instance, region):
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()

Expand Down

0 comments on commit 80bc704

Please sign in to comment.