Skip to content

Allow config to enable native jinja types#32738

Merged
mkrizek merged 29 commits intoansible:develfrom
jctanner:JINJA_NATIVE_TYPES
May 31, 2018
Merged

Allow config to enable native jinja types#32738
mkrizek merged 29 commits intoansible:develfrom
jctanner:JINJA_NATIVE_TYPES

Conversation

@jctanner
Copy link
Copy Markdown
Contributor

@jctanner jctanner commented Nov 9, 2017

SUMMARY

Allow config to enable native jinja types

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

templar

ANSIBLE VERSION
2.7
ADDITIONAL INFORMATION

Ties into the jinja 2.10 release feature http://jinja.pocoo.org/docs/2.10/nativetypes/

@jctanner
Copy link
Copy Markdown
Contributor Author

jctanner commented Nov 9, 2017

The initial patch here is pretty hacky, but feedback is very welcome.

@ansibot ansibot added affects_2.5 This issue/PR affects Ansible v2.5 feature_pull_request needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Nov 9, 2017
@jctanner jctanner changed the title Allow config to enable native jinja types WIP: Allow config to enable native jinja types Nov 9, 2017
@jctanner jctanner changed the title WIP: Allow config to enable native jinja types [WIP] Allow config to enable native jinja types Nov 9, 2017
@dagwieers
Copy link
Copy Markdown
Contributor

Is this something we would consider for v2.5 ?
And would it be configurable behaviour ?

@ansibot ansibot added the WIP This issue/PR is a work in progress. Nevertheless it was shared for getting input from peers. label Nov 9, 2017
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Nov 9, 2017
@jctanner
Copy link
Copy Markdown
Contributor Author

jctanner commented Nov 9, 2017

@dagwieers this is a message I got late last night ...

bcoca [12:47 AM]
@jctanner we need toggle for 'typing' ... off by default in 2.5, on by default in 2.6

The current patch does just that. Off by default.

Comment thread lib/ansible/template/__init__.py Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to do this here, AnsibleEnvironment inherits from Environment

Comment thread lib/ansible/template/__init__.py Outdated
Copy link
Copy Markdown
Member

@bcoca bcoca Nov 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move the previous:
from jinja2 import Environment
into an 'else/pass' here to make the choice obvious vs the 'masking' of the previous import

@bcoca bcoca added this to the 2.5.0 milestone Nov 9, 2017
@ansibot ansibot added the needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. label Nov 9, 2017
@ansibot ansibot removed the needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. label Nov 9, 2017
Comment thread lib/ansible/template/__init__.py Outdated
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't getattr(new_context, 'unsafe', False) be cleaner? (no idea about ansible code guidelines, just staw this PR when referenced in the related jinja PR

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, thanks

Copy link
Copy Markdown
Contributor

@cognifloyd cognifloyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you only swap out the Environment. What about the Template?

How does AnsibleJ2Template compare with NativeTemplate?

AnsibleJ2Template overrides new_context() but NativeTemplate overrides render(). Does Ansible not use render()?

Comment thread lib/ansible/template/__init__.py Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about doing the same with from jinja2.utils import concat as j2_concat?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I think you can drop line 41 too.

@ansible ansible deleted a comment from ansibot Nov 10, 2017
@abadger
Copy link
Copy Markdown
Contributor

abadger commented Nov 10, 2017

This passed in shippable. The UNSTABLE status is because of an unrelated, transient failure in the opensuse repository for the zypper test. The second test run was fine.

@ansibot ansibot added plugins/action test This PR relates to tests. labels Nov 15, 2017
@ansibot
Copy link
Copy Markdown
Contributor

ansibot commented Nov 15, 2017

The test ansible-test sanity --test boilerplate [?] failed with the following error:

Command "test/sanity/code-smell/boilerplate.sh" returned exit status 2.
>>> Standard Output
== Missing __metaclass__ = type ==
./lib/ansible/template/native_helpers.py

== Missing from __future__ import (absolute_import, division, print_function) ==
./lib/ansible/template/native_helpers.py

The test ansible-test sanity --test pep8 [?] failed with the following errors:

