From d181a643155f12ec99222bf5177b668addf6bda6 Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Tue, 25 Sep 2012 10:37:47 +0200 Subject: [PATCH] Use repr on replacements for only_if --- lib/ansible/runner/__init__.py | 2 +- lib/ansible/utils.py | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 29b39a086ff..0f9b88bbc77 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -317,7 +317,7 @@ class Runner(object): new_args = new_args + "%s='%s' " % (k,v) module_args = new_args - conditional = utils.template(self.basedir, self.conditional, inject) + conditional = utils.template(self.basedir, self.conditional, inject, do_repr=True) if not utils.check_conditional(conditional): result = utils.jsonify(dict(skipped=True)) self.callbacks.on_skipped(host, inject.get('item',None)) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index d6eee065165..3bab9044c1d 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -209,7 +209,7 @@ def varLookup(varname, vars): except VarNotFoundException: return None -def varReplace(raw, vars): +def varReplace(raw, vars, do_repr=False): ''' Perform variable replacement of $variables in string raw using vars dictionary ''' # this code originally from yum @@ -230,6 +230,13 @@ def varReplace(raw, vars): replacement = m.group() start, end = m.span() + if do_repr: + replacement = repr(replacement) + if (start > 0 and + ((raw[start - 1] == "'" and raw[end] == "'") or + (raw[start - 1] == '"' and raw[end] == '"'))): + start -= 1 + end += 1 done.append(raw[:start]) # Keep stuff leading up to token done.append(replacement) # Append replacement value raw = raw[end:] # Continue with remainder of string @@ -271,7 +278,7 @@ def varReplaceFilesAndPipes(basedir, raw): return ''.join(done) -def template(basedir, text, vars): +def template(basedir, text, vars, do_repr=False): ''' run a text buffer through the templating engine until it no longer changes ''' prev_text = '' @@ -285,7 +292,7 @@ def template(basedir, text, vars): if (depth > 20): raise errors.AnsibleError("template recursion depth exceeded") prev_text = text - text = varReplace(unicode(text), vars) + text = varReplace(unicode(text), vars, do_repr) text = varReplaceFilesAndPipes(basedir, text) return text