Make varReplace recursive instead of iterative
parent
d181a64315
commit
84f98534ae
|
@ -176,12 +176,13 @@ _LISTRE = re.compile(r"(\w+)\[(\d+)\]")
|
||||||
class VarNotFoundException(Exception):
|
class VarNotFoundException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _varLookup(name, vars):
|
def _varLookup(name, vars, depth=0):
|
||||||
''' 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('.')
|
||||||
space = vars
|
space = vars
|
||||||
for part in path:
|
for part in path:
|
||||||
|
part = varReplace(part, vars, depth=depth + 1)
|
||||||
if part in space:
|
if part in space:
|
||||||
space = space[part]
|
space = space[part]
|
||||||
elif "[" in part:
|
elif "[" in part:
|
||||||
|
@ -196,7 +197,7 @@ def _varLookup(name, vars):
|
||||||
raise VarNotFoundException()
|
raise VarNotFoundException()
|
||||||
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)\})")
|
||||||
|
|
||||||
def varLookup(varname, vars):
|
def varLookup(varname, vars):
|
||||||
''' helper function used by with_items '''
|
''' helper function used by with_items '''
|
||||||
|
@ -209,10 +210,13 @@ def varLookup(varname, vars):
|
||||||
except VarNotFoundException:
|
except VarNotFoundException:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def varReplace(raw, vars, do_repr=False):
|
def varReplace(raw, vars, do_repr=False, depth=0):
|
||||||
''' Perform variable replacement of $variables in string raw using vars dictionary '''
|
''' Perform variable replacement of $variables in string raw using vars dictionary '''
|
||||||
# this code originally from yum
|
# this code originally from yum
|
||||||
|
|
||||||
|
if (depth > 20):
|
||||||
|
raise errors.AnsibleError("template recursion depth exceeded")
|
||||||
|
|
||||||
done = [] # Completed chunks to return
|
done = [] # Completed chunks to return
|
||||||
|
|
||||||
while raw:
|
while raw:
|
||||||
|
@ -225,7 +229,8 @@ def varReplace(raw, vars, do_repr=False):
|
||||||
# original)
|
# original)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
replacement = unicode(_varLookup(m.group(2), vars))
|
replacement = unicode(_varLookup(m.group(2), vars, depth))
|
||||||
|
replacement = varReplace(replacement, vars, depth=depth + 1)
|
||||||
except VarNotFoundException:
|
except VarNotFoundException:
|
||||||
replacement = m.group()
|
replacement = m.group()
|
||||||
|
|
||||||
|
@ -286,13 +291,7 @@ def template(basedir, text, vars, do_repr=False):
|
||||||
text = text.decode('utf-8')
|
text = text.decode('utf-8')
|
||||||
except UnicodeEncodeError:
|
except UnicodeEncodeError:
|
||||||
pass # already unicode
|
pass # already unicode
|
||||||
depth = 0
|
text = varReplace(unicode(text), vars, do_repr)
|
||||||
while prev_text != text:
|
|
||||||
depth = depth + 1
|
|
||||||
if (depth > 20):
|
|
||||||
raise errors.AnsibleError("template recursion depth exceeded")
|
|
||||||
prev_text = text
|
|
||||||
text = varReplace(unicode(text), vars, do_repr)
|
|
||||||
text = varReplaceFilesAndPipes(basedir, text)
|
text = varReplaceFilesAndPipes(basedir, text)
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue