summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMartin Krizek <martin.krizek@gmail.com>2021-02-05 19:43:16 +0100
committerGitHub <noreply@github.com>2021-02-05 12:43:16 -0600
commit65fdc41fb180fca8adb552eed1ae24736858c419 (patch)
tree6f5fddeef76a76beac14d15a68c20f8516562b75 /test
parent8e5aab4d76896b4dcf735a626c281ddcb7b2d211 (diff)
downloadansible-65fdc41fb180fca8adb552eed1ae24736858c419.tar.gz
Local vars should have highest precedence in AnsibleJ2Vars (#72830) (#73369)
Ability to add local variables into AnsibleJ2Vars was added in 18a9eff11f0a6e51b17405ce596bd9ff7e676320 to fix #6653. Local variables are added using ``AnsibleJ2Vars.add_locals()`` method when creating a new context - typically when including/importing a template with context. For that use case local template variables created using ``set`` should override variables from higher contexts - either from the play or any parent template, or both; Jinja behaves the same way. Also removes AnsibleJ2Vars.extras instance variable which is not used. Also adds missing test for #6653. Fixes #72262 Fixes #72615 ci_complete (cherry picked from commit a2af8432f36ec8cc5368a747f1211d2b9ba01f2e)
Diffstat (limited to 'test')
-rw-r--r--test/integration/targets/template/6653.yml10
-rw-r--r--test/integration/targets/template/72262.yml6
-rw-r--r--test/integration/targets/template/72615.yml26
-rwxr-xr-xtest/integration/targets/template/runme.sh9
-rw-r--r--test/integration/targets/template/templates/6653-include.j21
-rw-r--r--test/integration/targets/template/templates/6653.j24
-rw-r--r--test/integration/targets/template/templates/72262-included.j21
-rw-r--r--test/integration/targets/template/templates/72262-vars.j21
-rw-r--r--test/integration/targets/template/templates/72262.j23
-rw-r--r--test/integration/targets/template/templates/72615-macro-nested.j24
-rw-r--r--test/integration/targets/template/templates/72615-macro.j28
-rw-r--r--test/integration/targets/template/templates/72615.j24
12 files changed, 77 insertions, 0 deletions
diff --git a/test/integration/targets/template/6653.yml b/test/integration/targets/template/6653.yml
new file mode 100644
index 0000000000..970478f9be
--- /dev/null
+++ b/test/integration/targets/template/6653.yml
@@ -0,0 +1,10 @@
+- hosts: localhost
+ gather_facts: no
+ vars:
+ mylist:
+ - alpha
+ - bravo
+ tasks:
+ - name: Should not fail on undefined variable
+ set_fact:
+ template_result: "{{ lookup('template', '6653.j2') }}"
diff --git a/test/integration/targets/template/72262.yml b/test/integration/targets/template/72262.yml
new file mode 100644
index 0000000000..33c610d4ec
--- /dev/null
+++ b/test/integration/targets/template/72262.yml
@@ -0,0 +1,6 @@
+- hosts: localhost
+ gather_facts: no
+ tasks:
+ - name: Should not fail on undefined variable
+ set_fact:
+ template_result: "{{ lookup('template', '72262.j2') }}"
diff --git a/test/integration/targets/template/72615.yml b/test/integration/targets/template/72615.yml
new file mode 100644
index 0000000000..9a6eb941b4
--- /dev/null
+++ b/test/integration/targets/template/72615.yml
@@ -0,0 +1,26 @@
+- hosts: localhost
+ gather_facts: no
+ vars:
+ foo: "top-level-foo"
+ tasks:
+ - set_fact:
+ template_result: "{{ lookup('template', '72615.j2') }}"
+
+ - assert:
+ that:
+ - "'template-level-bar' in template_result"
+ - "'template-nested-level-bar' in template_result"
+
+ - assert:
+ that:
+ - "'top-level-foo' not in template_result"
+ - "'template-level-foo' in template_result"
+ - "'template-nested-level-foo' in template_result"
+ when: lookup('pipe', ansible_python_interpreter ~ ' -c "import jinja2; print(jinja2.__version__)"') is version('2.9', '>=')
+
+ - assert:
+ that:
+ - "'top-level-foo' in template_result"
+ - "'template-level-foo' not in template_result"
+ - "'template-nested-level-foo' not in template_result"
+ when: lookup('pipe', ansible_python_interpreter ~ ' -c "import jinja2; print(jinja2.__version__)"') is version('2.9', '<')
diff --git a/test/integration/targets/template/runme.sh b/test/integration/targets/template/runme.sh
index 6edd4fdc48..b634e9d989 100755
--- a/test/integration/targets/template/runme.sh
+++ b/test/integration/targets/template/runme.sh
@@ -25,3 +25,12 @@ ansible-playbook undefined_var_info.yml -v "$@"
# https://github.com/ansible/ansible/issues/68699
ansible-playbook unused_vars_include.yml -v "$@"
+
+# https://github.com/ansible/ansible/issues/72615
+ansible-playbook 72615.yml -v "$@"
+
+# https://github.com/ansible/ansible/issues/6653
+ansible-playbook 6653.yml -v "$@"
+
+# https://github.com/ansible/ansible/issues/72262
+ansible-playbook 72262.yml -v "$@"
diff --git a/test/integration/targets/template/templates/6653-include.j2 b/test/integration/targets/template/templates/6653-include.j2
new file mode 100644
index 0000000000..26443b151b
--- /dev/null
+++ b/test/integration/targets/template/templates/6653-include.j2
@@ -0,0 +1 @@
+{{ x }}
diff --git a/test/integration/targets/template/templates/6653.j2 b/test/integration/targets/template/templates/6653.j2
new file mode 100644
index 0000000000..8026a79bef
--- /dev/null
+++ b/test/integration/targets/template/templates/6653.j2
@@ -0,0 +1,4 @@
+{% for x in mylist %}
+{{ x }}
+{% include '6653-include.j2' with context %}
+{% endfor %}
diff --git a/test/integration/targets/template/templates/72262-included.j2 b/test/integration/targets/template/templates/72262-included.j2
new file mode 100644
index 0000000000..35700cb8ef
--- /dev/null
+++ b/test/integration/targets/template/templates/72262-included.j2
@@ -0,0 +1 @@
+{{ vars.test }}
diff --git a/test/integration/targets/template/templates/72262-vars.j2 b/test/integration/targets/template/templates/72262-vars.j2
new file mode 100644
index 0000000000..6ef92208bb
--- /dev/null
+++ b/test/integration/targets/template/templates/72262-vars.j2
@@ -0,0 +1 @@
+{% set test = "I'm test variable" %}
diff --git a/test/integration/targets/template/templates/72262.j2 b/test/integration/targets/template/templates/72262.j2
new file mode 100644
index 0000000000..b72be0d18d
--- /dev/null
+++ b/test/integration/targets/template/templates/72262.j2
@@ -0,0 +1,3 @@
+{% import '72262-vars.j2' as vars with context %}
+{% macro included() %}{% include '72262-included.j2' %}{% endmacro %}
+{{ included()|indent }}
diff --git a/test/integration/targets/template/templates/72615-macro-nested.j2 b/test/integration/targets/template/templates/72615-macro-nested.j2
new file mode 100644
index 0000000000..c47a49922f
--- /dev/null
+++ b/test/integration/targets/template/templates/72615-macro-nested.j2
@@ -0,0 +1,4 @@
+{% macro print_context_vars_nested(value) %}
+foo: {{ foo }}
+bar: {{ value }}
+{% endmacro %}
diff --git a/test/integration/targets/template/templates/72615-macro.j2 b/test/integration/targets/template/templates/72615-macro.j2
new file mode 100644
index 0000000000..328c271cd5
--- /dev/null
+++ b/test/integration/targets/template/templates/72615-macro.j2
@@ -0,0 +1,8 @@
+{% macro print_context_vars(value) %}
+{{ foo }}
+{{ value }}
+{% set foo = "template-nested-level-foo" %}
+{% set bar = "template-nested-level-bar" %}
+{% from '72615-macro-nested.j2' import print_context_vars_nested with context %}
+{{ print_context_vars_nested(bar) }}
+{% endmacro %}
diff --git a/test/integration/targets/template/templates/72615.j2 b/test/integration/targets/template/templates/72615.j2
new file mode 100644
index 0000000000..b79f88e26d
--- /dev/null
+++ b/test/integration/targets/template/templates/72615.j2
@@ -0,0 +1,4 @@
+{% set foo = "template-level-foo" %}
+{% set bar = "template-level-bar" %}
+{% from '72615-macro.j2' import print_context_vars with context %}
+{{ print_context_vars(bar) }}