From dca37091a31984473fb04ee8b31f6afa85ac6aa7 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Tue, 21 Nov 2023 12:56:57 -0600 Subject: [PATCH] Add toggle --- lib/ansible/config/base.yml | 14 ++++++++++++++ lib/ansible/template/__init__.py | 3 +++ 2 files changed, 17 insertions(+) diff --git a/lib/ansible/config/base.yml b/lib/ansible/config/base.yml index a6130b58bba..3a75d796c28 100644 --- a/lib/ansible/config/base.yml +++ b/lib/ansible/config/base.yml @@ -764,6 +764,20 @@ DEFAULT_JINJA2_NATIVE: type: boolean yaml: {key: jinja2_native} version_added: 2.7 +JINJA2_BYTECODE_CACHE: + name: Cache the jinja2 bytecode for compiled templates + description: + - This option, which is enabled by default, will cache the jinja2 generated + bytecode for templates reducing execution time, specifically for template + heavy playbooks. Disabling this will prevent the bytecode cache from + being used. + env: + - {name: ANSIBLE_JINJA2_BYTECODE_CACHE} + ini: + - {key: jinja2_bytecode_cache, section: defaults} + type: boolean + default: True + version_added: '2.17' DEFAULT_KEEP_REMOTE_FILES: name: Keep remote files default: False diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index cffcc2db9c6..919a4e85980 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -583,6 +583,9 @@ class AnsibleEnvironment(NativeEnvironment): template_class: t.Type[Template] | None = None, ) -> Template: + if not C.JINJA2_BYTECODE_CACHE: + return super().from_string(source, globals=globals, template_class=template_class) + cache_dir = os.path.join(C.DEFAULT_LOCAL_TMP, 'j2cache') if not os.path.isdir(cache_dir): os.makedirs(cache_dir, mode=0o700, exist_ok=True)