def _parse_host_definition(self, line): ''' Takes a single line and tries to parse it as a host definition. Returns a list of Hosts if successful, or raises an error. ''' # A host definition comprises (1) a non-whitespace hostname or range, # optionally followed by (2) a series of key="some value" assignments. # We ignore any trailing whitespace and/or comments. For example, here # are a series of host definitions in a group: # # [groupname] # alpha # beta:2345 user=admin # we'll tell shlex # gamma sudo=True user=root # to ignore comments try: tokens = shlex_split(line, comments=True) except ValueError as e: self._raise_error("Error parsing host definition '%s': %s" % (line, e)) (hostnames, port) = self._expand_hostpattern(tokens[0]) # Try to process anything remaining as a series of key=value pairs. variables = {} for t in tokens[1:]: if '=' not in t: self._raise_error("Expected key=value host variable assignment, got: %s" % (t)) (k, v) = t.split('=', 1) variables[k] = self._parse_value(v) return hostnames, port, variables
def _parse_host_definition(self, line): ''' Takes a single line and tries to parse it as a host definition. Returns a list of Hosts if successful, or raises an error. ''' # A host definition comprises (1) a non-whitespace hostname or range, # optionally followed by (2) a series of key="some value" assignments. # We ignore any trailing whitespace and/or comments. For example, here # are a series of host definitions in a group: # # [groupname] # alpha # beta:2345 user=admin # we'll tell shlex # gamma sudo=True user=root # to ignore comments try: tokens = shlex_split(line, comments=True) except ValueError as e: self._raise_error("Error parsing host definition '%s': %s" % (line, e)) (hostnames, port) = self._expand_hostpattern(tokens[0]) # Try to process anything remaining as a series of key=value pairs. variables = {} for t in tokens[1:]: if '=' not in t: self._raise_error( "Expected key=value host variable assignment, got: %s" % (t)) (k, v) = t.split('=', 1) variables[k] = self._parse_value(v) return hostnames, port, variables
group = section state = 'hosts' data[group] = {} if state not in data[group]: if state == 'vars': data[group][state] = {} else: data[group][state] = [] else: if len(line.strip()) > 0: # Parse hosts or group members/vars try: tokens = shlex_split(line, comments=True) except ValueError as e: msg('E', "Error parsing host definition '%s': %s" % (line, e)) #print('tokens: %s' % tokens) (hostnames, port) = mip._expand_hostpattern(tokens[0]) # Create 'all' group if no group was defined yet if group is None: group = 'all' state = 'hosts' data['all'] = {'hosts': []} tok = []
def main(): # Read command line options parser = argparse.ArgumentParser(description=( 'Dynamic inventory script that reads inventory rootdir in the INI ' 'format.')) parser.add_argument('--rootdir', metavar='rootdir', help='Path to the inventory rootdir') parser.add_argument('--list', action='store_true', help='List all groups and hosts') args = parser.parse_args() # Get the rootdir from the command line arguments if args.rootdir: rootdir = args.rootdir elif 'INI_INVENTORY_FILENAME' in os.environ: rootdir = os.environ['INI_INVENTORY_FILENAME'] else: rootdir = "/Users/idir/MyProjects/workspace/monitoring/inventory/pm0002" #msg('E', 'No inventory file provided.') data = {} mim = MyInventoryModule() for subdir, dirs, files in os.walk(rootdir): for file in files: #print os.path.join(subdir, file) filepath = subdir + os.sep + file if not filepath.endswith(".py"): try: f = open(filepath) except Exception as e: msg( 'E', 'Cannot open inventory file %s. %s' % (filepath, str(e))) group = 'all' state = 'hosts' # Walk through the file and build the data structure for line in f: line = line.strip() # Skip comments and empty lines if line.startswith('#') or line.startswith(';') or len( line) == 0: continue if line.startswith('['): # Parse group section = file + '_' + line[1:-1] if ':' in line: group, state = line[1:-1].split(':') else: group = section state = 'hosts' if group not in data: data[group] = {} if state not in data[group]: if state == 'vars': data[group][state] = {} else: data[group][state] = [] else: # Parse hosts or group members/vars try: tokens = shlex_split(line, comments=True) except ValueError as e: msg( 'E', "Error parsing host definition '%s': %s" % (line, e)) (hostnames, port) = mim._expand_hostpattern(tokens[0]) # Create 'all' group if no group was defined yet if not 'all' in data: group = 'all' state = 'hosts' data['all'] = {'hosts': []} tok = [] if state == 'hosts': tok = tokens[1:] elif state == 'vars': tok = tokens variables = {} for t in tok: if '=' not in t: msg( 'E', "Expected key=value host variable assignment, " "got: %s" % (t)) (k, v) = t.split('=', 1) variables[k] = mim._parse_value(v) if state == 'vars': for key, val in variables.items(): data[group][state][key] = val else: for host in hostnames: data[group][state].append(host) if state == 'hosts' and len(variables): if '_meta' not in data: data['_meta'] = {'hostvars': {}} data['_meta']['hostvars'][host] = {} for key, val in variables.items(): data['_meta']['hostvars'][host][key] = val #try: # f.close() #except IOError as e: # msg('E', 'Cannot close inventory file %s. %s' % (filepath, str(e))) data['all']['hosts'] = list(set(data['all']['hosts'])) if args.list: print(json.dumps(data, sort_keys=True, indent=2))
def test_comments(self): self.assertEqual(shlex_split('"a b" c # d', comments=True), ["a b", "c"])
def main(): # Read command line options parser = argparse.ArgumentParser(description=( 'Dynamic inventory script that reads inventory file in the INI ' 'format.')) parser.add_argument('--filename', metavar='filename', required=True, help='Path to the inventory file') parser.add_argument('--list', action='store_true', help='List all groups and hosts') args = parser.parse_args() # Get the filename from the command line arguments filename = args.filename try: f = open(filename) except Exception as e: msg('E', 'Cannot open inventory file %s. %s' % (filename, str(e))) # Some default values data = {} group = None state = None mip = MyInventoryParser() # Walk through the file and build the data structure for line in f: line = line.strip() # Skip comments and empty lines if line.startswith('#') or line.startswith(';') or len(line) == 0: continue if line.startswith('['): # Parse group section = line[1:-1] if ':' in line: group, state = line[1:-1].split(':') else: group = section state = 'hosts' if group not in data: data[group] = {} if state not in data[group]: if state == 'vars': data[group][state] = {} else: data[group][state] = [] else: # Parse hosts or group members/vars try: tokens = shlex_split(line, comments=True) except ValueError as e: msg('E', "Error parsing host definition '%s': %s" % (line, e)) #print line hostnames = my_expand(tokens[0]) #(hostnames, port) = my_expand(tokens[0]) #hostnames=[] # Create 'all' group if no group was defined yet if group is None: group = 'all' state = 'hosts' data['all'] = {'hosts': []} tok = [] if state == 'hosts': tok = tokens[1:] elif state == 'vars': tok = tokens variables = {} for t in tok: if '=' not in t: msg( 'E', "Expected key=value host variable assignment, " "got: %s" % (t)) (k, v) = t.split('=', 1) #print '%s, %s' % (k,v) variables[k] = v #variables[k] = mip._parse_value(v) if state == 'vars': for key, val in variables.iteritems(): data[group][state][key] = val else: for host in hostnames: data[group][state].append(host) if state == 'hosts' and len(variables): if '_meta' not in data: data['_meta'] = {'hostvars': {}} data['_meta']['hostvars'][host] = {} for key, val in variables.iteritems(): data['_meta']['hostvars'][host][key] = val print(json.dumps(data, sort_keys=True, indent=2)) try: f.close() except IOError as e: msg('E', 'Cannot close inventory file %s. %s' % (filename, str(e)))
def test_unicode(self): self.assertEqual(shlex_split(u"a b \u010D"), [u"a", u"b", u"\u010D"])
def test_quoted(self): self.assertEqual(shlex_split('"a b" c'), ["a b", "c"])
def test_trivial(self): self.assertEqual(shlex_split("a b c"), ["a", "b", "c"])
state = 'hosts' # Added this condition to ensure prior population is not wiped out. if group not in data: data[group] = {} if state not in data[group]: if state == 'vars': data[group][state] = {} else: data[group][state] = [] # Parse hosts or group members/vars try: combined_values = "%s %s" % (line['host'], line['variables']) tokens = shlex_split(combined_values, comments=True) except ValueError as e: msg('E', "Error parsing host definition '%s': %s" % (line, e)) #print('tokens: %s' % tokens) (hostnames, port) = mip._expand_hostpattern(tokens[0]) # Create 'all' group if no group was defined yet if group is None: group = 'all' state = 'hosts' data['all'] = {'hosts': []} tok = [] if state == 'hosts':
group = section state = 'hosts' # Added this condition to ensure prior population is not wiped out. if group not in data: data[group] = {} if state not in data[group]: if state == 'vars': data[group][state] = {} else: data[group][state] = [] # Parse hosts or group members/vars try: tokens = shlex_split(row[0], comments=True) except ValueError as e: msg('E', "Error parsing host definition '%s': %s" % (row, e)) (hostnames, port) = mip._expand_hostpattern(tokens[0]) # Create 'all' group if no group was defined yet if group is None: group = 'all' state = 'hosts' data['all'] = {'hosts': []} tok = [] if state == 'hosts': tok = tokens[1:]