lib/ansible/plugins/action/debug.py:53:17: E265 block comment should start with '# '
lib/ansible/template/native_helpers.py:32:5: E265 block comment should start with '# '
lib/ansible/template/native_helpers.py:33:5: E265 block comment should start with '# '
lib/ansible/template/native_helpers.py:34:5: E265 block comment should start with '# '
lib/ansible/template/native_helpers.py:38:9: E265 block comment should start with '# '
lib/ansible/template/native_helpers.py:42:9: E265 block comment should start with '# '
lib/ansible/template/native_helpers.py:43:9: E265 block comment should start with '# '
lib/ansible/template/native_helpers.py:44:9: E265 block comment should start with '# '
lib/ansible/template/native_helpers.py:45:9: E265 block comment should start with '# '
lib/ansible/template/native_helpers.py:46:9: E265 block comment should start with '# '
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py:1:1: E265 block comment should start with '# '
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py:5:1: E302 expected 2 blank lines, found 1
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py:11:1: E302 expected 2 blank lines, found 1

click here for bot help

@ansibot
Copy link
Copy Markdown
Contributor

ansibot commented Nov 15, 2017

The test ansible-test sanity --test boilerplate [?] failed with the following error:

Command "test/sanity/code-smell/boilerplate.sh" returned exit status 2.
>>> Standard Output
== Missing __metaclass__ = type ==
./lib/ansible/template/native_helpers.py

== Missing from __future__ import (absolute_import, division, print_function) ==
./lib/ansible/template/native_helpers.py

The test ansible-test sanity --test pep8 [?] failed with the following errors:

test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py:1:1: E265 block comment should start with '# '
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py:5:1: E302 expected 2 blank lines, found 1
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py:11:1: E302 expected 2 blank lines, found 1

click here for bot help

@ansibot
Copy link
Copy Markdown
Contributor

ansibot commented Nov 15, 2017

The test ansible-test sanity --test pep8 [?] failed with the following errors:

test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py:1:1: E265 block comment should start with '# '
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py:5:1: E302 expected 2 blank lines, found 1
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py:11:1: E302 expected 2 blank lines, found 1

click here for bot help

@ansibot
Copy link
Copy Markdown
Contributor

ansibot commented Nov 15, 2017

The test ansible-test sanity --test pep8 [?] failed with the following errors:

test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py:1:1: E265 block comment should start with '# '
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py:5:1: E302 expected 2 blank lines, found 1
test/integration/targets/jinja2_native_types/filter_plugins/native_plugins.py:11:1: E302 expected 2 blank lines, found 1

click here for bot help

@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Nov 23, 2017
@mkrizek mkrizek merged commit a9e53cd into ansible:devel May 31, 2018
jacum pushed a commit to jacum/ansible that referenced this pull request Jun 26, 2018
Co-authored-by: Martin Krizek <martin.krizek@gmail.com>
@rdtechie
Copy link
Copy Markdown
Contributor

Forgive me my ignorance. But should this now be solved in devel? I'm using now the devel source from yesterday, but I still cannot get out proper integers. I'm using this playbook to test: https://gist.githubusercontent.com/jctanner/32df50fe270096afc7fe2582e2d21b8c/raw/6fbe189830de4b09d0e322e481fd458031674809/site.yml

@abadger
Copy link
Copy Markdown
Contributor

abadger commented Jul 24, 2018

Did you set the config setting to true?

@rdtechie
Copy link
Copy Markdown
Contributor

@abadger Nope! Works now. 👍 Thanks for the heads up!

@jmehnle
Copy link
Copy Markdown

jmehnle commented Jul 24, 2018

For posterity, the ansible.cfg config setting is jinja2_native in the defaults section.

cdosborn added a commit to cdosborn/clank that referenced this pull request Sep 6, 2018
Ansible cannot template numbers.
```
TACC_API_TIMEOUT:  "{{ TACC_API_TIMEOUT }}"
```
Will result in a yaml string. Atmosphere expects this variable
to be a yaml number. Ansible fixed this issue in 2.7
ansible/ansible#32738 .

Until this is fixed, it is hardcoded.
cdosborn added a commit to cdosborn/clank that referenced this pull request Sep 6, 2018
Ansible cannot template numbers.
```
TACC_API_TIMEOUT:  "{{ TACC_API_TIMEOUT }}"
```
Will result in a yaml string. Atmosphere expects this variable
to be a yaml number. Ansible fixed this issue in 2.7
ansible/ansible#32738 .

Until this is fixed, it is hardcoded.
@tobywan
Copy link
Copy Markdown

tobywan commented Feb 12, 2019

Alternatively to the config file,
export ANSIBLE_JINJA2_NATIVE=true

@ansible ansible locked and limited conversation to collaborators May 31, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

affects_2.5 This issue/PR affects Ansible v2.5 feature This issue/PR relates to a feature request. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. plugins/action support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. test This PR relates to tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.