Skip to content

Commit

Permalink
lookups, make file searching use better is_role (ansible#82290)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bcoca authored Dec 8, 2023
1 parent 5573d4c commit a9919dd
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/dwim_is_role_fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- dwim functions for lookups should be better at detectging role context even in abscense of tasks/main.
2 changes: 1 addition & 1 deletion lib/ansible/plugins/lookup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def find_file_in_search_path(self, myvars, subdir, needle, ignore_missing=False)

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)
Expand Down
8 changes: 5 additions & 3 deletions test/integration/targets/path_lookups/testplay.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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')}}"

Expand Down

0 comments on commit a9919dd

Please sign in to comment.