diff --git a/changelogs/fragments/jinja2-__version__-deprecated.yml b/changelogs/fragments/jinja2-__version__-deprecated.yml new file mode 100644 index 00000000000..3c84d7baee6 --- /dev/null +++ b/changelogs/fragments/jinja2-__version__-deprecated.yml @@ -0,0 +1,2 @@ +minor_changes: + - "Use ``importlib.metadata.version()`` to detect Jinja version as jinja2.__version__ is deprecated and will be removed in Jinja 3.3." diff --git a/lib/ansible/_internal/_templating/__init__.py b/lib/ansible/_internal/_templating/__init__.py index e2fd19558fc..0fe0a555d73 100644 --- a/lib/ansible/_internal/_templating/__init__.py +++ b/lib/ansible/_internal/_templating/__init__.py @@ -1,10 +1,12 @@ from __future__ import annotations -from jinja2 import __version__ as _jinja2_version +import importlib.metadata + +jinja2_version = importlib.metadata.version('jinja2') # DTFIX-FUTURE: sanity test to ensure this doesn't drift from requirements _MINIMUM_JINJA_VERSION = (3, 1) -_CURRENT_JINJA_VERSION = tuple(map(int, _jinja2_version.split('.', maxsplit=2)[:2])) +_CURRENT_JINJA_VERSION = tuple(map(int, jinja2_version.split('.', maxsplit=2)[:2])) if _CURRENT_JINJA_VERSION < _MINIMUM_JINJA_VERSION: - raise RuntimeError(f'Jinja version {".".join(map(str, _MINIMUM_JINJA_VERSION))} or higher is required (current version {_jinja2_version}).') + raise RuntimeError(f'Jinja version {".".join(map(str, _MINIMUM_JINJA_VERSION))} or higher is required (current version {jinja2_version}).') diff --git a/lib/ansible/cli/arguments/option_helpers.py b/lib/ansible/cli/arguments/option_helpers.py index c5bfdff903b..cb1762c2fc2 100644 --- a/lib/ansible/cli/arguments/option_helpers.py +++ b/lib/ansible/cli/arguments/option_helpers.py @@ -16,10 +16,9 @@ import typing as t import yaml -from jinja2 import __version__ as j2_version - import ansible from ansible import constants as C +from ansible._internal import _templating from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.common.yaml import HAS_LIBYAML, yaml_load from ansible.release import __version__ @@ -313,7 +312,7 @@ def version(prog=None): result.append(" ansible collection location = %s" % ':'.join(C.COLLECTIONS_PATHS)) result.append(" executable location = %s" % sys.argv[0]) result.append(" python version = %s (%s)" % (''.join(sys.version.splitlines()), to_native(sys.executable))) - result.append(" jinja version = %s" % j2_version) + result.append(f" jinja version = {_templating.jinja2_version}") result.append(f" pyyaml version = {yaml.__version__} ({libyaml_fragment})") return "\n".join(result)