Skip to content

Commit 84f76e1

Browse files
mkrizekbcoca
authored andcommitted
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.
1 parent cc85145 commit 84f76e1

File tree

5 files changed

+11
-19
lines changed

5 files changed

+11
-19
lines changed

lib/ansible/template/native_helpers.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from jinja2.runtime import StrictUndefined
1414

1515
from ansible.module_utils._text import to_text
16+
from ansible.module_utils.common.text.converters import container_to_text
17+
from ansible.module_utils.six import PY2
1618
from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicode
1719

1820

@@ -48,10 +50,6 @@ def ansible_native_concat(nodes):
4850
# See https://github.com/ansible/ansible/issues/52158
4951
# We do that only here because it is taken care of by to_text() in the else block below already.
5052
str(out)
51-
52-
# short circuit literal_eval when possible
53-
if not isinstance(out, list):
54-
return out
5553
else:
5654
if isinstance(nodes, types.GeneratorType):
5755
nodes = chain(head, nodes)
@@ -60,6 +58,10 @@ def ansible_native_concat(nodes):
6058
out = u''.join([to_text(v) for v in nodes])
6159

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

test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py

-8
This file was deleted.

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 }}"

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:

test/sanity/ignore.txt

-2
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,6 @@ test/integration/targets/incidental_win_dsc/files/xTestDsc/1.0.1/DSCResources/AN
286286
test/integration/targets/incidental_win_dsc/files/xTestDsc/1.0.1/xTestDsc.psd1 pslint!skip
287287
test/integration/targets/incidental_win_ping/library/win_ping_syntax_error.ps1 pslint!skip
288288
test/integration/targets/incidental_win_reboot/templates/post_reboot.ps1 pslint!skip
289-
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py future-import-boilerplate
290-
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py metaclass-boilerplate
291289
test/integration/targets/lookup_ini/lookup-8859-15.ini no-smart-quotes
292290
test/integration/targets/module_precedence/lib_with_extension/ping.py future-import-boilerplate
293291
test/integration/targets/module_precedence/lib_with_extension/ping.py metaclass-boilerplate

0 commit comments

Comments
 (0)