Commit Graph

273 Commits (devel)

Author SHA1 Message Date
Brian Coca 124d03145c
inspect components, ansible_managed templatable (#83053)
* inspect components, ansible_managed templatable

fixes #82322
1 month ago
Davide Sbetti 79ea21a39f
Fix error when templating an unsafe string leading to a type error in Python (#82675)
Fixes #82600
3 months ago
Abhijeet Kasurde 7a0c321054
Remove redundant undefined error message (#81867)
Fixes: #78703

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
4 months ago
Martin Krizek 6d34eb88d9
Do not ignore SyntaxError from jinja2.Environment.from_string (#82607)
Jinja may generate an invalid Python source code from a template. Trying
to compile such source code into a Python code object results in
SyntaxError being thrown. An example of such a template is providing the
same keyword argument into a lookup twice, resulting in:
`SyntaxError: keyword argument repeated`.

Since `jinja2.exceptions.TemplateSyntaxError` does not cover such a
case, as it is not a Jinja parsing error, we need to catch SyntaxError
explicitly ourselves.

Fixes #82606
4 months ago
Matt Clay 90203b841a Use functools.update_wrapper instead of a copy 7 months ago
Isaac Chung 5812cabaf5
Fix typos in builtin modules (#81969) 8 months ago
Matt Clay 9f899f9492
Require `from __future__ import annotations` (#81902) 8 months ago
Matt Davis dd79c49a4d
fix various Jinja plugin caching issues (#79781)
* fix various Jinja plugin caching issues

* consolidate the wrapper plugin cache
* remove redundant cache in J2 filter/test interceptor

* intra-template loader bypass

* fix early exits swallowing some exception detail

* misc comment cleanup
9 months ago
Nikita Korolev bd3ffbe109
fix incorrect ansible_managed formatting (#79129)
Ansible breaks configs if file name could be parsed as jinja template
or contains `%` chars.
Especially `%n`[ame%] which is a line break pattern according to [strftime (3)](https://manpages.debian.org/bullseye/manpages-dev/strftime.3.en.html)
10 months ago
Sloane Hertel 4c41562270
remove deprecated datetime.datetime methods (#81323)
* Remove datetime.datetime.utcfromtimestamp and datetime.datetime.uctnow
  from controller code since they are deprecated in Python 3.12.

* Update target side code to use new utcfromtimestamp and utcnow utils in ansible.module_utils.compat.datetime that return aware datetime objects on Python 2.7 and 3.

Co-authored-by: Matt Clay <matt@mystile.com>
10 months ago
Brian Coca 38067860e2
template escaping: use configured variable start instead of default one (#81001)
this will handle escaping correctly in tempaltes that override the default jinja variable tokens
also fix optimization for single var path when overrides happen
12 months ago
Matt Martz 73e04ef2d6
Don't mutate templar.environment, only overlay on local myenv (#81005) 12 months ago
Martin Krizek 932abc0711
Prevent setting arbitrary attrs on Jinja2 envs via overrides (#80715) 1 year ago
Martin Krizek 8cd95a8e66
Account for overlays when interacting with Jinja envs (#80705)
Instead of using Templar.environment in Templar.do_template for
accessing/mutating the environment, myenv local variable should be used
because it is the environment used for actual templating. It can either
point to Templar.environment or newly created environment overlay.

Fixes #80605
1 year ago
Matt Clay 2cd1744be3
Use ansible.module_utils.common.text.converters (#80704)
Replace use of old `ansible.module_utils._text` and add a unit test to maintain backwards compatibility.
1 year ago
Martin Krizek 1e8b889b93
Templar: remove deprecated shared_loader_obj param (#80439) 1 year ago
Martin Krizek 5ddd530d1d
Templar: do not add dict to globals (#80475)
All required Jinja2 versions (>=3.0) already do this, see
f9d804ead7
1 year ago
Martin Krizek 694f12d01b
Avoid redundant unsafe wrapping in ansible_eval_concat (#80143)
The wrap_var is done in do_template for all concat functions after their
return.
1 year ago
devon-mar 2f8f7fba4c
Update lookup to use positional only arg for name to avoid conflicts (#79839) 1 year ago
Matt Clay 27cd5e77d8
Update mypy and its dependencies. (#79812)
* Update mypy and its dependencies.

* Fix requirements.

* Remove unnecessary matching constraints.
1 year ago
Martin Krizek 60f76436c1
Simplify AnsibleJ2Vars by using ChainMap for vars (#78713)
Co-authored-by: Matt Martz <matt@sivel.net>
2 years ago
Martin Krizek d34b578685
jinja2_native: preserve quotes in strings (#79119)
Fixes #79083
2 years ago
Felix Fontein 6d0aeac1e1
Do not crash templating when filter/test name is not a valid Ansible plugin name (#78913)
* Do not crash templating when filter/test name is not a valid Ansible plugin name.
* Store and re-raise KeyError if there was one.

Co-authored-by: s-hertel <19572925+s-hertel@users.noreply.github.com>
2 years ago
Martin Krizek fcfdca8e7d
Overriding AnsibleContext.resolve is not needed/allowed (#78714)
https://github.com/pallets/jinja/pull/1393
https://github.com/pallets/jinja/pull/1544

ci_complete
2 years ago
Brian Coca 4260b71cc7
refactor and fixes for doc parsing (#77719)
* refactor and remove redundant code in documentation

  allow location and building api to be more accessible
  fix issues with displaying ansible.legacy and ansible.builtin
  ensure we don't x2 process tokens (some modules reference them also) fixes #77764
  move to constants vs hardcoded
  more informative errors and comments
  now have actual filter/test plugins, which expose the filter/test functions
  moved filter/test loading/finding logic into jinja2pluginloader, removed dupe implementations
  added tests for case in which we unique by basename when listing

Update lib/ansible/utils/plugin_docs.py
Co-authored-by: Sloane Hertel <19572925+s-hertel@users.noreply.github.com>
2 years ago
Martin Krizek 254de2a434
Templar: deprecate shared_loader_obj option (#78540) 2 years ago
Matt Davis 0591efd5e6
remove Templar caching (#78419)
* source of templating bugs (and at least one CVE), only applies to single-var values anyway
2 years ago
Matt Martz 884244f1b2
Python 3.9 min for controller (#77566) 2 years ago
Martin Krizek d070b03ad8
Fix templating nested vars with convert_data=False (#78273)
Regression introduced in #78259.
2 years ago
Martin Krizek 9afdb7fec1
template module/lookup: fix convert_data for macros (#78259)
Fixes #78141
2 years ago
Brian Coca 1f59bbf4f3
listify_lookup_plugin_terms deprecate dataloader (#78244)
* listify_lookup_plugin_terms deprecate dataloader

  deprecated useless dataloader pass to function
  also removed from callers in core


Co-authored-by: Sloane Hertel <19572925+s-hertel@users.noreply.github.com>
2 years ago
Martin Krizek 17d52c8d64
Move undefined check from concat to finalize (#78165)
* Move undefined check from concat to finalize

In the classic Jinja2's Environment str() is called on the return value of the
finalize method to potentially trigger the undefined error. That is not
the case in NativeEnvironment where string conversion of the return value is
not desired. We workaround that by checking for Undefined in all of our concat
functions. It seems simpler to do it earlier in the finalize method(s) instead.
As a side-effect it fixes an undefined variable detection in imported templates.

Fixes #78156

ci_complete

* Fix sanity

* ...

* sigh
2 years ago
Martin Krizek 39b3581316
Deprecate non lists lookups (#77875) 2 years ago
Joseph Torcasso c9ce7d08a2
template/__init__.py - fix KeyError when wantlist=False with non-list 2 years ago
Martin Krizek 3980eb8c09
Prevent losing unsafe from lookups (#77609)
* Prevent losing unsafe from lookups

This patch fixes a bug which under certain conditions results in data
returned from lookups not being marked as unsafe.

Each time Templar.do_template is invoked a new AnsibleContext is
created and stored effectively at two places:
1) as an instance variable in templar_obj.cur_context
2) as a local variable called new_context in do_template method of Templar

Due to custom functionality in Ansible's Context that allows for nested
templating it is possible that during resolving variable's value
template/do_template method is called recursively again, again creating
a new context. At that point the problem manifests itself because as
mentioned in 1) above the context is overwriten on the templar object
which means that any subsequent calls to _lookup will use the new
context to mark it as unsafe which is now different to the local
new_context which is used for testing for unsafe property.

The solution to the problem appears to be to restore the original
context inside do_template and also to eliminate the local variable
new_context to prevent problems in the future.

It appears that we don't have a better way of storing the context other
than as some form of global variable and so this appears to be the
"best" solution possible at this point. Hopefully data tagging will be
the solution here.

For more examples see unit and integration tests included in this patch.

Fixes #77535
2 years ago
Martin Krizek cbe42bff7f
Allow for lazy evaluation of Jinja2 expressions (#56116) 2 years ago
Corubba 94138cf608
template override colon bugfixes (#77495)
* Proper error on missing jinja2 override separator

Properly catch the case when no (or a wrong) separator is used in a
jinja2 override, and return a useful error message to the user.

* Support colons in jinja2 override value

By limiting the split to 1, any colons in the value are preserved and
passed on.
2 years ago
Martin Krizek c7e198b907
Ensure correct environment_class is set on Template (#77485) 2 years ago
Matt Clay f68c66a3ef Remove collections compat from controller code. 2 years ago
Sloane Hertel 8063643b4c
Fix collection redirects for filter and test plugins (#77210)
* Fix collection redirects for jinja2 filters/tests

* Handle recursive redirects

Co-authored-by: Matt Martz <matt@sivel.net>
2 years ago
Martin Krizek 8febd37f32
Attach concat func to an environment class (#76282)
* Attach concat func to an environment class

ci_complete

* clog and docstrings
2 years ago
Matt Martz 9f46f6eb30
Attempt 2: Catch errors getting filters and tests (#76806) 2 years ago
Martin Krizek 094a0746b3
ansible_concat: return strings only unless eval (#76634)
Fixes #76610
2 years ago
Martin Krizek 2058ea5991
Clarify unroll in _finalize (#76436)
Clarifies that unrolling in _finalize is actually necessary. Also using
decorator directly.
3 years ago
Martin Krizek 19a58859d6
AnsibleJ2Template should be based on NativeTemplate (#76471)
Fixes #76442

ci_complete
3 years ago
Martin Krizek 3fe42e106c
Remove finalize from globals (#76439) 3 years ago
Martin Krizek 4e7be293a5
Properly set finalize for the new environment (#76435)
* Properly set finalize for the new environment

Fixes #76379

* Fix sanity
3 years ago
Martin Krizek 382a353234
No longer needed to fall back to sha (#76281) 3 years ago
Martin Krizek 95e120ed02
Keep pre Python 3.10 literal_eval behavior (#76261) 3 years ago
Martin Krizek 26707a3c6b
Heisen jinja2_native (#75587)
* Use NativeEnvironment for all templating

ci_complete

* Keep Templar.copy_with_new_env for backwards compat

* Mention that AnsibleUndefined.__repr__ changed in the porting guide

* Templar.copy_with_new_env backwards compat

* ci_complete
3 years ago