Commit 49bd8b0b by Richard C Isaacson

Fix inventory for test_dir_inventory

It came up that fixing this unit test may relate to another ticket that is open. This work allows us to uncomment this unit test by fixing how we pars variables allowing a quoted variable to contain a '#'.

Work also went into cleaning up some of the test data to clarify what was working.

Lastly work went into cleaning up formatting so that the code is easily read.
parent ca4ff261
...@@ -27,6 +27,7 @@ import shlex ...@@ -27,6 +27,7 @@ import shlex
import re import re
import ast import ast
class InventoryParser(object): class InventoryParser(object):
""" """
Host inventory for ansible. Host inventory for ansible.
...@@ -47,7 +48,6 @@ class InventoryParser(object): ...@@ -47,7 +48,6 @@ class InventoryParser(object):
self._parse_group_variables() self._parse_group_variables()
return self.groups return self.groups
# [webservers] # [webservers]
# alpha # alpha
# beta:2345 # beta:2345
...@@ -65,9 +65,36 @@ class InventoryParser(object): ...@@ -65,9 +65,36 @@ class InventoryParser(object):
active_group_name = 'ungrouped' active_group_name = 'ungrouped'
for line in self.lines: for line in self.lines:
line = line.split("#")[0].strip()
# Split off any comments that are not contained in a variable.
if "#" in line:
split_line = line.split("#")
instances = len(split_line) - 1
if instances > 0:
marker = 0
while marker < instances:
if ("=\"" in split_line[marker] and "\"" in split_line[marker + 1]) or (
"='" in split_line[marker] and "'" in split_line[marker + 1]):
marker += 1
else:
if marker == 0:
line = split_line[marker]
else:
# We have multiple fragments that we need to combine back together.
# rekram is us reversing that work we did with marker.
rekram = 0
new_line = split_line[rekram]
while marker > rekram:
rekram += 1
new_line = new_line + "#" + split_line[rekram]
line = new_line
break
# Clean up the end of the line.
line = line.strip()
if line.startswith("[") and line.endswith("]"): if line.startswith("[") and line.endswith("]"):
active_group_name = line.replace("[","").replace("]","") active_group_name = line.replace("[", "").replace("]", "")
if line.find(":vars") != -1 or line.find(":children") != -1: if line.find(":vars") != -1 or line.find(":children") != -1:
active_group_name = active_group_name.rsplit(":", 1)[0] active_group_name = active_group_name.rsplit(":", 1)[0]
if active_group_name not in self.groups: if active_group_name not in self.groups:
...@@ -95,20 +122,18 @@ class InventoryParser(object): ...@@ -95,20 +122,18 @@ class InventoryParser(object):
if hostname.count(".") == 1: if hostname.count(".") == 1:
(hostname, port) = hostname.rsplit(".", 1) (hostname, port) = hostname.rsplit(".", 1)
elif (hostname.find("[") != -1 and elif (hostname.find("[") != -1 and
hostname.find("]") != -1 and hostname.find("]") != -1 and
hostname.find(":") != -1 and hostname.find(":") != -1 and
(hostname.rindex("]") < hostname.rindex(":")) or (hostname.rindex("]") < hostname.rindex(":")) or
(hostname.find("]") == -1 and hostname.find(":") != -1)): (hostname.find("]") == -1 and hostname.find(":") != -1)):
(hostname, port) = hostname.rsplit(":", 1) (hostname, port) = hostname.rsplit(":", 1)
hostnames = []
if detect_range(hostname): if detect_range(hostname):
hostnames = expand_hostname_range(hostname) hostnames = expand_hostname_range(hostname)
else: else:
hostnames = [hostname] hostnames = [hostname]
for hn in hostnames: for hn in hostnames:
host = None
if hn in self.hosts: if hn in self.hosts:
host = self.hosts[hn] host = self.hosts[hn]
else: else:
...@@ -119,15 +144,24 @@ class InventoryParser(object): ...@@ -119,15 +144,24 @@ class InventoryParser(object):
if t.startswith('#'): if t.startswith('#'):
break break
try: try:
(k,v) = t.split("=", 1) (k, v) = t.split("=", 1)
except ValueError, e: except ValueError, e:
raise errors.AnsibleError("Invalid ini entry: %s - %s" % (t, str(e))) raise errors.AnsibleError("Invalid ini entry: %s - %s" % (t, str(e)))
try: # I am not sure where a variable with a hash needs to be evaluated via ast.
host.set_variable(k,ast.literal_eval(v)) # If an instance comes up this is the condition we need to modify.
except: if "#" in v:
# most likely a string that literal_eval host.set_variable(k, v)
# doesn't like, so just set it else:
host.set_variable(k,v) try:
host.set_variable(k, ast.literal_eval(v))
# Using explicit exceptions.
# Likely a string that literal_eval does not like. We wil then just set it.
except ValueError:
# For some reason this was thought to be malformed.
host.set_variable(k, v)
except SyntaxError:
# Is this a hash with an equals at the end?
host.set_variable(k, v)
self.groups[active_group_name].add_host(host) self.groups[active_group_name].add_host(host)
# [southeast:children] # [southeast:children]
...@@ -142,7 +176,7 @@ class InventoryParser(object): ...@@ -142,7 +176,7 @@ class InventoryParser(object):
if line is None or line == '': if line is None or line == '':
continue continue
if line.startswith("[") and line.find(":children]") != -1: if line.startswith("[") and line.find(":children]") != -1:
line = line.replace("[","").replace(":children]","") line = line.replace("[", "").replace(":children]", "")
group = self.groups.get(line, None) group = self.groups.get(line, None)
if group is None: if group is None:
group = self.groups[line] = Group(name=line) group = self.groups[line] = Group(name=line)
...@@ -157,7 +191,6 @@ class InventoryParser(object): ...@@ -157,7 +191,6 @@ class InventoryParser(object):
else: else:
group.add_child_group(kid_group) group.add_child_group(kid_group)
# [webservers:vars] # [webservers:vars]
# http_port=1234 # http_port=1234
# maxRequestsPerChild=200 # maxRequestsPerChild=200
...@@ -167,7 +200,7 @@ class InventoryParser(object): ...@@ -167,7 +200,7 @@ class InventoryParser(object):
for line in self.lines: for line in self.lines:
line = line.strip() line = line.strip()
if line.startswith("[") and line.find(":vars]") != -1: if line.startswith("[") and line.find(":vars]") != -1:
line = line.replace("[","").replace(":vars]","") line = line.replace("[", "").replace(":vars]", "")
group = self.groups.get(line, None) group = self.groups.get(line, None)
if group is None: if group is None:
raise errors.AnsibleError("can't add vars to undefined group: %s" % line) raise errors.AnsibleError("can't add vars to undefined group: %s" % line)
......
zeus var_a=2 zeus var_a=0
morpheus morpheus
thor thor
[titan]
cronus var_a="a#b" var_b="b#c" var_c="c#d" # Is this overkill?
[major-god] # group with inline comments [major-god] # group with inline comments
zeus var_a="1#2" # host with inline comments and "#" in the var string zeus var_a="2#3" # host with inline comments and "#" in the var string
# A comment # A comment
thor thor
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment