Skip to content

Commit

Permalink
Ensure Jinja2 template header overrides are used (#75306)
Browse files Browse the repository at this point in the history
Fixes #75275
  • Loading branch information
mkrizek committed Jul 26, 2021
1 parent e5a2fe4 commit 767b2f0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
@@ -0,0 +1,2 @@
bugfixes:
- template - ensure Jinja2 overrides from template header are used (https://github.com/ansible/ansible/issues/75275)
16 changes: 11 additions & 5 deletions lib/ansible/template/__init__.py
Expand Up @@ -1079,15 +1079,21 @@ def do_template(self, data, preserve_trailing_newlines=True, escape_backslashes=
if fail_on_undefined is None:
fail_on_undefined = self._fail_on_undefined_errors

has_template_overrides = data.startswith(JINJA2_OVERRIDE)

try:
# allows template header overrides to change jinja2 options.
if overrides is None:
myenv = self.environment
else:
# NOTE Creating an overlay that lives only inside do_template means that overrides are not applied
# when templating nested variables in AnsibleJ2Vars where Templar.environment is used, not the overlay.
# This is historic behavior that is kept for backwards compatibility.
if overrides:
myenv = self.environment.overlay(overrides)
elif has_template_overrides:
myenv = self.environment.overlay()
else:
myenv = self.environment

# Get jinja env overrides from template
if hasattr(data, 'startswith') and data.startswith(JINJA2_OVERRIDE):
if has_template_overrides:
eol = data.find('\n')
line = data[len(JINJA2_OVERRIDE):eol]
data = data[eol + 1:]
Expand Down
5 changes: 5 additions & 0 deletions test/integration/targets/template/in_template_overrides.j2
@@ -0,0 +1,5 @@
#jinja2:variable_start_string:'<<' , variable_end_string:'>>'
var_a: << var_a >>
var_b: << var_b >>
var_c: << var_c >>
var_d: << var_d >>
28 changes: 28 additions & 0 deletions test/integration/targets/template/in_template_overrides.yml
@@ -0,0 +1,28 @@
- hosts: localhost
gather_facts: false
vars:
var_a: "value"
var_b: "{{ var_a }}"
var_c: "<< var_a >>"
tasks:
- set_fact:
var_d: "{{ var_a }}"

- block:
- template:
src: in_template_overrides.j2
dest: out.txt

- command: cat out.txt
register: out

- assert:
that:
- "'var_a: value' in out.stdout"
- "'var_b: value' in out.stdout"
- "'var_c: << var_a >>' in out.stdout"
- "'var_d: value' in out.stdout"
always:
- file:
path: out.txt
state: absent
2 changes: 2 additions & 0 deletions test/integration/targets/template/runme.sh
Expand Up @@ -38,3 +38,5 @@ ansible-playbook 72262.yml -v "$@"
# ensure unsafe is preserved, even with extra newlines
ansible-playbook unsafe.yml -v "$@"

# ensure Jinja2 overrides from a template are used
ansible-playbook in_template_overrides.yml -v "$@"

0 comments on commit 767b2f0

Please sign in to comment.