mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 13:20:23 -07:00
Allow config to enable native jinja types (#32738)
Co-authored-by: Martin Krizek <martin.krizek@gmail.com>
This commit is contained in:
parent
81510970ae
commit
a9e53cdb68
13 changed files with 364 additions and 20 deletions
1
test/integration/targets/jinja2_native_types/aliases
Normal file
1
test/integration/targets/jinja2_native_types/aliases
Normal file
|
@ -0,0 +1 @@
|
|||
posix/ci/group3
|
|
@ -0,0 +1,8 @@
|
|||
from ansible.module_utils._text import to_text
|
||||
|
||||
|
||||
class FilterModule(object):
|
||||
def filters(self):
|
||||
return {
|
||||
'to_text': to_text,
|
||||
}
|
5
test/integration/targets/jinja2_native_types/runme.sh
Executable file
5
test/integration/targets/jinja2_native_types/runme.sh
Executable file
|
@ -0,0 +1,5 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -eux
|
||||
|
||||
ANSIBLE_JINJA2_NATIVE=1 ansible-playbook -i inventory.jinja2_native_types runtests.yml -v "$@"
|
47
test/integration/targets/jinja2_native_types/runtests.yml
Normal file
47
test/integration/targets/jinja2_native_types/runtests.yml
Normal file
|
@ -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
test/integration/targets/jinja2_native_types/test_bool.yml
Normal file
53
test/integration/targets/jinja2_native_types/test_bool.yml
Normal file
|
@ -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"'
|
|
@ -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"'
|
|
@ -0,0 +1,88 @@
|
|||
- name: add two ints
|
||||
set_fact:
|
||||
integer_sum: "{{ i_one + i_two }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'integer_sum == 3'
|
||||
- 'integer_sum|type_debug == "int"'
|
||||
|
||||
- name: add casted string and int
|
||||
set_fact:
|
||||
integer_sum2: "{{ s_one|int + i_two }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'integer_sum2 == 3'
|
||||
- 'integer_sum2|type_debug == "int"'
|
||||
|
||||
- name: concatenate int and string
|
||||
set_fact:
|
||||
string_sum: "{{ [(i_one|to_text), s_two]|join('') }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'string_sum == "12"'
|
||||
- 'string_sum|type_debug in ["string", "unicode"]'
|
||||
|
||||
- name: add two lists
|
||||
set_fact:
|
||||
list_sum: "{{ list_one + list_two }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'list_sum == ["one", "two", "three", "four"]'
|
||||
- 'list_sum|type_debug == "list"'
|
||||
|
||||
- name: add two lists, multi expression
|
||||
set_fact:
|
||||
list_sum_multi: "{{ list_one }} + {{ list_two }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'list_sum_multi|type_debug in ["string", "unicode"]'
|
||||
|
||||
- name: add two dicts
|
||||
set_fact:
|
||||
dict_sum: "{{ dict_one + dict_two }}"
|
||||
ignore_errors: yes
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'dict_sum is undefined'
|
||||
|
||||
- name: loop through list with strings
|
||||
set_fact:
|
||||
list_for_strings: "{% for x in list_one %}{{ x }}{% endfor %}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'list_for_strings == "onetwo"'
|
||||
- 'list_for_strings|type_debug in ["string", "unicode"]'
|
||||
|
||||
- name: loop through list with int
|
||||
set_fact:
|
||||
list_for_int: "{% for x in list_one_int %}{{ x }}{% endfor %}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'list_for_int == 1'
|
||||
- 'list_for_int|type_debug == "int"'
|
||||
|
||||
- name: loop through list with ints
|
||||
set_fact:
|
||||
list_for_ints: "{% for x in list_ints %}{{ x }}{% endfor %}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'list_for_ints == 42'
|
||||
- 'list_for_ints|type_debug == "int"'
|
||||
|
||||
- name: loop through list to create a new list
|
||||
set_fact:
|
||||
list_from_list: "[{% for x in list_ints %}{{ x }},{% endfor %}]"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'list_from_list == [4, 2]'
|
||||
- 'list_from_list|type_debug == "list"'
|
23
test/integration/targets/jinja2_native_types/test_dunder.yml
Normal file
23
test/integration/targets/jinja2_native_types/test_dunder.yml
Normal file
|
@ -0,0 +1,23 @@
|
|||
- name: test variable dunder
|
||||
set_fact:
|
||||
var_dunder: "{{ b_true.__class__ }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'var_dunder|type_debug == "type"'
|
||||
|
||||
- name: test constant dunder
|
||||
set_fact:
|
||||
const_dunder: "{{ true.__class__ }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'const_dunder|type_debug == "type"'
|
||||
|
||||
- name: test constant dunder to string
|
||||
set_fact:
|
||||
const_dunder: "{{ true.__class__|string }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'const_dunder|type_debug in ["string", "unicode"]'
|
20
test/integration/targets/jinja2_native_types/test_types.yml
Normal file
20
test/integration/targets/jinja2_native_types/test_types.yml
Normal file
|
@ -0,0 +1,20 @@
|
|||
- assert:
|
||||
that:
|
||||
- 'i_one|type_debug == "int"'
|
||||
- 's_one|type_debug == "AnsibleUnicode"'
|
||||
- 'dict_one|type_debug == "dict"'
|
||||
- 'dict_one is mapping'
|
||||
- 'list_one|type_debug == "list"'
|
||||
- 'b_true|type_debug == "bool"'
|
||||
- 's_true|type_debug == "AnsibleUnicode"'
|
||||
|
||||
- set_fact:
|
||||
a_list: "{{[i_one, s_two]}}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- 'a_list|type_debug == "list"'
|
||||
- 'a_list[0] == 1'
|
||||
- 'a_list[0]|type_debug == "int"'
|
||||
- 'a_list[1] == "2"'
|
||||
- 'a_list[1]|type_debug == "AnsibleUnicode"'
|
Loading…
Add table
Add a link
Reference in a new issue