Skip to content

Commit

Permalink
native types: literal_eval all the things (ansible#68938)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
mkrizek committed Apr 20, 2020
1 parent 6410570 commit 0a0ae0c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 19 deletions.
14 changes: 9 additions & 5 deletions lib/ansible/template/native_helpers.py
Expand Up @@ -13,6 +13,10 @@
from jinja2._compat import text_type

from jinja2.runtime import StrictUndefined

from ansible.module_utils._text import to_text
from ansible.module_utils.common.text.converters import container_to_text
from ansible.module_utils.six import PY2
from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicode


Expand Down Expand Up @@ -48,10 +52,6 @@ def ansible_native_concat(nodes):
# See https://github.com/ansible/ansible/issues/52158
# We do that only here because it is taken care of by text_type() in the else block below already.
str(out)

# short circuit literal_eval when possible
if not isinstance(out, list):
return out
else:
if isinstance(nodes, types.GeneratorType):
nodes = chain(head, nodes)
Expand All @@ -60,6 +60,10 @@ def ansible_native_concat(nodes):
out = u''.join([text_type(v) for v in nodes])

try:
return literal_eval(out)
out = literal_eval(out)
if PY2:
# ensure bytes are not returned back into Ansible from templating
out = container_to_text(out)
return out
except (ValueError, SyntaxError, MemoryError):
return out

This file was deleted.

6 changes: 3 additions & 3 deletions test/integration/targets/jinja2_native_types/test_casting.yml
@@ -1,9 +1,9 @@
- name: cast things to other things
set_fact:
int_to_str: "{{ i_two|to_text }}"
int_to_str: "'{{ i_two }}'"
str_to_int: "{{ s_two|int }}"
dict_to_str: "{{ dict_one|to_text }}"
list_to_str: "{{ list_one|to_text }}"
dict_to_str: "'{{ dict_one }}'"
list_to_str: "'{{ list_one }}'"
int_to_bool: "{{ i_one|bool }}"
str_true_to_bool: "{{ s_true|bool }}"
str_false_to_bool: "{{ s_false|bool }}"
Expand Down
Expand Up @@ -18,7 +18,7 @@

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

- assert:
that:
Expand Down
2 changes: 0 additions & 2 deletions test/sanity/ignore.txt
Expand Up @@ -5984,8 +5984,6 @@ test/integration/targets/inventory_kubevirt_conformance/inventory_diff.py future
test/integration/targets/inventory_kubevirt_conformance/inventory_diff.py metaclass-boilerplate
test/integration/targets/inventory_kubevirt_conformance/server.py future-import-boilerplate
test/integration/targets/inventory_kubevirt_conformance/server.py metaclass-boilerplate
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py future-import-boilerplate
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py metaclass-boilerplate
test/integration/targets/lambda_policy/files/mini_http_lambda.py future-import-boilerplate
test/integration/targets/lambda_policy/files/mini_http_lambda.py metaclass-boilerplate
test/integration/targets/lookup_properties/lookup-8859-15.ini no-smart-quotes
Expand Down

0 comments on commit 0a0ae0c

Please sign in to comment.