1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Allows Jinja2 that starts a YAML line to not cause a syntax error.

Technically this isn't quite valid YAML when this happens, so we make it valid.  This means that if a future commander
API allows save/load it should make sure it does similar processing.
This commit is contained in:
Michael DeHaan 2013-04-10 18:07:19 -04:00
parent b09ef21ec9
commit d7206d84bb

View file

@ -254,6 +254,28 @@ def parse_json(raw_data):
return { "failed" : True, "parsed" : False, "msg" : orig_data } return { "failed" : True, "parsed" : False, "msg" : orig_data }
return results 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): def parse_yaml(data):
''' convert a yaml string to a data structure ''' ''' convert a yaml string to a data structure '''
return yaml.safe_load(data) return yaml.safe_load(data)
@ -288,6 +310,7 @@ def parse_yaml_from_file(path):
try: try:
data = file(path).read() data = file(path).read()
data = preprocess_yaml(data)
return parse_yaml(data) return parse_yaml(data)
except IOError: except IOError:
raise errors.AnsibleError("file not found: %s" % path) raise errors.AnsibleError("file not found: %s" % path)