From ac8188febdd9843307df9763a3b533d0e39f0fa5 Mon Sep 17 00:00:00 2001 From: Klaus Frank <2544867+agowa@users.noreply.github.com> Date: Thu, 31 Aug 2023 21:58:45 +0200 Subject: [PATCH] first_found fix no terms option (#76550) (#81451) For backport without the fix for multiple items clobbering previous settings Co-authored-by: Sloane Hertel <19572925+s-hertel@users.noreply.github.com> --- changelogs/fragments/first_found_fixes.yml | 2 ++ lib/ansible/plugins/lookup/first_found.py | 3 +++ .../integration/targets/lookup_first_found/tasks/main.yml | 8 ++++++++ 3 files changed, 13 insertions(+) create mode 100644 changelogs/fragments/first_found_fixes.yml diff --git a/changelogs/fragments/first_found_fixes.yml b/changelogs/fragments/first_found_fixes.yml new file mode 100644 index 00000000000..0e7662d60d1 --- /dev/null +++ b/changelogs/fragments/first_found_fixes.yml @@ -0,0 +1,2 @@ +bugfixes: + - first found lookup has been updated to use the normalized argument parsing (pythonic) matching the documented examples. diff --git a/lib/ansible/plugins/lookup/first_found.py b/lib/ansible/plugins/lookup/first_found.py index 5b94b103a4e..649bc5c1096 100644 --- a/lib/ansible/plugins/lookup/first_found.py +++ b/lib/ansible/plugins/lookup/first_found.py @@ -165,6 +165,9 @@ class LookupModule(LookupBase): total_search = [] skip = False + if not terms and kwargs: + terms = [''] + # can use a dict instead of list item to pass inline config for term in terms: if isinstance(term, Mapping): diff --git a/test/integration/targets/lookup_first_found/tasks/main.yml b/test/integration/targets/lookup_first_found/tasks/main.yml index 9aeaf1d1ed5..d0ca91eaf54 100644 --- a/test/integration/targets/lookup_first_found/tasks/main.yml +++ b/test/integration/targets/lookup_first_found/tasks/main.yml @@ -94,3 +94,11 @@ - assert: that: - foo is defined + +# TODO: no 'terms' test +- name: test first_found lookup with no terms + set_fact: + no_terms: "{{ query('first_found', files=['missing1', 'hosts', 'missing2'], paths=['/etc'], errors='ignore') }}" + +- assert: + that: "no_terms|first == '/etc/hosts'"