Created
June 1, 2018 12:52
-
-
Save mkrizek/cbcd450ec5dbd4c78a37c73667159cb3 to your computer and use it in GitHub Desktop.
Native Jinja2 Types
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
# Upstream Jinja2 native types docs: http://jinja.pocoo.org/docs/2.10/nativetypes/ | |
# Upstream Jinja2 native types PR: https://github.com/pallets/jinja/pull/708 | |
# Jinja2 native types in Ansible PR: https://github.com/ansible/ansible/pull/32738 | |
# ansible-devel@ announcement: https://groups.google.com/forum/#!topic/ansible-devel/o42OoC0VZ4A | |
# Preserving types of castings | |
- hosts: localhost | |
gather_facts: no | |
vars: | |
adict: | |
str_value: "42" | |
tasks: | |
- debug: | |
msg: "{{ adict.str_value|int }}" | |
# $ ansible-playbook ../ansible-repro/jinja-native.yml | |
# PLAY [localhost] ************************************************************* | |
# | |
# TASK [debug] ***************************************************************** | |
# ok: [localhost] => { | |
# "msg": "42" | |
# } | |
# $ ANSIBLE_JINJA2_NATIVE=1 ansible-playbook ../ansible-repro/jinja-native.yml | |
# PLAY [localhost] ************************************************************ | |
# | |
# TASK [debug] **************************************************************** | |
# ok: [localhost] => { | |
# "msg": 42 | |
# } | |
# Preserving return types of filters | |
- hosts: localhost | |
gather_facts: no | |
vars: | |
x: 2 | |
tasks: | |
- debug: | |
msg: "{{ x | pow(2) }}" | |
# ansible-playbook ../ansible-repro/jinja-native.yml | |
# PLAY [localhost] ************************************************************* | |
# | |
# TASK [debug] ***************************************************************** | |
# ok: [localhost] => { | |
# "msg": "4.0" | |
# } | |
# | |
# ANSIBLE_JINJA2_NATIVE=1 ansible-playbook ../ansible-repro/jinja-native.yml | |
# PLAY [localhost] ************************************************************* | |
# | |
# TASK [debug] ***************************************************************** | |
# ok: [localhost] => { | |
# "msg": 4.0 | |
# } | |
# Preserving return types of operations | |
- hosts: localhost | |
gather_facts: no | |
vars: | |
adict: | |
str_value: "40" | |
tasks: | |
- set_fact: | |
result: "{{ adict.str_value|int + 2 }}" | |
- debug: | |
var: result | |
- debug: | |
msg: "{{ result|type_debug }}" | |
# $ ansible-playbook jinja-native.yml | |
# PLAY [localhost] ************************************************************* | |
# | |
# TASK [set_fact] ************************************************************** | |
# ok: [localhost] | |
# | |
# TASK [debug] ***************************************************************** | |
# ok: [localhost] => { | |
# "result": "42" | |
# } | |
# | |
# TASK [debug] ***************************************************************** | |
# ok: [localhost] => { | |
# "msg": "unicode" | |
# } | |
# $ ANSIBLE_JINJA2_NATIVE=1 ansible-playbook jinja-native.yml | |
# PLAY [localhost] ************************************************************* | |
# | |
# TASK [set_fact] ************************************************************** | |
# ok: [localhost] | |
# | |
# TASK [debug] ***************************************************************** | |
# ok: [localhost] => { | |
# "result": 42 | |
# } | |
# | |
# TASK [debug] ***************************************************************** | |
# ok: [localhost] => { | |
# "msg": "int" | |
# } | |
- hosts: localhost | |
gather_facts: no | |
vars: | |
my_var: 1 | |
tasks: | |
- copy: | |
dest: /tmp/output | |
content: | | |
{ | |
"key1": "{{ my_var }}", | |
"key2": "2", | |
"key3": "3", | |
"key4": "4", | |
"key5": "5", | |
} | |
# content of /tmp/output without ANSIBLE_JINJA2_NATIVE set | |
# {"key3": "3", "key2": "2", "key1": "1", "key5": "5", "key4": "4"} | |
# | |
# content of /tmp/output with ANSIBLE_JINJA2_NATIVE set | |
# { | |
# "key1": "1", | |
# "key2": "2", | |
# "key3": "3", | |
# "key4": "4", | |
# "key5": "5", | |
# } | |
# | |
# The format is preserved. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment