summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Cammarata <jimi@sngx.net>2016-03-19 09:13:22 -0400
committerJames Cammarata <jimi@sngx.net>2016-03-19 09:13:22 -0400
commitd9f398ec91df9d112871518a5ea59da47226959a (patch)
tree3591244bba4d14425fe8775a0599a1bf9121be77
parente1ee2ed5a5720615ae665bc151c51aa3c41e2e4b (diff)
parentc9da48d5ba054a1cfdca2f5c6b576e98aca73213 (diff)
downloadansible-d9f398ec91df9d112871518a5ea59da47226959a.tar.gz
Merge branch 'a13m-destination_format' into devel
-rw-r--r--contrib/inventory/ec2.ini10
-rwxr-xr-xcontrib/inventory/ec2.py13
2 files changed, 22 insertions, 1 deletions
diff --git a/contrib/inventory/ec2.ini b/contrib/inventory/ec2.ini
index 7f4535c8a7..0ee7f2fb47 100644
--- a/contrib/inventory/ec2.ini
+++ b/contrib/inventory/ec2.ini
@@ -45,6 +45,16 @@ destination_variable = public_dns_name
# vpc_destination_variable = private_ip_address
vpc_destination_variable = ip_address
+# The following two settings allow flexible ansible host naming based on a
+# python format string and a comma-separated list of ec2 tags. Note that:
+#
+# 1) If the tags referenced are not present for some instances, empty strings
+# will be substituted in the format string.
+# 2) This overrides both destination_variable and vpc_destination_variable.
+#
+#destination_format = {0}.{1}.example.com
+#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 = False
diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py
index fd53b71ecd..02fdfdf890 100755
--- a/contrib/inventory/ec2.py
+++ b/contrib/inventory/ec2.py
@@ -236,11 +236,20 @@ class Ec2Inventory(object):
# Destination addresses
self.destination_variable = config.get('ec2', 'destination_variable')
self.vpc_destination_variable = config.get('ec2', 'vpc_destination_variable')
+
if config.has_option('ec2', 'hostname_variable'):
self.hostname_variable = config.get('ec2', 'hostname_variable')
else:
self.hostname_variable = None
+ if config.has_option('ec2', 'destination_format') and \
+ config.has_option('ec2', 'destination_format_tags'):
+ self.destination_format = config.get('ec2', 'destination_format')
+ self.destination_format_tags = config.get('ec2', 'destination_format_tags').split(',')
+ else:
+ self.destination_format = None
+ self.destination_format_tags = None
+
# Route53
self.route53_enabled = config.getboolean('ec2', 'route53')
self.route53_excluded_zones = []
@@ -627,7 +636,9 @@ class Ec2Inventory(object):
return
# Select the best destination address
- if instance.subnet_id:
+ if self.destination_format and self.destination_format_tags:
+ dest = self.destination_format.format(*[ getattr(instance, 'tags').get(tag, '') for tag in self.destination_format_tags ])
+ elif instance.subnet_id:
dest = getattr(instance, self.vpc_destination_variable, None)
if dest is None:
dest = getattr(instance, 'tags').get(self.vpc_destination_variable, None)