Support list indexing in varReplace.

pull/603/head
Jeroen Hoekx 13 years ago
parent 4819ce4989
commit 3cbc229990

@ -199,6 +199,8 @@ def parse_json(data):
return { "failed" : True, "parsed" : False, "msg" : data } return { "failed" : True, "parsed" : False, "msg" : data }
return results return results
_LISTRE = re.compile(r"(\w+)\[(\d+)\]")
def varLookup(name, vars): def varLookup(name, vars):
''' find the contents of a possibly complex variable in vars. ''' ''' find the contents of a possibly complex variable in vars. '''
path = name.split('.') path = name.split('.')
@ -206,11 +208,19 @@ def varLookup(name, vars):
for part in path: for part in path:
if part in space: if part in space:
space = space[part] space = space[part]
elif "[" in part:
m = _LISTRE.search(part)
if not m:
return
try:
space = space[m.group(1)][int(m.group(2))]
except (KeyError, IndexError):
return
else: else:
return return
return space return space
_KEYCRE = re.compile(r"\$(?P<complex>\{){0,1}((?(complex)[\w\.]+|\w+))(?(complex)\})") _KEYCRE = re.compile(r"\$(?P<complex>\{){0,1}((?(complex)[\w\.\[\]]+|\w+))(?(complex)\})")
# if { -> complex if complex, allow . and need trailing } # if { -> complex if complex, allow . and need trailing }
def varReplace(raw, vars): def varReplace(raw, vars):

@ -143,6 +143,56 @@ class TestUtils(unittest.TestCase):
assert res == u'hello wórld' assert res == u'hello wórld'
def test_varReplace_list(self):
template = 'hello ${data[1]}'
vars = {
'data': [ 'no-one', 'world' ]
}
res = ansible.utils.varReplace(template, vars)
assert res == 'hello world'
def test_varReplace_invalid_list(self):
template = 'hello ${data[1}'
vars = {
'data': [ 'no-one', 'world' ]
}
res = ansible.utils.varReplace(template, vars)
assert res == template
def test_varReplace_list_oob(self):
template = 'hello ${data[2]}'
vars = {
'data': [ 'no-one', 'world' ]
}
res = ansible.utils.varReplace(template, vars)
assert res == template
def test_varReplace_list_nolist(self):
template = 'hello ${data[1]}'
vars = {
'data': { 'no-one': 0, 'world': 1 }
}
res = ansible.utils.varReplace(template, vars)
assert res == template
def test_varReplace_nested_list(self):
template = 'hello ${data[1].msg[0]}'
vars = {
'data': [ 'no-one', {'msg': [ 'world'] } ]
}
res = ansible.utils.varReplace(template, vars)
assert res == 'hello world'
##################################### #####################################
### Template function tests ### Template function tests

Loading…
Cancel
Save