Merge pull request #8516 from mpeters/ec2_dyn_inventory_include_exclude_patterns

adding include_pattern and exclude_pattern options to ec2.py
This commit is contained in:
Michael DeHaan 2014-08-08 11:21:38 -04:00
commit 2d2178f2c5
2 changed files with 30 additions and 0 deletions

View file

@ -67,3 +67,9 @@ cache_max_age = 300
# Organize groups into a nested/hierarchy instead of a flat namespace. # Organize groups into a nested/hierarchy instead of a flat namespace.
nested_groups = False 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-*

View file

@ -252,6 +252,22 @@ class Ec2Inventory(object):
else: else:
self.nested_groups = False 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): def parse_cli_args(self):
''' Command line argument processing ''' ''' Command line argument processing '''
@ -359,6 +375,14 @@ class Ec2Inventory(object):
# Skip instances we cannot address (e.g. private VPC subnet) # Skip instances we cannot address (e.g. private VPC subnet)
return 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 # Add to index
self.index[dest] = [region, instance.id] self.index[dest] = [region, instance.id]