From 0be66ed6dcb9f499488df5c93168a8400b43cdac Mon Sep 17 00:00:00 2001 From: Sloane Hertel <19572925+s-hertel@users.noreply.github.com> Date: Tue, 6 Aug 2024 17:53:23 -0400 Subject: [PATCH] Fix task-adjacent search path in roles (#83621) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Restore search path in the current task file’s directory for roles --- changelogs/fragments/dwim_is_role_fix_task_relative.yml | 2 ++ lib/ansible/parsing/dataloader.py | 9 ++++----- .../targets/lookup_first_found/roles/a/tasks/main.yml | 1 + .../lookup_first_found/roles/a/tasks/subdir/main.yml | 7 +++++++ .../lookup_first_found/roles/a/tasks/subdir/relative | 0 .../targets/lookup_first_found/tasks/main.yml | 4 ++++ 6 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/dwim_is_role_fix_task_relative.yml create mode 100644 test/integration/targets/lookup_first_found/roles/a/tasks/main.yml create mode 100644 test/integration/targets/lookup_first_found/roles/a/tasks/subdir/main.yml create mode 100644 test/integration/targets/lookup_first_found/roles/a/tasks/subdir/relative diff --git a/changelogs/fragments/dwim_is_role_fix_task_relative.yml b/changelogs/fragments/dwim_is_role_fix_task_relative.yml new file mode 100644 index 00000000000..bb4c6b39c09 --- /dev/null +++ b/changelogs/fragments/dwim_is_role_fix_task_relative.yml @@ -0,0 +1,2 @@ +bugfixes: + - Fix using the current task's directory for looking up relative paths within roles (https://github.com/ansible/ansible/issues/82695). diff --git a/lib/ansible/parsing/dataloader.py b/lib/ansible/parsing/dataloader.py index 17fc5342964..9554aef36be 100644 --- a/lib/ansible/parsing/dataloader.py +++ b/lib/ansible/parsing/dataloader.py @@ -329,11 +329,10 @@ class DataLoader: if (is_role or self._is_role(path)) and b_pb_base_dir.endswith(b'/tasks'): search.append(os.path.join(os.path.dirname(b_pb_base_dir), b_dirname, b_source)) search.append(os.path.join(b_pb_base_dir, b_source)) - else: - # don't add dirname if user already is using it in source - if b_source.split(b'/')[0] != dirname: - search.append(os.path.join(b_upath, b_dirname, b_source)) - search.append(os.path.join(b_upath, b_source)) + # don't add dirname if user already is using it in source + if b_source.split(b'/')[0] != dirname: + search.append(os.path.join(b_upath, b_dirname, b_source)) + search.append(os.path.join(b_upath, b_source)) # always append basedir as last resort # don't add dirname if user already is using it in source diff --git a/test/integration/targets/lookup_first_found/roles/a/tasks/main.yml b/test/integration/targets/lookup_first_found/roles/a/tasks/main.yml new file mode 100644 index 00000000000..60f4b90d231 --- /dev/null +++ b/test/integration/targets/lookup_first_found/roles/a/tasks/main.yml @@ -0,0 +1 @@ +- include_tasks: subdir/main.yml diff --git a/test/integration/targets/lookup_first_found/roles/a/tasks/subdir/main.yml b/test/integration/targets/lookup_first_found/roles/a/tasks/subdir/main.yml new file mode 100644 index 00000000000..64f4d86e8d5 --- /dev/null +++ b/test/integration/targets/lookup_first_found/roles/a/tasks/subdir/main.yml @@ -0,0 +1,7 @@ +- assert: + that: + - "lookup('first_found', task_adjacent) is is_file" + vars: + task_adjacent: + - files: + - relative diff --git a/test/integration/targets/lookup_first_found/roles/a/tasks/subdir/relative b/test/integration/targets/lookup_first_found/roles/a/tasks/subdir/relative new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/integration/targets/lookup_first_found/tasks/main.yml b/test/integration/targets/lookup_first_found/tasks/main.yml index 174de6d6d75..9a4d134e383 100644 --- a/test/integration/targets/lookup_first_found/tasks/main.yml +++ b/test/integration/targets/lookup_first_found/tasks/main.yml @@ -152,3 +152,7 @@ assert: that: - q('first_found', ['/nonexistant'], skip=True) == [] + +- name: Test relative paths in roles + include_role: + role: "{{ role_path }}/roles/a"