Handle '#' in var strings by splitting on ' #'

If someone has a " #" in a quoted var string, it
will interpret that as a comment and refuse to
load the inventory file due to an unbalanced
quote. Noisy failure > unexpected behavior.
This commit is contained in:
Pol Llovet 2013-05-31 12:34:45 -06:00
parent 4a5b567f0c
commit 4432c01ceb
3 changed files with 4 additions and 4 deletions

View file

@ -65,7 +65,7 @@ class InventoryParser(object):
for line in self.lines: for line in self.lines:
if line.startswith("["): if line.startswith("["):
active_group_name = line.split("#")[0].replace("[","").replace("]","").strip() active_group_name = line.split(" #")[0].replace("[","").replace("]","").strip()
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:
@ -78,7 +78,7 @@ class InventoryParser(object):
elif line.startswith("#") or line == '': elif line.startswith("#") or line == '':
pass pass
elif active_group_name: elif active_group_name:
tokens = shlex.split(line.split("#")[0]) tokens = shlex.split(line.split(" #")[0])
if len(tokens) == 0: if len(tokens) == 0:
continue continue
hostname = tokens[0] hostname = tokens[0]

View file

@ -302,4 +302,4 @@ class TestInventory(unittest.TestCase):
assert vars == {'inventory_hostname': 'zeus', assert vars == {'inventory_hostname': 'zeus',
'inventory_hostname_short': 'zeus', 'inventory_hostname_short': 'zeus',
'group_names': ['greek', 'major-god', 'ungrouped'], 'group_names': ['greek', 'major-god', 'ungrouped'],
'var_a': '1'} 'var_a': '1#2'}

View file

@ -1,5 +1,5 @@
[major-god] # group with inline comments [major-god] # group with inline comments
zeus var_a=1 # host with inline comments zeus var_a="1#2" # host with inline comments and "#" in the var string
# A comment # A comment
thor thor