When using ANSIBLE_JINJA2_NATIVE bypass our None filtering in _finalze (#41408)
* When using ANSIBLE_JINJA2_NATIVE bypass our None filtering in _finalize. Fixes #41392 * Add tests for _finalize bypass * Address python3 failures in tests
This commit is contained in:
parent
46ae1a343a
commit
ad0827e5c0
6 changed files with 26 additions and 8 deletions
|
@ -609,8 +609,13 @@ class Templar:
|
||||||
|
|
||||||
def _finalize(self, thing):
|
def _finalize(self, thing):
|
||||||
'''
|
'''
|
||||||
A custom finalize method for jinja2, which prevents None from being returned
|
A custom finalize method for jinja2, which prevents None from being returned. This
|
||||||
|
avoids a string of ``"None"`` as ``None`` has no importance in YAML.
|
||||||
|
|
||||||
|
If using ANSIBLE_JINJA2_NATIVE we bypass this and return the actual value always
|
||||||
'''
|
'''
|
||||||
|
if USE_JINJA2_NATIVE:
|
||||||
|
return thing
|
||||||
return thing if thing is not None else ''
|
return thing if thing is not None else ''
|
||||||
|
|
||||||
def _fail_lookup(self, name, *args, **kwargs):
|
def _fail_lookup(self, name, *args, **kwargs):
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
b_false: False
|
b_false: False
|
||||||
s_true: "True"
|
s_true: "True"
|
||||||
s_false: "False"
|
s_false: "False"
|
||||||
|
yaml_none: ~
|
||||||
tasks:
|
tasks:
|
||||||
- name: check jinja version
|
- name: check jinja version
|
||||||
shell: python -c 'import jinja2; print(jinja2.__version__)'
|
shell: python -c 'import jinja2; print(jinja2.__version__)'
|
||||||
|
@ -44,4 +45,5 @@
|
||||||
- import_tasks: test_bool.yml
|
- import_tasks: test_bool.yml
|
||||||
- import_tasks: test_dunder.yml
|
- import_tasks: test_dunder.yml
|
||||||
- import_tasks: test_types.yml
|
- import_tasks: test_types.yml
|
||||||
|
- import_tasks: test_none.yml
|
||||||
when: is_native
|
when: is_native
|
||||||
|
|
|
@ -11,11 +11,11 @@
|
||||||
- assert:
|
- assert:
|
||||||
that:
|
that:
|
||||||
- 'int_to_str == "2"'
|
- 'int_to_str == "2"'
|
||||||
- 'int_to_str|type_debug in ["string", "unicode"]'
|
- 'int_to_str|type_debug in ["str", "unicode"]'
|
||||||
- 'str_to_int == 2'
|
- 'str_to_int == 2'
|
||||||
- 'str_to_int|type_debug == "int"'
|
- 'str_to_int|type_debug == "int"'
|
||||||
- 'dict_to_str|type_debug in ["string", "unicode"]'
|
- 'dict_to_str|type_debug in ["str", "unicode"]'
|
||||||
- 'list_to_str|type_debug in ["string", "unicode"]'
|
- 'list_to_str|type_debug in ["str", "unicode"]'
|
||||||
- 'int_to_bool is sameas true'
|
- 'int_to_bool is sameas true'
|
||||||
- 'int_to_bool|type_debug == "bool"'
|
- 'int_to_bool|type_debug == "bool"'
|
||||||
- 'str_true_to_bool is sameas true'
|
- 'str_true_to_bool is sameas true'
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
- assert:
|
- assert:
|
||||||
that:
|
that:
|
||||||
- 'string_sum == "12"'
|
- 'string_sum == "12"'
|
||||||
- 'string_sum|type_debug in ["string", "unicode"]'
|
- 'string_sum|type_debug in ["str", "unicode"]'
|
||||||
|
|
||||||
- name: add two lists
|
- name: add two lists
|
||||||
set_fact:
|
set_fact:
|
||||||
|
@ -40,7 +40,7 @@
|
||||||
|
|
||||||
- assert:
|
- assert:
|
||||||
that:
|
that:
|
||||||
- 'list_sum_multi|type_debug in ["string", "unicode"]'
|
- 'list_sum_multi|type_debug in ["str", "unicode"]'
|
||||||
|
|
||||||
- name: add two dicts
|
- name: add two dicts
|
||||||
set_fact:
|
set_fact:
|
||||||
|
@ -58,7 +58,7 @@
|
||||||
- assert:
|
- assert:
|
||||||
that:
|
that:
|
||||||
- 'list_for_strings == "onetwo"'
|
- 'list_for_strings == "onetwo"'
|
||||||
- 'list_for_strings|type_debug in ["string", "unicode"]'
|
- 'list_for_strings|type_debug in ["str", "unicode"]'
|
||||||
|
|
||||||
- name: loop through list with int
|
- name: loop through list with int
|
||||||
set_fact:
|
set_fact:
|
||||||
|
|
|
@ -20,4 +20,4 @@
|
||||||
|
|
||||||
- assert:
|
- assert:
|
||||||
that:
|
that:
|
||||||
- 'const_dunder|type_debug in ["string", "unicode"]'
|
- 'const_dunder|type_debug in ["str", "unicode"]'
|
||||||
|
|
11
test/integration/targets/jinja2_native_types/test_none.yml
Normal file
11
test/integration/targets/jinja2_native_types/test_none.yml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
- name: test none
|
||||||
|
set_fact:
|
||||||
|
none_var: "{{ yaml_none }}"
|
||||||
|
none_var_direct: "{{ None }}"
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- 'none_var is sameas none'
|
||||||
|
- 'none_var|type_debug == "NoneType"'
|
||||||
|
- 'none_var_direct is sameas none'
|
||||||
|
- 'none_var_direct|type_debug == "NoneType"'
|
Loading…
Reference in a new issue