From b23fdc3be34d990be113f65ee5d9377d6eea32c5 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Tue, 15 Nov 2022 08:47:51 +0100 Subject: [PATCH] dependent lookup: prevent deprecation warning with ansible-core 2.14 (#5543) (#5547) * Prevent deprecation warning. * Improve naming and add comment. (cherry picked from commit 60c8b9a67fe0788c770db69641ca6552beb2f522) Co-authored-by: Felix Fontein --- changelogs/fragments/5543-dependent-template.yml | 2 ++ plugins/lookup/dependent.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/5543-dependent-template.yml diff --git a/changelogs/fragments/5543-dependent-template.yml b/changelogs/fragments/5543-dependent-template.yml new file mode 100644 index 0000000000..63e8f67d63 --- /dev/null +++ b/changelogs/fragments/5543-dependent-template.yml @@ -0,0 +1,2 @@ +bugfixes: + - "dependent lookup plugin - avoid warning on deprecated parameter for ``Templar.template()`` (https://github.com/ansible-collections/community.general/pull/5543)." diff --git a/plugins/lookup/dependent.py b/plugins/lookup/dependent.py index 0bd234c1b4..16793e6402 100644 --- a/plugins/lookup/dependent.py +++ b/plugins/lookup/dependent.py @@ -125,8 +125,16 @@ from ansible.errors import AnsibleLookupError from ansible.module_utils.common._collections_compat import Mapping, Sequence from ansible.module_utils.six import string_types from ansible.plugins.lookup import LookupBase +from ansible.release import __version__ as ansible_version from ansible.template import Templar +from ansible_collections.community.general.plugins.module_utils.version import LooseVersion + + +# Whether Templar has a cache, which can be controlled by Templar.template()'s cache option. +# The cache was removed for ansible-core 2.14 (https://github.com/ansible/ansible/pull/78419) +_TEMPLAR_HAS_TEMPLATE_CACHE = LooseVersion(ansible_version) < LooseVersion('2.14.0') + class LookupModule(LookupBase): def __evaluate(self, expression, templar, variables): @@ -136,7 +144,10 @@ class LookupModule(LookupBase): ``variables`` are the variables to use. """ templar.available_variables = variables or {} - return templar.template("{0}{1}{2}".format("{{", expression, "}}"), cache=False) + expression = "{0}{1}{2}".format("{{", expression, "}}") + if _TEMPLAR_HAS_TEMPLATE_CACHE: + return templar.template(expression, cache=False) + return templar.template(expression) def __process(self, result, terms, index, current, templar, variables): """Fills ``result`` list with evaluated items.