From 7c2cc15c593d5495d217361d582e782919b952d4 Mon Sep 17 00:00:00 2001 From: Michael Peters Date: Fri, 8 Aug 2014 09:26:14 -0400 Subject: [PATCH] adding include_pattern and exclude_pattern options to ec2.py --- plugins/inventory/ec2.ini | 6 ++++++ plugins/inventory/ec2.py | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/plugins/inventory/ec2.ini b/plugins/inventory/ec2.ini index bfac981be9f..a0c8672394c 100644 --- a/plugins/inventory/ec2.ini +++ b/plugins/inventory/ec2.ini @@ -67,3 +67,9 @@ cache_max_age = 300 # Organize groups into a nested/hierarchy instead of a flat namespace. nested_groups = False + +# If you only want to include hosts that match a certain regular expression +# pattern_include = stage-* + +# If you want to exclude any hosts that match a certain regular expression +# pattern_exclude = stage-* diff --git a/plugins/inventory/ec2.py b/plugins/inventory/ec2.py index 10f6d3f806c..c8e6d6e7f31 100755 --- a/plugins/inventory/ec2.py +++ b/plugins/inventory/ec2.py @@ -252,6 +252,22 @@ class Ec2Inventory(object): else: self.nested_groups = False + # Do we need to just include hosts that match a pattern? + try: + pattern_include = config.get('ec2', 'pattern_include') + if pattern_include and len(pattern_include) > 0: + self.pattern_include = re.compile(pattern_include) + except ConfigParser.NoOptionError, e: + self.pattern_include = None + + # Do we need to exclude hosts that match a pattern? + try: + pattern_exclude = config.get('ec2', 'pattern_exclude'); + if pattern_exclude and len(pattern_exclude) > 0: + self.pattern_exclude = re.compile(pattern_exclude) + except ConfigParser.NoOptionError, e: + self.pattern_exclude = '' + def parse_cli_args(self): ''' Command line argument processing ''' @@ -359,6 +375,14 @@ class Ec2Inventory(object): # Skip instances we cannot address (e.g. private VPC subnet) return + # 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): + return + + # if we need to exclude hosts that match a pattern, skip those + if self.pattern_exclude and self.pattern_exclude.match(dest): + return + # Add to index self.index[dest] = [region, instance.id]