From 7461eabc6a67afb467b52d9ba8045065d0973617 Mon Sep 17 00:00:00 2001 From: Chris Church Date: Sat, 9 Aug 2014 20:10:27 -0400 Subject: [PATCH] Updated ec2 inventory from core: ansible/ansible#8516 --- awx/plugins/inventory/ec2.ini.example | 6 ++++++ awx/plugins/inventory/ec2.py | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/awx/plugins/inventory/ec2.ini.example b/awx/plugins/inventory/ec2.ini.example index bfac981be9..a0c8672394 100644 --- a/awx/plugins/inventory/ec2.ini.example +++ b/awx/plugins/inventory/ec2.ini.example @@ -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/awx/plugins/inventory/ec2.py b/awx/plugins/inventory/ec2.py index 10f6d3f806..c8e6d6e7f3 100755 --- a/awx/plugins/inventory/ec2.py +++ b/awx/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]