Skip to content

Commit

Permalink
Merge pull request #946 from dhozac/false-vars
Browse files Browse the repository at this point in the history
Allow variable expansion for vars that evaluate to false
  • Loading branch information
mpdehaan committed Aug 28, 2012
2 parents 8371871 + 89a31b0 commit 8c0af4b
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/ansible/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ def parse_json(raw_data):

_LISTRE = re.compile(r"(\w+)\[(\d+)\]")

class VarNotFoundException(Exception):
pass

def _varLookup(name, vars):
''' find the contents of a possibly complex variable in vars. '''

Expand All @@ -167,13 +170,13 @@ def _varLookup(name, vars):
elif "[" in part:
m = _LISTRE.search(part)
if not m:
return
raise VarNotFoundException()
try:
space = space[m.group(1)][int(m.group(2))]
except (KeyError, IndexError):
return
raise VarNotFoundException()
else:
return
raise VarNotFoundException()
return space

_KEYCRE = re.compile(r"\$(?P<complex>\{){0,1}((?(complex)[\w\.\[\]]+|\w+))(?(complex)\})")
Expand All @@ -184,7 +187,10 @@ def varLookup(varname, vars):
m = _KEYCRE.search(varname)
if not m:
return None
return _varLookup(m.group(2), vars)
try:
return _varLookup(m.group(2), vars)
except VarNotFoundException:
return None

def varReplace(raw, vars):
''' Perform variable replacement of $variables in string raw using vars dictionary '''
Expand All @@ -202,7 +208,10 @@ def varReplace(raw, vars):
# original)
varname = m.group(2)

replacement = unicode(_varLookup(varname, vars) or m.group())
try:
replacement = unicode(_varLookup(varname, vars))
except VarNotFoundException:
replacement = m.group()

start, end = m.span()
done.append(raw[:start]) # Keep stuff leading up to token
Expand Down

0 comments on commit 8c0af4b

Please sign in to comment.