make - replacement in group names optional

This commit is contained in:
Brian Coca 2015-10-21 13:18:32 -04:00
parent 04aa7701d1
commit 07d584c0a7
2 changed files with 14 additions and 4 deletions

View file

@ -94,6 +94,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
# Replace - tags when creating groups to avoid issues with ansible
replace_dash_in_groups = True
# The EC2 inventory output can become very large. To manage its size, # The EC2 inventory output can become very large. To manage its size,
# configure which groups should be created. # configure which groups should be created.
group_by_instance_id = True group_by_instance_id = True

View file

@ -327,6 +327,12 @@ class Ec2Inventory(object):
else: else:
self.nested_groups = False self.nested_groups = False
# Replace dash or not in group names
if config.has_option('ec2', 'replace_dash_in_groups'):
self.replace_dash_in_groups = config.getboolean('ec2', 'replace_dash_in_groups')
else:
self.replace_dash_in_groups = True
# Configure which groups should be created. # Configure which groups should be created.
group_by_options = [ group_by_options = [
'group_by_instance_id', 'group_by_instance_id',
@ -1285,10 +1291,11 @@ class Ec2Inventory(object):
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', temp).lower() return re.sub('([a-z0-9])([A-Z])', r'\1_\2', temp).lower()
def to_safe(self, word): def to_safe(self, word):
''' Converts 'bad' characters in a string to underscores so they can be ''' Converts 'bad' characters in a string to underscores so they can be used as Ansible groups '''
used as Ansible groups ''' regex = "[^A-Za-z0-9\_"
if self.replace_dash_in_groups:
return re.sub("[^A-Za-z0-9\_]", "_", word) regex += "\-"
return re.sub(regex + "]", "_", word)
def json_format_dict(self, data, pretty=False): def json_format_dict(self, data, pretty=False):
''' Converts a dict to a JSON object and dumps it as a formatted ''' Converts a dict to a JSON object and dumps it as a formatted