From a9919dd7f62c9efe17b8acaebf7c627606ae9f66 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 8 Dec 2023 11:05:40 -0500 Subject: [PATCH] lookups, make file searching use better is_role (#82290) * lookups, make file searching use better is_role The dwim function will internally try by detecting tasks/main['','.yml','.yaml] but this is far from optimial, the existince of role path in vars is much better indicator that we can use to pass a hint * updated test to avoid main.yml --- changelogs/fragments/dwim_is_role_fix.yml | 2 ++ lib/ansible/plugins/lookup/__init__.py | 2 +- .../roles/showfile/tasks/{main.yml => notmain.yml} | 0 test/integration/targets/path_lookups/testplay.yml | 8 +++++--- 4 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/dwim_is_role_fix.yml rename test/integration/targets/path_lookups/roles/showfile/tasks/{main.yml => notmain.yml} (100%) diff --git a/changelogs/fragments/dwim_is_role_fix.yml b/changelogs/fragments/dwim_is_role_fix.yml new file mode 100644 index 00000000000..bab02012ff8 --- /dev/null +++ b/changelogs/fragments/dwim_is_role_fix.yml @@ -0,0 +1,2 @@ +bugfixes: + - dwim functions for lookups should be better at detectging role context even in abscense of tasks/main. diff --git a/lib/ansible/plugins/lookup/__init__.py b/lib/ansible/plugins/lookup/__init__.py index 22e73032c85..bc15943ef5c 100644 --- a/lib/ansible/plugins/lookup/__init__.py +++ b/lib/ansible/plugins/lookup/__init__.py @@ -115,7 +115,7 @@ class LookupBase(AnsiblePlugin): result = None try: - result = self._loader.path_dwim_relative_stack(paths, subdir, needle) + result = self._loader.path_dwim_relative_stack(paths, subdir, needle, is_role=bool('role_path' in myvars)) except AnsibleFileNotFound: if not ignore_missing: self._display.warning("Unable to find '%s' in expected paths (use -vvvvv to see paths)" % needle) diff --git a/test/integration/targets/path_lookups/roles/showfile/tasks/main.yml b/test/integration/targets/path_lookups/roles/showfile/tasks/notmain.yml similarity index 100% rename from test/integration/targets/path_lookups/roles/showfile/tasks/main.yml rename to test/integration/targets/path_lookups/roles/showfile/tasks/notmain.yml diff --git a/test/integration/targets/path_lookups/testplay.yml b/test/integration/targets/path_lookups/testplay.yml index 8bf4553275d..bc05c7e531f 100644 --- a/test/integration/targets/path_lookups/testplay.yml +++ b/test/integration/targets/path_lookups/testplay.yml @@ -4,9 +4,11 @@ pre_tasks: - name: remove {{ remove }} file: path={{ playbook_dir }}/{{ remove }} state=absent - roles: - - showfile - post_tasks: + tasks: + - import_role: + name: showfile + tasks_from: notmain.yml + - name: from play set_fact: play_result="{{lookup('file', 'testfile')}}"