From f6a4c20cc0a1a35af0826f255a58f0f3dfd79791 Mon Sep 17 00:00:00 2001 From: Oleg Bulatov Date: Sun, 16 Mar 2014 21:47:26 +0400 Subject: [PATCH] Make parsing variables for hosts and groups more consistent This commit allows to specify arrays in [group:vars] block. Signed-off-by: Oleg Bulatov --- lib/ansible/inventory/ini.py | 38 +++++++++----------- test/units/TestInventory.py | 5 +-- test/units/inventory_test_data/complex_hosts | 1 + 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/lib/ansible/inventory/ini.py b/lib/ansible/inventory/ini.py index 3b38911d25..9863de17b8 100644 --- a/lib/ansible/inventory/ini.py +++ b/lib/ansible/inventory/ini.py @@ -48,6 +48,20 @@ class InventoryParser(object): self._parse_group_variables() return self.groups + @staticmethod + def _parse_value(v): + if "#" not in v: + try: + return 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. + pass + except SyntaxError: + # Is this a hash with an equals at the end? + pass + return v # [webservers] # alpha @@ -123,22 +137,7 @@ class InventoryParser(object): (k,v) = t.split("=", 1) except ValueError, e: raise errors.AnsibleError("Invalid ini entry: %s - %s" % (t, str(e))) - - # If there is a hash in the value don't pass it through to ast at ast will split at the hash. - if "#" in v: - host.set_variable(k, v) - else: - 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) - + host.set_variable(k, self._parse_value(v)) self.groups[active_group_name].add_host(host) # [southeast:children] @@ -193,12 +192,7 @@ class InventoryParser(object): raise errors.AnsibleError("variables assigned to group must be in key=value form") else: (k, v) = [e.strip() for e in line.split("=", 1)] - # When the value is a single-quoted or double-quoted string - if re.match(r"^(['\"]).*\1$", v): - # Unquote the string - group.set_variable(k, re.sub(r"^['\"]|['\"]$", '', v)) - else: - group.set_variable(k, v) + group.set_variable(k, self._parse_value(v)) def get_host_variables(self, host): return {} diff --git a/test/units/TestInventory.py b/test/units/TestInventory.py index f8e9232c54..4aae739a23 100644 --- a/test/units/TestInventory.py +++ b/test/units/TestInventory.py @@ -236,9 +236,10 @@ class TestInventory(unittest.TestCase): print vars expected = dict( - a='1', b='2', c='3', d='10002', e='10003', f='10004 != 10005', + a=1, b=2, c=3, d=10002, e=10003, f='10004 != 10005', g=' g ', h=' h ', i="' i \"", j='" j', - rga='1', rgb='2', rgc='3', + k=[ 'k1', 'k2' ], + rga=1, rgb=2, rgc=3, inventory_hostname='rtp_a', inventory_hostname_short='rtp_a', group_names=[ 'eastcoast', 'nc', 'redundantgroup', 'redundantgroup2', 'redundantgroup3', 'rtp', 'us' ] ) diff --git a/test/units/inventory_test_data/complex_hosts b/test/units/inventory_test_data/complex_hosts index d7f172f203..0217d03f99 100644 --- a/test/units/inventory_test_data/complex_hosts +++ b/test/units/inventory_test_data/complex_hosts @@ -40,6 +40,7 @@ e = 10003 h = ' h ' i = ' i " j = " j + k = ['k1', 'k2'] [rtp] rtp_a