This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
forked from ansible/ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow config to enable native jinja types (ansible#32738)
Co-authored-by: Martin Krizek <[email protected]>
- Loading branch information
Showing
13 changed files
with
364 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright: (c) 2018, Ansible Project | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# Make coding more python3-ish | ||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
|
||
from ast import literal_eval | ||
from itertools import islice, chain | ||
import types | ||
|
||
from jinja2._compat import text_type | ||
|
||
|
||
def ansible_native_concat(nodes): | ||
"""Return a native Python type from the list of compiled nodes. If the | ||
result is a single node, its value is returned. Otherwise, the nodes are | ||
concatenated as strings. If the result can be parsed with | ||
:func:`ast.literal_eval`, the parsed value is returned. Otherwise, the | ||
string is returned. | ||
""" | ||
|
||
# https://github.com/pallets/jinja/blob/master/jinja2/nativetypes.py | ||
|
||
head = list(islice(nodes, 2)) | ||
|
||
if not head: | ||
return None | ||
|
||
if len(head) == 1: | ||
out = head[0] | ||
# short circuit literal_eval when possible | ||
if not isinstance(out, list): # FIXME is this needed? | ||
return out | ||
else: | ||
if isinstance(nodes, types.GeneratorType): | ||
nodes = chain(head, nodes) | ||
out = u''.join([text_type(v) for v in nodes]) | ||
|
||
try: | ||
return literal_eval(out) | ||
except (ValueError, SyntaxError, MemoryError): | ||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
posix/ci/group3 |
8 changes: 8 additions & 0 deletions
8
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from ansible.module_utils._text import to_text | ||
|
||
|
||
class FilterModule(object): | ||
def filters(self): | ||
return { | ||
'to_text': to_text, | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -eux | ||
|
||
ANSIBLE_JINJA2_NATIVE=1 ansible-playbook -i inventory.jinja2_native_types runtests.yml -v "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
- name: Test jinja2 native types | ||
hosts: localhost | ||
gather_facts: no | ||
vars: | ||
i_one: 1 | ||
i_two: 2 | ||
i_three: 3 | ||
s_one: "1" | ||
s_two: "2" | ||
s_three: "3" | ||
dict_one: | ||
foo: bar | ||
baz: bang | ||
dict_two: | ||
bar: foo | ||
foobar: barfoo | ||
list_one: | ||
- one | ||
- two | ||
list_two: | ||
- three | ||
- four | ||
list_ints: | ||
- 4 | ||
- 2 | ||
list_one_int: | ||
- 1 | ||
b_true: True | ||
b_false: False | ||
s_true: "True" | ||
s_false: "False" | ||
tasks: | ||
- name: check jinja version | ||
shell: python -c 'import jinja2; print(jinja2.__version__)' | ||
register: jinja2_version | ||
|
||
- name: make sure jinja is the right version | ||
set_fact: | ||
is_native: "{{ jinja2_version.stdout is version('2.10', '>=') }}" | ||
|
||
- block: | ||
- import_tasks: test_casting.yml | ||
- import_tasks: test_concatentation.yml | ||
- import_tasks: test_bool.yml | ||
- import_tasks: test_dunder.yml | ||
- import_tasks: test_types.yml | ||
when: is_native |
53 changes: 53 additions & 0 deletions
53
test/integration/targets/jinja2_native_types/test_bool.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
- name: test bool True | ||
set_fact: | ||
bool_var_true: "{{ b_true }}" | ||
|
||
- assert: | ||
that: | ||
- 'bool_var_true is sameas true' | ||
- 'bool_var_true|type_debug == "bool"' | ||
|
||
- name: test bool False | ||
set_fact: | ||
bool_var_false: "{{ b_false }}" | ||
|
||
- assert: | ||
that: | ||
- 'bool_var_false is sameas false' | ||
- 'bool_var_false|type_debug == "bool"' | ||
|
||
- name: test bool expr True | ||
set_fact: | ||
bool_var_expr_true: "{{ 1 == 1 }}" | ||
|
||
- assert: | ||
that: | ||
- 'bool_var_expr_true is sameas true' | ||
- 'bool_var_expr_true|type_debug == "bool"' | ||
|
||
- name: test bool expr False | ||
set_fact: | ||
bool_var_expr_false: "{{ 2 + 2 == 5 }}" | ||
|
||
- assert: | ||
that: | ||
- 'bool_var_expr_false is sameas false' | ||
- 'bool_var_expr_false|type_debug == "bool"' | ||
|
||
- name: test bool expr with None, True | ||
set_fact: | ||
bool_var_none_expr_true: "{{ None == None }}" | ||
|
||
- assert: | ||
that: | ||
- 'bool_var_none_expr_true is sameas true' | ||
- 'bool_var_none_expr_true|type_debug == "bool"' | ||
|
||
- name: test bool expr with None, False | ||
set_fact: | ||
bool_var_none_expr_false: "{{ '' == None }}" | ||
|
||
- assert: | ||
that: | ||
- 'bool_var_none_expr_false is sameas false' | ||
- 'bool_var_none_expr_false|type_debug == "bool"' |
24 changes: 24 additions & 0 deletions
24
test/integration/targets/jinja2_native_types/test_casting.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
- name: cast things to other things | ||
set_fact: | ||
int_to_str: "{{ i_two|to_text }}" | ||
str_to_int: "{{ s_two|int }}" | ||
dict_to_str: "{{ dict_one|to_text }}" | ||
list_to_str: "{{ list_one|to_text }}" | ||
int_to_bool: "{{ i_one|bool }}" | ||
str_true_to_bool: "{{ s_true|bool }}" | ||
str_false_to_bool: "{{ s_false|bool }}" | ||
|
||
- assert: | ||
that: | ||
- 'int_to_str == "2"' | ||
- 'int_to_str|type_debug in ["string", "unicode"]' | ||
- 'str_to_int == 2' | ||
- 'str_to_int|type_debug == "int"' | ||
- 'dict_to_str|type_debug in ["string", "unicode"]' | ||
- 'list_to_str|type_debug in ["string", "unicode"]' | ||
- 'int_to_bool is sameas true' | ||
- 'int_to_bool|type_debug == "bool"' | ||
- 'str_true_to_bool is sameas true' | ||
- 'str_true_to_bool|type_debug == "bool"' | ||
- 'str_false_to_bool is sameas false' | ||
- 'str_false_to_bool|type_debug == "bool"' |
Oops, something went wrong.