allow references to names of variables in with_items without needing to surround them with Jinja2 '{{' delimeters

This commit is contained in:
Michael DeHaan
2013-04-10 18:42:54 -04:00
parent 9ac25bb8f6
commit f0b21dcc0f
5 changed files with 61 additions and 40 deletions

View File

@@ -254,28 +254,6 @@ def parse_json(raw_data):
return { "failed" : True, "parsed" : False, "msg" : orig_data }
return results
def preprocess_yaml(data):
# allow the following casual user error:
# bar: {{ baz }}
# instead of bar: '{{ baz }}'
# (commander-API should auto-quote these in similar ways on save/load)
new_lines = []
lines = data.split("\n")
for line in lines:
if line.find("{{") != -1 and line.find(":") != -1:
tokens = line.split(":",1)
if tokens[1].strip().startswith("{{"):
new_line = "%s: '%s'" % (tokens[0], tokens[1])
else:
new_line = line
new_lines.append(new_line)
else:
new_lines.append(line)
result = "\n".join(new_lines)
return result
def parse_yaml(data):
''' convert a yaml string to a data structure '''
return yaml.safe_load(data)
@@ -310,7 +288,6 @@ def parse_yaml_from_file(path):
try:
data = file(path).read()
data = preprocess_yaml(data)
return parse_yaml(data)
except IOError:
raise errors.AnsibleError("file not found: %s" % path)
@@ -724,7 +701,12 @@ def safe_eval(str):
# do not allow imports
if re.search(r'import \w+', str):
return str
return eval(str)
try:
return eval(str)
except Exception, e:
return str

View File

@@ -435,7 +435,12 @@ def template_from_string(basedir, data, vars):
raise errors.AnsibleError("recursive loop detected in template string: %s" % data)
else:
return data
def test_foo():
return 'test_foo!'
t.globals['test_foo'] = test_foo
res = jinja2.utils.concat(t.root_render_func(t.new_context(_jinja2_vars(basedir, vars, t.globals), shared=True)))
return res
except jinja2.exceptions.UndefinedError: