From ad0ec47fe9aca20be91081b9a2cc2d4ff130cc42 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 22 Mar 2024 13:00:46 -0400 Subject: [PATCH] first_found lookup; minor fixes (#82836) Always process options no matter the combination return a full normalized path (symlinks still not followed, should be specific option in future) --- changelogs/fragments/first_found_fixes.yml | 3 +++ lib/ansible/plugins/lookup/first_found.py | 6 ++++-- test/integration/targets/lookup_first_found/tasks/main.yml | 5 +++++ 3 files changed, 12 insertions(+), 2 deletions(-) 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..a62c5c05dbb --- /dev/null +++ b/changelogs/fragments/first_found_fixes.yml @@ -0,0 +1,3 @@ +bugfixes: + - first_found lookup now always takes into account k=v options + - first_found lookup now always returns a full (absolute) and normalized path diff --git a/lib/ansible/plugins/lookup/first_found.py b/lib/ansible/plugins/lookup/first_found.py index 7b9b48fc613..4f522e36433 100644 --- a/lib/ansible/plugins/lookup/first_found.py +++ b/lib/ansible/plugins/lookup/first_found.py @@ -147,6 +147,7 @@ from jinja2.exceptions import UndefinedError from ansible.errors import AnsibleLookupError, AnsibleUndefinedVariable from ansible.module_utils.six import string_types from ansible.plugins.lookup import LookupBase +from ansible.utils.path import unfrackpath def _split_on(terms, spliters=','): @@ -206,8 +207,9 @@ class LookupModule(LookupBase): def run(self, terms, variables, **kwargs): + self.set_options(var_options=variables, direct=kwargs) + if not terms: - self.set_options(var_options=variables, direct=kwargs) terms = self.get_option('files') total_search, skip = self._process_terms(terms, variables, kwargs) @@ -234,7 +236,7 @@ class LookupModule(LookupBase): # exit if we find one! if path is not None: - return [path] + return [unfrackpath(path, follow=False)] # if we get here, no file was found if skip: diff --git a/test/integration/targets/lookup_first_found/tasks/main.yml b/test/integration/targets/lookup_first_found/tasks/main.yml index 7aa9742708b..174de6d6d75 100644 --- a/test/integration/targets/lookup_first_found/tasks/main.yml +++ b/test/integration/targets/lookup_first_found/tasks/main.yml @@ -147,3 +147,8 @@ - ishouldnotbefound.yml paths: - "{{role_path}}/vars" + +- name: Make sure skip works in 'mixed' argument passing + assert: + that: + - q('first_found', ['/nonexistant'], skip=True) == []