From d664f13b4a117b324f107b603e9b8e2bb9af50c5 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Wed, 22 Nov 2023 17:42:51 +0100 Subject: [PATCH] Allow include_tasks handlers for searching role subdirs (#82248) Fixes #82241 --- changelogs/fragments/82241-handler-include-tasks-from.yml | 2 ++ lib/ansible/playbook/included_file.py | 4 ++-- test/integration/targets/handlers/82241.yml | 6 ++++++ .../targets/handlers/roles/role-82241/handlers/main.yml | 2 ++ .../targets/handlers/roles/role-82241/tasks/entry_point.yml | 2 ++ .../handlers/roles/role-82241/tasks/included_tasks.yml | 2 ++ test/integration/targets/handlers/runme.sh | 3 +++ 7 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/82241-handler-include-tasks-from.yml create mode 100644 test/integration/targets/handlers/82241.yml create mode 100644 test/integration/targets/handlers/roles/role-82241/handlers/main.yml create mode 100644 test/integration/targets/handlers/roles/role-82241/tasks/entry_point.yml create mode 100644 test/integration/targets/handlers/roles/role-82241/tasks/included_tasks.yml diff --git a/changelogs/fragments/82241-handler-include-tasks-from.yml b/changelogs/fragments/82241-handler-include-tasks-from.yml new file mode 100644 index 00000000000..276a612bf7e --- /dev/null +++ b/changelogs/fragments/82241-handler-include-tasks-from.yml @@ -0,0 +1,2 @@ +bugfixes: + - Fix issue where an ``include_tasks`` handler in a role was not able to locate a file in ``tasks/`` when ``tasks_from`` was used as a role entry point and ``main.yml`` was not present (https://github.com/ansible/ansible/issues/82241) diff --git a/lib/ansible/playbook/included_file.py b/lib/ansible/playbook/included_file.py index 177900f653e..14d8982231f 100644 --- a/lib/ansible/playbook/included_file.py +++ b/lib/ansible/playbook/included_file.py @@ -147,8 +147,8 @@ class IncludedFile: 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) + loader.path_dwim_relative(original_task._role._role_path, dirname, include_target, is_role=True), + loader.path_dwim_relative(new_basedir, dirname, include_target, is_role=True) ] for include_file in candidates: try: diff --git a/test/integration/targets/handlers/82241.yml b/test/integration/targets/handlers/82241.yml new file mode 100644 index 00000000000..4a9421fbcd9 --- /dev/null +++ b/test/integration/targets/handlers/82241.yml @@ -0,0 +1,6 @@ +- hosts: A + gather_facts: false + tasks: + - import_role: + name: role-82241 + tasks_from: entry_point.yml diff --git a/test/integration/targets/handlers/roles/role-82241/handlers/main.yml b/test/integration/targets/handlers/roles/role-82241/handlers/main.yml new file mode 100644 index 00000000000..ad59b963e2e --- /dev/null +++ b/test/integration/targets/handlers/roles/role-82241/handlers/main.yml @@ -0,0 +1,2 @@ +- name: handler + include_tasks: included_tasks.yml diff --git a/test/integration/targets/handlers/roles/role-82241/tasks/entry_point.yml b/test/integration/targets/handlers/roles/role-82241/tasks/entry_point.yml new file mode 100644 index 00000000000..50aec1c7b3d --- /dev/null +++ b/test/integration/targets/handlers/roles/role-82241/tasks/entry_point.yml @@ -0,0 +1,2 @@ +- command: echo + notify: handler diff --git a/test/integration/targets/handlers/roles/role-82241/tasks/included_tasks.yml b/test/integration/targets/handlers/roles/role-82241/tasks/included_tasks.yml new file mode 100644 index 00000000000..e3ffeb7e137 --- /dev/null +++ b/test/integration/targets/handlers/roles/role-82241/tasks/included_tasks.yml @@ -0,0 +1,2 @@ +- debug: + msg: included_task_from_tasks_dir diff --git a/test/integration/targets/handlers/runme.sh b/test/integration/targets/handlers/runme.sh index e24dad60289..613fa4a8185 100755 --- a/test/integration/targets/handlers/runme.sh +++ b/test/integration/targets/handlers/runme.sh @@ -210,3 +210,6 @@ ansible-playbook force_handlers_blocks_81533-2.yml -i inventory.handlers "$@" 2> ansible-playbook nested_flush_handlers_failure_force.yml -i inventory.handlers "$@" 2>&1 | tee out.txt [ "$(grep out.txt -ce 'flush_handlers_rescued')" = "1" ] [ "$(grep out.txt -ce 'flush_handlers_always')" = "2" ] + +ansible-playbook 82241.yml -i inventory.handlers "$@" 2>&1 | tee out.txt +[ "$(grep out.txt -ce 'included_task_from_tasks_dir')" = "1" ]