From 2a29739ee6c6c5d4d50a0cdb2bc798f9f6032445 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Wed, 27 Sep 2023 16:11:51 +0200 Subject: [PATCH] Allow for searching handler subdir for included task via include_role (#81733) (#81758) Fixes #81722 (cherry picked from commit 1e7f7875c617a12e5b16bcf290d489a6446febdb) --- .../fragments/81722-handler-subdir-include_tasks.yml | 2 ++ lib/ansible/playbook/included_file.py | 9 ++++++--- .../handlers/include_handlers.yml | 2 ++ .../include_role_include_tasks_handler/handlers/main.yml | 2 ++ .../include_role_include_tasks_handler/tasks/main.yml | 2 ++ test/integration/targets/handlers/runme.sh | 3 +++ .../handlers/test_include_tasks_in_include_role.yml | 5 +++++ 7 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/81722-handler-subdir-include_tasks.yml create mode 100644 test/integration/targets/handlers/roles/include_role_include_tasks_handler/handlers/include_handlers.yml create mode 100644 test/integration/targets/handlers/roles/include_role_include_tasks_handler/handlers/main.yml create mode 100644 test/integration/targets/handlers/roles/include_role_include_tasks_handler/tasks/main.yml create mode 100644 test/integration/targets/handlers/test_include_tasks_in_include_role.yml diff --git a/changelogs/fragments/81722-handler-subdir-include_tasks.yml b/changelogs/fragments/81722-handler-subdir-include_tasks.yml new file mode 100644 index 00000000000..97607818a8f --- /dev/null +++ b/changelogs/fragments/81722-handler-subdir-include_tasks.yml @@ -0,0 +1,2 @@ +bugfixes: + - Allow for searching handler subdir for included task via include_role (https://github.com/ansible/ansible/issues/81722) diff --git a/lib/ansible/playbook/included_file.py b/lib/ansible/playbook/included_file.py index b833077c951..c8162dd2253 100644 --- a/lib/ansible/playbook/included_file.py +++ b/lib/ansible/playbook/included_file.py @@ -148,9 +148,12 @@ class IncludedFile: cumulative_path = parent_include_dir include_target = templar.template(include_result['include']) if original_task._role: - new_basedir = os.path.join(original_task._role._role_path, 'tasks', cumulative_path) - candidates = [loader.path_dwim_relative(original_task._role._role_path, 'tasks', include_target), - loader.path_dwim_relative(new_basedir, 'tasks', include_target)] + dirname = 'handlers' if isinstance(original_task, Handler) else 'tasks' + new_basedir = os.path.join(original_task._role._role_path, dirname, cumulative_path) + candidates = [ + loader.path_dwim_relative(original_task._role._role_path, dirname, include_target), + loader.path_dwim_relative(new_basedir, dirname, include_target) + ] for include_file in candidates: try: # may throw OSError diff --git a/test/integration/targets/handlers/roles/include_role_include_tasks_handler/handlers/include_handlers.yml b/test/integration/targets/handlers/roles/include_role_include_tasks_handler/handlers/include_handlers.yml new file mode 100644 index 00000000000..f39ac4fcc07 --- /dev/null +++ b/test/integration/targets/handlers/roles/include_role_include_tasks_handler/handlers/include_handlers.yml @@ -0,0 +1,2 @@ +- debug: + msg: handler ran diff --git a/test/integration/targets/handlers/roles/include_role_include_tasks_handler/handlers/main.yml b/test/integration/targets/handlers/roles/include_role_include_tasks_handler/handlers/main.yml new file mode 100644 index 00000000000..4ce8a3f2d51 --- /dev/null +++ b/test/integration/targets/handlers/roles/include_role_include_tasks_handler/handlers/main.yml @@ -0,0 +1,2 @@ +- name: handler + include_tasks: include_handlers.yml diff --git a/test/integration/targets/handlers/roles/include_role_include_tasks_handler/tasks/main.yml b/test/integration/targets/handlers/roles/include_role_include_tasks_handler/tasks/main.yml new file mode 100644 index 00000000000..50aec1c7b3d --- /dev/null +++ b/test/integration/targets/handlers/roles/include_role_include_tasks_handler/tasks/main.yml @@ -0,0 +1,2 @@ +- command: echo + notify: handler diff --git a/test/integration/targets/handlers/runme.sh b/test/integration/targets/handlers/runme.sh index be4aecb2ce0..9b17dc7bbf3 100755 --- a/test/integration/targets/handlers/runme.sh +++ b/test/integration/targets/handlers/runme.sh @@ -192,3 +192,6 @@ ansible-playbook test_listen_role_dedup.yml "$@" 2>&1 | tee out.txt [ "$(grep out.txt -ce 'a handler from a role')" = "1" ] ansible localhost -m include_role -a "name=r1-dep_chain-vars" "$@" + +ansible-playbook test_include_tasks_in_include_role.yml "$@" 2>&1 | tee out.txt +[ "$(grep out.txt -ce 'handler ran')" = "1" ] diff --git a/test/integration/targets/handlers/test_include_tasks_in_include_role.yml b/test/integration/targets/handlers/test_include_tasks_in_include_role.yml new file mode 100644 index 00000000000..405e4b50315 --- /dev/null +++ b/test/integration/targets/handlers/test_include_tasks_in_include_role.yml @@ -0,0 +1,5 @@ +- hosts: localhost + gather_facts: false + tasks: + - include_role: + name: include_role_include_tasks_handler