From 2d6986bb538fb3075685b339b25beab1677fd499 Mon Sep 17 00:00:00 2001 From: Chris Church Date: Wed, 11 Feb 2015 01:45:47 -0500 Subject: [PATCH] Update EC2 inventory to include changes from ansible/devel, add tag_none to tags group, update vpc group naming to match ansible/devel. --- awx/main/tests/inventory.py | 6 ++++-- awx/plugins/inventory/ec2.ini.example | 1 + awx/plugins/inventory/ec2.py | 16 +++++++++++----- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/awx/main/tests/inventory.py b/awx/main/tests/inventory.py index 03a8bd0cb2..0d685de289 100644 --- a/awx/main/tests/inventory.py +++ b/awx/main/tests/inventory.py @@ -1669,7 +1669,8 @@ class InventoryUpdatesTest(BaseTransactionTest): self.assertTrue('security_groups' in child_names) self.assertTrue('tags' in child_names) self.assertTrue('images' in child_names) - self.assertTrue('tag_none' in child_names) + self.assertFalse('tag_none' in child_names) + self.assertTrue('tag_none' in self.group.children.get(name='tags').children.values_list('name', flat=True)) self.assertFalse('instances' in child_names) # Make sure we clean up the cache path when finished (when one is not # provided explicitly via source_vars). @@ -1708,7 +1709,7 @@ class InventoryUpdatesTest(BaseTransactionTest): # Skip vpcs as selected inventory may or may not have any. child_names = self.group.children.filter(active=True).values_list('name', flat=True) self.assertTrue('ec2' in child_names) - self.assertTrue('tag_none' in child_names) + self.assertFalse('tag_none' in child_names) self.assertTrue('regions' in child_names) self.assertTrue(self.group.children.get(name='regions').children.filter(active=True).count()) self.assertTrue('types' in child_names) @@ -1719,6 +1720,7 @@ class InventoryUpdatesTest(BaseTransactionTest): self.assertTrue(self.group.children.get(name='security_groups').children.filter(active=True).count()) self.assertTrue('tags' in child_names) self.assertTrue(self.group.children.get(name='tags').children.filter(active=True).count()) + self.assertTrue('tag_none' in self.group.children.get(name='tags').children.values_list('name', flat=True)) self.assertTrue('images' in child_names) self.assertTrue(self.group.children.get(name='images').children.filter(active=True).count()) self.assertTrue('instances' in child_names) diff --git a/awx/plugins/inventory/ec2.ini.example b/awx/plugins/inventory/ec2.ini.example index adc7d13513..70f6a819d5 100644 --- a/awx/plugins/inventory/ec2.ini.example +++ b/awx/plugins/inventory/ec2.ini.example @@ -79,6 +79,7 @@ group_by_key_pair = True group_by_vpc_id = True group_by_security_group = True 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 = True diff --git a/awx/plugins/inventory/ec2.py b/awx/plugins/inventory/ec2.py index 5ad7bb4cd7..0f7c198575 100755 --- a/awx/plugins/inventory/ec2.py +++ b/awx/plugins/inventory/ec2.py @@ -412,9 +412,13 @@ class Ec2Inventory(object): # Select the best destination address if instance.subnet_id: - dest = getattr(instance, self.vpc_destination_variable) + dest = getattr(instance, self.vpc_destination_variable, None) + if dest is None: + dest = getattr(instance, 'tags').get(self.vpc_destination_variable, None) else: - dest = getattr(instance, self.destination_variable) + dest = getattr(instance, self.destination_variable, None) + if dest is None: + dest = getattr(instance, 'tags').get(self.destination_variable, None) if not dest: # Skip instances we cannot address (e.g. private VPC subnet) @@ -474,7 +478,7 @@ class Ec2Inventory(object): # Inventory: Group by VPC if self.group_by_vpc_id and instance.vpc_id: - vpc_id_name = self.to_safe(instance.vpc_id) + vpc_id_name = self.to_safe('vpc_id_' + instance.vpc_id) self.push(self.inventory, vpc_id_name, dest) if self.nested_groups: self.push_group(self.inventory, 'vpcs', vpc_id_name) @@ -512,6 +516,8 @@ class Ec2Inventory(object): # Global Tag: instances without tags if self.group_by_tag_none and len(instance.tags) == 0: self.push(self.inventory, 'tag_none', dest) + if self.nested_groups: + self.push_group(self.inventory, 'tags', 'tag_none') # Global Tag: tag all EC2 instances self.push(self.inventory, 'ec2', dest) @@ -566,7 +572,7 @@ class Ec2Inventory(object): # Inventory: Group by VPC if self.group_by_vpc_id and instance.subnet_group and instance.subnet_group.vpc_id: - vpc_id_name = self.to_safe(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) if self.nested_groups: self.push_group(self.inventory, 'vpcs', vpc_id_name) @@ -707,7 +713,7 @@ class Ec2Inventory(object): # try updating the cache self.do_api_calls_update_cache() if not self.args.host in self.index: - # host migh not exist anymore + # host might not exist anymore return self.json_format_dict({}, True) (region, instance_id) = self.index[self.args.host]