Skip to content

Instantly share code, notes, and snippets.

@fholzer
Last active February 21, 2019 20:28
Show Gist options
  • Save fholzer/40aecf4a87e7b6f0f5089e119afeabd6 to your computer and use it in GitHub Desktop.
Save fholzer/40aecf4a87e7b6f0f5089e119afeabd6 to your computer and use it in GitHub Desktop.
ansible inventory plugin patch
--- __init__.py.bak 2019-02-21 17:23:21.768801439 +0000
+++ __init__.py 2019-02-21 20:05:12.171070413 +0000
@@ -40,15 +40,6 @@
from ansible.utils.display import Display
display = Display()
-_SAFE_GROUP = re.compile("[^A-Za-z0-9_]")
-
-
-# Helper methods
-def to_safe_group_name(name):
- ''' Converts 'bad' characters in a string to underscores so they can be used as Ansible hosts or groups '''
- return _SAFE_GROUP.sub("_", name)
-
-
def detect_range(line=None):
'''
A helper function that checks a given host line to see if it contains
@@ -179,6 +170,11 @@
self.display.vvv('Skipping due to inventory source not existing or not being readable by the current user')
return valid
+ def _to_safe_group_name(self, name):
+ ''' Converts 'bad' characters in a string to underscores so they can be used as Ansible hosts or groups '''
+
+ return self.safe_group.sub("_", name)
+
def _populate_host_vars(self, hosts, variables, group=None, port=None):
if not isinstance(variables, Mapping):
raise AnsibleParserError("Invalid data from file, expected dictionary and got:\n\n%s" % to_native(variables))
@@ -215,6 +211,14 @@
if self._options.get('cache'):
self._set_cache_options(self._options)
+ if config.get('safe_group_expression'):
+ try:
+ self.safe_group = re.compile(config.get('safe_group_expression'))
+ except re.error as ex:
+ raise AnsibleParserError("Failed to compile 'safe_group_expression' regular expression: %s" % config.get('safe_group_expression'))
+ else:
+ self.safe_group = re.compile("[^A-Za-z0-9_]")
+
return config
def _set_cache_options(self, options):
@@ -325,18 +329,20 @@
continue
if key:
- prefix = keyed.get('prefix', '')
sep = keyed.get('separator', '_')
+ prefix = keyed.get('prefix', '')
+ if prefix != '':
+ prefix = "%s%s" % (prefix, sep)
if isinstance(key, string_types):
- groups.append('%s%s%s' % (prefix, sep, key))
+ groups.append('%s%s' % (prefix, key))
elif isinstance(key, list):
for name in key:
- groups.append('%s%s%s' % (prefix, sep, name))
+ groups.append('%s%s' % (prefix, name))
elif isinstance(key, Mapping):
for (gname, gval) in key.items():
name = '%s%s%s' % (gname, sep, gval)
- groups.append('%s%s%s' % (prefix, sep, name))
+ groups.append('%s%s' % (prefix, name))
else:
raise AnsibleParserError("Invalid group name format, expected a string or a list of them or dictionary, got: %s" % type(key))
else:
@@ -347,6 +353,6 @@
# now actually add any groups
for group_name in groups:
- gname = to_safe_group_name(group_name)
+ gname = self._to_safe_group_name(group_name)
self.inventory.add_group(gname)
self.inventory.add_child(gname, host)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment