From c9ce7d08a256646abdaccc80b480b8b9c2df9f1b Mon Sep 17 00:00:00 2001 From: Joseph Torcasso <87090265+jatorcasso@users.noreply.github.com> Date: Tue, 17 May 2022 12:24:53 -0400 Subject: [PATCH] template/__init__.py - fix KeyError when wantlist=False with non-list --- .../fragments/77789-catch-keyerror-lookup-dict.yml | 2 ++ lib/ansible/template/__init__.py | 5 +++++ test/integration/targets/templating_lookups/runme.sh | 2 +- .../template_lookups/mock_lookup_plugins/77788.py | 6 ++++++ .../template_lookups/tasks/main.yml | 11 +++++++++++ 5 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/77789-catch-keyerror-lookup-dict.yml create mode 100644 test/integration/targets/templating_lookups/template_lookups/mock_lookup_plugins/77788.py diff --git a/changelogs/fragments/77789-catch-keyerror-lookup-dict.yml b/changelogs/fragments/77789-catch-keyerror-lookup-dict.yml new file mode 100644 index 00000000000..132d7417f7d --- /dev/null +++ b/changelogs/fragments/77789-catch-keyerror-lookup-dict.yml @@ -0,0 +1,2 @@ +bugfixes: + - lookup plugin - catch KeyError when lookup returns dictionary (https://github.com/ansible/ansible/pull/77789). diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 69433280305..a60ba270dcc 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -1014,6 +1014,11 @@ class Templar: else: ran = wrap_var(ran) + except KeyError: + # Lookup Plugin returned a dict. Return comma-separated string. + # See https://github.com/ansible/ansible/pull/77789 + ran = wrap_var(",".join(ran)) + return ran def _make_undefined(self, hint=None): diff --git a/test/integration/targets/templating_lookups/runme.sh b/test/integration/targets/templating_lookups/runme.sh index b900c153de7..60b3923b1a2 100755 --- a/test/integration/targets/templating_lookups/runme.sh +++ b/test/integration/targets/templating_lookups/runme.sh @@ -2,7 +2,7 @@ set -eux -ANSIBLE_ROLES_PATH=./ UNICODE_VAR=café ansible-playbook runme.yml "$@" +ANSIBLE_LOOKUP_PLUGINS=. ANSIBLE_ROLES_PATH=./ UNICODE_VAR=café ansible-playbook runme.yml "$@" ansible-playbook template_lookup_vaulted/playbook.yml --vault-password-file template_lookup_vaulted/test_vault_pass "$@" diff --git a/test/integration/targets/templating_lookups/template_lookups/mock_lookup_plugins/77788.py b/test/integration/targets/templating_lookups/template_lookups/mock_lookup_plugins/77788.py new file mode 100644 index 00000000000..436ceaf3916 --- /dev/null +++ b/test/integration/targets/templating_lookups/template_lookups/mock_lookup_plugins/77788.py @@ -0,0 +1,6 @@ +from ansible.plugins.lookup import LookupBase + + +class LookupModule(LookupBase): + def run(self, terms, variables, **kwargs): + return {'one': 1, 'two': 2} diff --git a/test/integration/targets/templating_lookups/template_lookups/tasks/main.yml b/test/integration/targets/templating_lookups/template_lookups/tasks/main.yml index f240a2340df..430ac917776 100644 --- a/test/integration/targets/templating_lookups/template_lookups/tasks/main.yml +++ b/test/integration/targets/templating_lookups/template_lookups/tasks/main.yml @@ -87,4 +87,15 @@ that: - password1 != password2 +# 77788 - KeyError when wantlist=False with dict returned +- name: Test that dicts can be parsed with wantlist false + set_fact: + dict_wantlist_true: "{{ lookup('77788', wantlist=True) }}" + dict_wantlist_false: "{{ lookup('77788', wantlist=False) }}" + +- assert: + that: + - dict_wantlist_true is mapping + - dict_wantlist_false is string + - include_tasks: ./errors.yml