Make parsing variables for hosts and groups more consistent
This commit allows to specify arrays in [group:vars] block. Signed-off-by: Oleg Bulatov <oleg@bulatov.me>
This commit is contained in:
parent
138b45e32f
commit
f6a4c20cc0
3 changed files with 20 additions and 24 deletions
|
@ -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 {}
|
||||
|
|
|
@ -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' ]
|
||||
)
|
||||
|
|
|
@ -40,6 +40,7 @@ e = 10003
|
|||
h = ' h '
|
||||
i = ' i "
|
||||
j = " j
|
||||
k = ['k1', 'k2']
|
||||
|
||||
[rtp]
|
||||
rtp_a
|
||||
|
|
Loading…
Reference in a new issue