From 89a31b0a2adff393aceab55534963926839a5ea3 Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Mon, 27 Aug 2012 15:41:56 +0200 Subject: [PATCH] Allow variable expansion for vars that evaluate to false --- lib/ansible/utils.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index 3743337e74..f0ceaad122 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -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. ''' @@ -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\{){0,1}((?(complex)[\w\.\[\]]+|\w+))(?(complex)\})") @@ -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 ''' @@ -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