Skip to content

Commit bd83348

Browse files
committed
native types: literal_eval all the things (ansible#68938)
With pallets/jinja#1190 merged our short-circuit is no longer valid (has it ever been?) as now data like ' True ' may go through our ansible_native_concat function as opposed to going through intermediate call to Jinja2's native_concat before. Now we need to always send data through literal_eval to ensure native types are returned. (cherry picked from commit acdc9eb)
1 parent 6410570 commit bd83348

File tree

5 files changed

+12
-19
lines changed

5 files changed

+12
-19
lines changed

Diff for: lib/ansible/template/native_helpers.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
from jinja2._compat import text_type
1414

1515
from jinja2.runtime import StrictUndefined
16+
17+
from ansible.module_utils.common.text.converters import container_to_text
18+
from ansible.module_utils.six import PY2
1619
from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicode
1720

1821

@@ -48,10 +51,6 @@ def ansible_native_concat(nodes):
4851
# See https://github.com/ansible/ansible/issues/52158
4952
# We do that only here because it is taken care of by text_type() in the else block below already.
5053
str(out)
51-
52-
# short circuit literal_eval when possible
53-
if not isinstance(out, list):
54-
return out
5554
else:
5655
if isinstance(nodes, types.GeneratorType):
5756
nodes = chain(head, nodes)
@@ -60,6 +59,10 @@ def ansible_native_concat(nodes):
6059
out = u''.join([text_type(v) for v in nodes])
6160

6261
try:
63-
return literal_eval(out)
62+
out = literal_eval(out)
63+
if PY2:
64+
# ensure bytes are not returned back into Ansible from templating
65+
out = container_to_text(out)
66+
return out
6467
except (ValueError, SyntaxError, MemoryError):
6568
return out

Diff for: test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py

-8
This file was deleted.

Diff for: test/integration/targets/jinja2_native_types/test_casting.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
- name: cast things to other things
22
set_fact:
3-
int_to_str: "{{ i_two|to_text }}"
3+
int_to_str: "'{{ i_two }}'"
44
str_to_int: "{{ s_two|int }}"
5-
dict_to_str: "{{ dict_one|to_text }}"
6-
list_to_str: "{{ list_one|to_text }}"
5+
dict_to_str: "'{{ dict_one }}'"
6+
list_to_str: "'{{ list_one }}'"
77
int_to_bool: "{{ i_one|bool }}"
88
str_true_to_bool: "{{ s_true|bool }}"
99
str_false_to_bool: "{{ s_false|bool }}"

Diff for: test/integration/targets/jinja2_native_types/test_concatentation.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
- name: concatenate int and string
2020
set_fact:
21-
string_sum: "{{ [(i_one|to_text), s_two]|join('') }}"
21+
string_sum: "'{{ [i_one, s_two]|join('') }}'"
2222

2323
- assert:
2424
that:

Diff for: test/sanity/ignore.txt

-2
Original file line numberDiff line numberDiff line change
@@ -5984,8 +5984,6 @@ test/integration/targets/inventory_kubevirt_conformance/inventory_diff.py future
59845984
test/integration/targets/inventory_kubevirt_conformance/inventory_diff.py metaclass-boilerplate
59855985
test/integration/targets/inventory_kubevirt_conformance/server.py future-import-boilerplate
59865986
test/integration/targets/inventory_kubevirt_conformance/server.py metaclass-boilerplate
5987-
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py future-import-boilerplate
5988-
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py metaclass-boilerplate
59895987
test/integration/targets/lambda_policy/files/mini_http_lambda.py future-import-boilerplate
59905988
test/integration/targets/lambda_policy/files/mini_http_lambda.py metaclass-boilerplate
59915989
test/integration/targets/lookup_properties/lookup-8859-15.ini no-smart-quotes

0 commit comments

Comments
 (0)