From e6a9b2cae5628aa644a0290adb9c10140a96422c Mon Sep 17 00:00:00 2001 From: Abhijit Menon-Sen Date: Tue, 6 Oct 2015 12:18:43 +0530 Subject: [PATCH] Add an expand_csv_tags setting to ec2.ini MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If enabled, this will convert tags of the form "a,b,c" to a list and use the results to create additional inventory groups. This is based on PR #8676 by nickpeck (but not a straight rebase—both the code and the nomenclature have been changed here). Closes #8676 --- contrib/inventory/ec2.ini | 4 ++++ contrib/inventory/ec2.py | 28 +++++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/contrib/inventory/ec2.ini b/contrib/inventory/ec2.ini index f3b75570f65..1231c9d4665 100644 --- a/contrib/inventory/ec2.ini +++ b/contrib/inventory/ec2.ini @@ -97,6 +97,10 @@ nested_groups = False # Replace - tags when creating groups to avoid issues with ansible replace_dash_in_groups = True +# If set to true, any tag of the form "a,b,c" is expanded into a list +# and the results are used to create additional tag_* inventory groups. +expand_csv_tags = False + # The EC2 inventory output can become very large. To manage its size, # configure which groups should be created. group_by_instance_id = True diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py index 0208ed1fde5..4c5cf23fcb8 100755 --- a/contrib/inventory/ec2.py +++ b/contrib/inventory/ec2.py @@ -141,6 +141,7 @@ except ImportError: class Ec2Inventory(object): + def _empty_inventory(self): return {"_meta" : {"hostvars" : {}}} @@ -321,6 +322,11 @@ class Ec2Inventory(object): self.cache_path_index = cache_dir + "/ansible-ec2.index" self.cache_max_age = config.getint('ec2', 'cache_max_age') + if config.has_option('ec2', 'expand_csv_tags'): + self.expand_csv_tags = config.getboolean('ec2', 'expand_csv_tags') + else: + self.expand_csv_tags = False + # Configure nested groups instead of flat namespace. if config.has_option('ec2', 'nested_groups'): self.nested_groups = config.getboolean('ec2', 'nested_groups') @@ -696,15 +702,21 @@ class Ec2Inventory(object): # Inventory: Group by tag keys if self.group_by_tag_keys: for k, v in instance.tags.items(): - if v: - key = self.to_safe("tag_" + k + "=" + v) + if self.expand_csv_tags and v and ',' in v: + values = map(lambda x: x.strip(), v.split(',')) else: - key = self.to_safe("tag_" + k) - self.push(self.inventory, key, dest) - if self.nested_groups: - self.push_group(self.inventory, 'tags', self.to_safe("tag_" + k)) + values = [v] + + for v in values: if v: - self.push_group(self.inventory, self.to_safe("tag_" + k), key) + key = self.to_safe("tag_" + k + "=" + v) + else: + key = self.to_safe("tag_" + k) + self.push(self.inventory, key, dest) + if self.nested_groups: + self.push_group(self.inventory, 'tags', self.to_safe("tag_" + k)) + if v: + self.push_group(self.inventory, self.to_safe("tag_" + k), key) # Inventory: Group by Route53 domain names if enabled if self.route53_enabled and self.group_by_route53_names: @@ -1120,6 +1132,8 @@ class Ec2Inventory(object): instance_vars['ec2_placement'] = value.zone elif key == 'ec2_tags': for k, v in value.items(): + if self.expand_csv_tags and ',' in v: + v = map(lambda x: x.strip(), v.split(',')) key = self.to_safe('ec2_tag_' + k) instance_vars[key] = v elif key == 'ec2_groups':