mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Added hostname_variable to ec2 inventory
This commit is contained in:
parent
0c6693aa4c
commit
53756af546
2 changed files with 67 additions and 28 deletions
|
@ -29,6 +29,11 @@ regions_exclude = us-gov-west-1,cn-north-1
|
||||||
# in the event of a collision.
|
# in the event of a collision.
|
||||||
destination_variable = public_dns_name
|
destination_variable = public_dns_name
|
||||||
|
|
||||||
|
# This allows you to override the inventory_name with an ec2 variable, instead
|
||||||
|
# of using the destination_variable above. Addressing (aka ansible_ssh_host)
|
||||||
|
# will still use destination_variable. Tags should be written as 'tag_TAGNAME'.
|
||||||
|
#hostname_variable = tag_Name
|
||||||
|
|
||||||
# For server inside a VPC, using DNS names may not make sense. When an instance
|
# For server inside a VPC, using DNS names may not make sense. When an instance
|
||||||
# has 'subnet_id' set, this variable is used. If the subnet is public, setting
|
# has 'subnet_id' set, this variable is used. If the subnet is public, setting
|
||||||
# this to 'ip_address' will return the public IP address. For instances in a
|
# this to 'ip_address' will return the public IP address. For instances in a
|
||||||
|
|
|
@ -236,7 +236,11 @@ class Ec2Inventory(object):
|
||||||
# Destination addresses
|
# Destination addresses
|
||||||
self.destination_variable = config.get('ec2', 'destination_variable')
|
self.destination_variable = config.get('ec2', 'destination_variable')
|
||||||
self.vpc_destination_variable = config.get('ec2', 'vpc_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
|
||||||
|
|
||||||
# Route53
|
# Route53
|
||||||
self.route53_enabled = config.getboolean('ec2', 'route53')
|
self.route53_enabled = config.getboolean('ec2', 'route53')
|
||||||
self.route53_excluded_zones = []
|
self.route53_excluded_zones = []
|
||||||
|
@ -628,32 +632,46 @@ class Ec2Inventory(object):
|
||||||
# Skip instances we cannot address (e.g. private VPC subnet)
|
# Skip instances we cannot address (e.g. private VPC subnet)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Set the inventory name
|
||||||
|
hostname = None
|
||||||
|
if self.hostname_variable:
|
||||||
|
if self.hostname_variable.startswith('tag_'):
|
||||||
|
hostname = instance.tags.get(self.hostname_variable[4:], None)
|
||||||
|
else:
|
||||||
|
hostname = getattr(instance, self.hostname_variable)
|
||||||
|
|
||||||
|
# If we can't get a nice hostname, use the destination address
|
||||||
|
if not hostname:
|
||||||
|
hostname = dest
|
||||||
|
|
||||||
|
hostname = self.to_safe(hostname).lower()
|
||||||
|
|
||||||
# if we only want to include hosts that match a pattern, skip those that don't
|
# if we only want to include hosts that match a pattern, skip those that don't
|
||||||
if self.pattern_include and not self.pattern_include.match(dest):
|
if self.pattern_include and not self.pattern_include.match(hostname):
|
||||||
return
|
return
|
||||||
|
|
||||||
# if we need to exclude hosts that match a pattern, skip those
|
# if we need to exclude hosts that match a pattern, skip those
|
||||||
if self.pattern_exclude and self.pattern_exclude.match(dest):
|
if self.pattern_exclude and self.pattern_exclude.match(hostname):
|
||||||
return
|
return
|
||||||
|
|
||||||
# Add to index
|
# Add to index
|
||||||
self.index[dest] = [region, instance.id]
|
self.index[hostname] = [region, instance.id]
|
||||||
|
|
||||||
# Inventory: Group by instance ID (always a group of 1)
|
# Inventory: Group by instance ID (always a group of 1)
|
||||||
if self.group_by_instance_id:
|
if self.group_by_instance_id:
|
||||||
self.inventory[instance.id] = [dest]
|
self.inventory[instance.id] = [hostname]
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'instances', instance.id)
|
self.push_group(self.inventory, 'instances', instance.id)
|
||||||
|
|
||||||
# Inventory: Group by region
|
# Inventory: Group by region
|
||||||
if self.group_by_region:
|
if self.group_by_region:
|
||||||
self.push(self.inventory, region, dest)
|
self.push(self.inventory, region, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'regions', region)
|
self.push_group(self.inventory, 'regions', region)
|
||||||
|
|
||||||
# Inventory: Group by availability zone
|
# Inventory: Group by availability zone
|
||||||
if self.group_by_availability_zone:
|
if self.group_by_availability_zone:
|
||||||
self.push(self.inventory, instance.placement, dest)
|
self.push(self.inventory, instance.placement, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
if self.group_by_region:
|
if self.group_by_region:
|
||||||
self.push_group(self.inventory, region, instance.placement)
|
self.push_group(self.inventory, region, instance.placement)
|
||||||
|
@ -662,28 +680,28 @@ class Ec2Inventory(object):
|
||||||
# Inventory: Group by Amazon Machine Image (AMI) ID
|
# Inventory: Group by Amazon Machine Image (AMI) ID
|
||||||
if self.group_by_ami_id:
|
if self.group_by_ami_id:
|
||||||
ami_id = self.to_safe(instance.image_id)
|
ami_id = self.to_safe(instance.image_id)
|
||||||
self.push(self.inventory, ami_id, dest)
|
self.push(self.inventory, ami_id, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'images', ami_id)
|
self.push_group(self.inventory, 'images', ami_id)
|
||||||
|
|
||||||
# Inventory: Group by instance type
|
# Inventory: Group by instance type
|
||||||
if self.group_by_instance_type:
|
if self.group_by_instance_type:
|
||||||
type_name = self.to_safe('type_' + instance.instance_type)
|
type_name = self.to_safe('type_' + instance.instance_type)
|
||||||
self.push(self.inventory, type_name, dest)
|
self.push(self.inventory, type_name, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'types', type_name)
|
self.push_group(self.inventory, 'types', type_name)
|
||||||
|
|
||||||
# Inventory: Group by key pair
|
# Inventory: Group by key pair
|
||||||
if self.group_by_key_pair and instance.key_name:
|
if self.group_by_key_pair and instance.key_name:
|
||||||
key_name = self.to_safe('key_' + instance.key_name)
|
key_name = self.to_safe('key_' + instance.key_name)
|
||||||
self.push(self.inventory, key_name, dest)
|
self.push(self.inventory, key_name, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'keys', key_name)
|
self.push_group(self.inventory, 'keys', key_name)
|
||||||
|
|
||||||
# Inventory: Group by VPC
|
# Inventory: Group by VPC
|
||||||
if self.group_by_vpc_id and instance.vpc_id:
|
if self.group_by_vpc_id and instance.vpc_id:
|
||||||
vpc_id_name = self.to_safe('vpc_id_' + instance.vpc_id)
|
vpc_id_name = self.to_safe('vpc_id_' + instance.vpc_id)
|
||||||
self.push(self.inventory, vpc_id_name, dest)
|
self.push(self.inventory, vpc_id_name, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'vpcs', vpc_id_name)
|
self.push_group(self.inventory, 'vpcs', vpc_id_name)
|
||||||
|
|
||||||
|
@ -692,7 +710,7 @@ class Ec2Inventory(object):
|
||||||
try:
|
try:
|
||||||
for group in instance.groups:
|
for group in instance.groups:
|
||||||
key = self.to_safe("security_group_" + group.name)
|
key = self.to_safe("security_group_" + group.name)
|
||||||
self.push(self.inventory, key, dest)
|
self.push(self.inventory, key, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'security_groups', key)
|
self.push_group(self.inventory, 'security_groups', key)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
@ -712,7 +730,7 @@ class Ec2Inventory(object):
|
||||||
key = self.to_safe("tag_" + k + "=" + v)
|
key = self.to_safe("tag_" + k + "=" + v)
|
||||||
else:
|
else:
|
||||||
key = self.to_safe("tag_" + k)
|
key = self.to_safe("tag_" + k)
|
||||||
self.push(self.inventory, key, dest)
|
self.push(self.inventory, key, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'tags', self.to_safe("tag_" + k))
|
self.push_group(self.inventory, 'tags', self.to_safe("tag_" + k))
|
||||||
if v:
|
if v:
|
||||||
|
@ -722,20 +740,21 @@ class Ec2Inventory(object):
|
||||||
if self.route53_enabled and self.group_by_route53_names:
|
if self.route53_enabled and self.group_by_route53_names:
|
||||||
route53_names = self.get_instance_route53_names(instance)
|
route53_names = self.get_instance_route53_names(instance)
|
||||||
for name in route53_names:
|
for name in route53_names:
|
||||||
self.push(self.inventory, name, dest)
|
self.push(self.inventory, name, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'route53', name)
|
self.push_group(self.inventory, 'route53', name)
|
||||||
|
|
||||||
# Global Tag: instances without tags
|
# Global Tag: instances without tags
|
||||||
if self.group_by_tag_none and len(instance.tags) == 0:
|
if self.group_by_tag_none and len(instance.tags) == 0:
|
||||||
self.push(self.inventory, 'tag_none', dest)
|
self.push(self.inventory, 'tag_none', hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'tags', 'tag_none')
|
self.push_group(self.inventory, 'tags', 'tag_none')
|
||||||
|
|
||||||
# Global Tag: tag all EC2 instances
|
# Global Tag: tag all EC2 instances
|
||||||
self.push(self.inventory, 'ec2', dest)
|
self.push(self.inventory, 'ec2', hostname)
|
||||||
|
|
||||||
self.inventory["_meta"]["hostvars"][dest] = self.get_host_info_dict_from_instance(instance)
|
self.inventory["_meta"]["hostvars"][hostname] = self.get_host_info_dict_from_instance(instance)
|
||||||
|
self.inventory["_meta"]["hostvars"][hostname]['ansible_ssh_host'] = dest
|
||||||
|
|
||||||
|
|
||||||
def add_rds_instance(self, instance, region):
|
def add_rds_instance(self, instance, region):
|
||||||
|
@ -753,24 +772,38 @@ class Ec2Inventory(object):
|
||||||
# Skip instances we cannot address (e.g. private VPC subnet)
|
# Skip instances we cannot address (e.g. private VPC subnet)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Set the inventory name
|
||||||
|
hostname = None
|
||||||
|
if self.hostname_variable:
|
||||||
|
if self.hostname_variable.startswith('tag_'):
|
||||||
|
hostname = instance.tags.get(self.hostname_variable[4:], None)
|
||||||
|
else:
|
||||||
|
hostname = getattr(instance, self.hostname_variable)
|
||||||
|
|
||||||
|
# If we can't get a nice hostname, use the destination address
|
||||||
|
if not hostname:
|
||||||
|
hostname = dest
|
||||||
|
|
||||||
|
hostname = self.to_safe(hostname).lower()
|
||||||
|
|
||||||
# Add to index
|
# Add to index
|
||||||
self.index[dest] = [region, instance.id]
|
self.index[hostname] = [region, instance.id]
|
||||||
|
|
||||||
# Inventory: Group by instance ID (always a group of 1)
|
# Inventory: Group by instance ID (always a group of 1)
|
||||||
if self.group_by_instance_id:
|
if self.group_by_instance_id:
|
||||||
self.inventory[instance.id] = [dest]
|
self.inventory[instance.id] = [hostname]
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'instances', instance.id)
|
self.push_group(self.inventory, 'instances', instance.id)
|
||||||
|
|
||||||
# Inventory: Group by region
|
# Inventory: Group by region
|
||||||
if self.group_by_region:
|
if self.group_by_region:
|
||||||
self.push(self.inventory, region, dest)
|
self.push(self.inventory, region, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'regions', region)
|
self.push_group(self.inventory, 'regions', region)
|
||||||
|
|
||||||
# Inventory: Group by availability zone
|
# Inventory: Group by availability zone
|
||||||
if self.group_by_availability_zone:
|
if self.group_by_availability_zone:
|
||||||
self.push(self.inventory, instance.availability_zone, dest)
|
self.push(self.inventory, instance.availability_zone, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
if self.group_by_region:
|
if self.group_by_region:
|
||||||
self.push_group(self.inventory, region, instance.availability_zone)
|
self.push_group(self.inventory, region, instance.availability_zone)
|
||||||
|
@ -779,14 +812,14 @@ class Ec2Inventory(object):
|
||||||
# Inventory: Group by instance type
|
# Inventory: Group by instance type
|
||||||
if self.group_by_instance_type:
|
if self.group_by_instance_type:
|
||||||
type_name = self.to_safe('type_' + instance.instance_class)
|
type_name = self.to_safe('type_' + instance.instance_class)
|
||||||
self.push(self.inventory, type_name, dest)
|
self.push(self.inventory, type_name, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'types', type_name)
|
self.push_group(self.inventory, 'types', type_name)
|
||||||
|
|
||||||
# Inventory: Group by VPC
|
# Inventory: Group by VPC
|
||||||
if self.group_by_vpc_id and instance.subnet_group and instance.subnet_group.vpc_id:
|
if self.group_by_vpc_id and instance.subnet_group and instance.subnet_group.vpc_id:
|
||||||
vpc_id_name = self.to_safe('vpc_id_' + instance.subnet_group.vpc_id)
|
vpc_id_name = self.to_safe('vpc_id_' + instance.subnet_group.vpc_id)
|
||||||
self.push(self.inventory, vpc_id_name, dest)
|
self.push(self.inventory, vpc_id_name, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'vpcs', vpc_id_name)
|
self.push_group(self.inventory, 'vpcs', vpc_id_name)
|
||||||
|
|
||||||
|
@ -795,7 +828,7 @@ class Ec2Inventory(object):
|
||||||
try:
|
try:
|
||||||
if instance.security_group:
|
if instance.security_group:
|
||||||
key = self.to_safe("security_group_" + instance.security_group.name)
|
key = self.to_safe("security_group_" + instance.security_group.name)
|
||||||
self.push(self.inventory, key, dest)
|
self.push(self.inventory, key, hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'security_groups', key)
|
self.push_group(self.inventory, 'security_groups', key)
|
||||||
|
|
||||||
|
@ -806,20 +839,21 @@ class Ec2Inventory(object):
|
||||||
|
|
||||||
# Inventory: Group by engine
|
# Inventory: Group by engine
|
||||||
if self.group_by_rds_engine:
|
if self.group_by_rds_engine:
|
||||||
self.push(self.inventory, self.to_safe("rds_" + instance.engine), dest)
|
self.push(self.inventory, self.to_safe("rds_" + instance.engine), hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'rds_engines', self.to_safe("rds_" + instance.engine))
|
self.push_group(self.inventory, 'rds_engines', self.to_safe("rds_" + instance.engine))
|
||||||
|
|
||||||
# Inventory: Group by parameter group
|
# Inventory: Group by parameter group
|
||||||
if self.group_by_rds_parameter_group:
|
if self.group_by_rds_parameter_group:
|
||||||
self.push(self.inventory, self.to_safe("rds_parameter_group_" + instance.parameter_group.name), dest)
|
self.push(self.inventory, self.to_safe("rds_parameter_group_" + instance.parameter_group.name), hostname)
|
||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'rds_parameter_groups', self.to_safe("rds_parameter_group_" + instance.parameter_group.name))
|
self.push_group(self.inventory, 'rds_parameter_groups', self.to_safe("rds_parameter_group_" + instance.parameter_group.name))
|
||||||
|
|
||||||
# Global Tag: all RDS instances
|
# Global Tag: all RDS instances
|
||||||
self.push(self.inventory, 'rds', dest)
|
self.push(self.inventory, 'rds', hostname)
|
||||||
|
|
||||||
self.inventory["_meta"]["hostvars"][dest] = self.get_host_info_dict_from_instance(instance)
|
self.inventory["_meta"]["hostvars"][hostname] = self.get_host_info_dict_from_instance(instance)
|
||||||
|
self.inventory["_meta"]["hostvars"][hostname]['ansible_ssh_host'] = dest
|
||||||
|
|
||||||
def add_elasticache_cluster(self, cluster, region):
|
def add_elasticache_cluster(self, cluster, region):
|
||||||
''' Adds an ElastiCache cluster to the inventory and index, as long as
|
''' Adds an ElastiCache cluster to the inventory and index, as long as
|
||||||
|
|
Loading…
Reference in a new issue