From 3ab388f6ddb21fe671021db171bfc86f1902de3f Mon Sep 17 00:00:00 2001 From: Matthew Wedgwood Date: Thu, 12 Mar 2015 15:04:36 -0500 Subject: [PATCH] Add account ID grouping and attribute to ease multi-account management --- contrib/inventory/ec2.ini | 1 + contrib/inventory/ec2.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/contrib/inventory/ec2.ini b/contrib/inventory/ec2.ini index a7d043f1dcf..8a9cf82a523 100644 --- a/contrib/inventory/ec2.ini +++ b/contrib/inventory/ec2.ini @@ -124,6 +124,7 @@ expand_csv_tags = False group_by_instance_id = True group_by_region = True group_by_availability_zone = True +group_by_aws_account = False group_by_ami_id = True group_by_instance_type = True group_by_key_pair = True diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py index 84bc22f2e11..46cdb5a6901 100755 --- a/contrib/inventory/ec2.py +++ b/contrib/inventory/ec2.py @@ -162,6 +162,8 @@ class Ec2Inventory(object): # and availability zones self.inventory = self._empty_inventory() + self.aws_account_id = None + # Index of hostname (address) to instance ID self.index = {} @@ -420,6 +422,7 @@ class Ec2Inventory(object): 'group_by_elasticache_cluster', 'group_by_elasticache_parameter_group', 'group_by_elasticache_replication_group', + 'group_by_aws_account', ] for option in group_by_options: if config.has_option('ec2', option): @@ -555,6 +558,9 @@ class Ec2Inventory(object): for tag in tags: tags_by_instance_id[tag.res_id][tag.name] = tag.value + if (not self.aws_account_id) and reservations: + self.aws_account_id = reservations[0].owner_id + for reservation in reservations: for instance in reservation.instances: instance.tags = tags_by_instance_id[instance.id] @@ -863,6 +869,12 @@ class Ec2Inventory(object): self.fail_with_error('\n'.join(['Package boto seems a bit older.', 'Please upgrade boto >= 2.3.0.'])) + # Inventory: Group by AWS account ID + if self.group_by_aws_account: + self.push(self.inventory, self.aws_account_id, dest) + if self.nested_groups: + self.push_group(self.inventory, 'accounts', self.aws_account_id) + # Inventory: Group by tag keys if self.group_by_tag_keys: for k, v in instance.tags.items(): @@ -1335,6 +1347,8 @@ class Ec2Inventory(object): #print type(value) #print value + instance_vars[self.to_safe('ec2_account_id')] = self.aws_account_id + return instance_vars def get_host_info_dict_from_describe_dict(self, describe_dict):