template override colon bugfixes (#77495)

* Proper error on missing jinja2 override separator

Properly catch the case when no (or a wrong) separator is used in a
jinja2 override, and return a useful error message to the user.

* Support colons in jinja2 override value

By limiting the split to 1, any colons in the value are preserved and
passed on.
pull/77516/head
Corubba 3 years ago committed by GitHub
parent 94eff70030
commit 94138cf608
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
bugfixes:
- More gracefully handle separator errors in jinja2 template overrides (https://github.com/ansible/ansible/pull/77495).
- Support colons in jinja2 template override values (https://github.com/ansible/ansible/pull/77495).

@ -1054,7 +1054,10 @@ class Templar:
line = data[len(JINJA2_OVERRIDE):eol]
data = data[eol + 1:]
for pair in line.split(','):
(key, val) = pair.split(':')
if ':' not in pair:
raise AnsibleError("failed to parse jinja2 override '%s'."
" Did you use something different from colon as key-value separator?" % pair.strip())
(key, val) = pair.split(':', 1)
key = key.strip()
setattr(myenv, key, ast.literal_eval(val.strip()))

@ -761,3 +761,35 @@
- test
vars:
test: "{{ lookup('file', '{{ output_dir }}/empty_template.templated')|length == 0 }}"
- name: test jinja2 override without colon throws proper error
block:
- template:
src: override_separator.j2
dest: "{{ output_dir }}/override_separator.templated"
- assert:
that:
- False
rescue:
- assert:
that:
- "'failed to parse jinja2 override' in ansible_failed_result.msg"
- name: test jinja2 override with colon in value
template:
src: override_colon_value.j2
dest: "{{ output_dir }}/override_colon_value.templated"
ignore_errors: yes
register: override_colon_value_task
- copy:
src: override_colon_value.expected
dest: "{{output_dir}}/override_colon_value.expected"
- command: "diff {{ output_dir }}/override_colon_value.templated {{ output_dir}}/override_colon_value.expected"
register: override_colon_value_diff
- assert:
that:
- override_colon_value_task is success
- override_colon_value_diff.rc == 0

@ -0,0 +1,4 @@
#jinja2: line_statement_prefix:":"
: if true
foo
: endif
Loading…
Cancel
Save