From 4432c01cebf079493e8ae1a6ae0d5d0fcd07188f Mon Sep 17 00:00:00 2001 From: Pol Llovet Date: Fri, 31 May 2013 12:34:45 -0600 Subject: [PATCH] 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. --- lib/ansible/inventory/ini.py | 4 ++-- test/TestInventory.py | 2 +- test/inventory_dir/3comments | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ansible/inventory/ini.py b/lib/ansible/inventory/ini.py index 314f2c7cd96..8412f4acac5 100644 --- a/lib/ansible/inventory/ini.py +++ b/lib/ansible/inventory/ini.py @@ -65,7 +65,7 @@ class InventoryParser(object): for line in self.lines: 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: active_group_name = active_group_name.rsplit(":", 1)[0] if active_group_name not in self.groups: @@ -78,7 +78,7 @@ class InventoryParser(object): elif line.startswith("#") or line == '': pass elif active_group_name: - tokens = shlex.split(line.split("#")[0]) + tokens = shlex.split(line.split(" #")[0]) if len(tokens) == 0: continue hostname = tokens[0] diff --git a/test/TestInventory.py b/test/TestInventory.py index 9db3a15d45e..18e3fea0896 100644 --- a/test/TestInventory.py +++ b/test/TestInventory.py @@ -302,4 +302,4 @@ class TestInventory(unittest.TestCase): assert vars == {'inventory_hostname': 'zeus', 'inventory_hostname_short': 'zeus', 'group_names': ['greek', 'major-god', 'ungrouped'], - 'var_a': '1'} + 'var_a': '1#2'} diff --git a/test/inventory_dir/3comments b/test/inventory_dir/3comments index 02ff6ec000b..74642f13cc7 100644 --- a/test/inventory_dir/3comments +++ b/test/inventory_dir/3comments @@ -1,5 +1,5 @@ [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 thor