Use repr on replacements for only_if

This commit is contained in:
Daniel Hokka Zakrisson 2012-09-25 10:37:47 +02:00 committed by Michael DeHaan
parent 489844f960
commit d181a64315
2 changed files with 11 additions and 4 deletions

View file

@ -317,7 +317,7 @@ class Runner(object):
new_args = new_args + "%s='%s' " % (k,v) new_args = new_args + "%s='%s' " % (k,v)
module_args = new_args 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): if not utils.check_conditional(conditional):
result = utils.jsonify(dict(skipped=True)) result = utils.jsonify(dict(skipped=True))
self.callbacks.on_skipped(host, inject.get('item',None)) self.callbacks.on_skipped(host, inject.get('item',None))

View file

@ -209,7 +209,7 @@ def varLookup(varname, vars):
except VarNotFoundException: except VarNotFoundException:
return None return None
def varReplace(raw, vars): def varReplace(raw, vars, do_repr=False):
''' 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
@ -230,6 +230,13 @@ def varReplace(raw, vars):
replacement = m.group() replacement = m.group()
start, end = m.span() 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(raw[:start]) # Keep stuff leading up to token
done.append(replacement) # Append replacement value done.append(replacement) # Append replacement value
raw = raw[end:] # Continue with remainder of string raw = raw[end:] # Continue with remainder of string
@ -271,7 +278,7 @@ def varReplaceFilesAndPipes(basedir, raw):
return ''.join(done) 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 ''' ''' run a text buffer through the templating engine until it no longer changes '''
prev_text = '' prev_text = ''
@ -285,7 +292,7 @@ def template(basedir, text, vars):
if (depth > 20): if (depth > 20):
raise errors.AnsibleError("template recursion depth exceeded") raise errors.AnsibleError("template recursion depth exceeded")
prev_text = text prev_text = text
text = varReplace(unicode(text), vars) text = varReplace(unicode(text), vars, do_repr)
text = varReplaceFilesAndPipes(basedir, text) text = varReplaceFilesAndPipes(basedir, text)
return text return text