Allow for searching handler subdir for included tasks (#73809)

* Allow for searching handler subdir for included tasks
This commit is contained in:
David Shrewsbury 2021-03-15 15:39:59 -04:00 committed by GitHub
parent 3098022146
commit 1e5ccb326f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 81 additions and 47 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- A handler defined within a role will now search handlers subdir for included tasks (issue https://github.com/ansible/ansible/issues/71222).

View file

@ -24,6 +24,7 @@ import os
from ansible import constants as C
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_text
from ansible.playbook.handler import Handler
from ansible.playbook.task_include import TaskInclude
from ansible.playbook.role_include import IncludeRole
from ansible.template import Templar
@ -114,7 +115,6 @@ class IncludedFile:
if original_task.action in C._ACTION_ALL_INCLUDE_TASKS:
include_file = None
if original_task:
if original_task.static:
continue
@ -167,7 +167,11 @@ class IncludedFile:
if include_file is None:
if original_task._role:
include_target = templar.template(include_result['include'])
include_file = loader.path_dwim_relative(original_task._role._role_path, 'tasks', include_target)
include_file = loader.path_dwim_relative(
original_task._role._role_path,
'handlers' if isinstance(original_task, Handler) else 'tasks',
include_target,
is_role=True)
else:
include_file = loader.path_dwim(include_result['include'])

View file

@ -0,0 +1 @@
- debug: msg="handler with tasks from A.yml called"

View file

@ -0,0 +1,5 @@
- name: role-based handler from handler subdir
include_tasks: A.yml
- name: role-based handler from tasks subdir
include_tasks: B.yml

View file

@ -0,0 +1 @@
- debug: msg="handler with tasks from B.yml called"

View file

@ -87,6 +87,9 @@ set -e
# https://github.com/ansible/ansible/issues/47287
[ "$(ansible-playbook test_handlers_including_task.yml -i ../../inventory -v "$@" | grep -E -o 'failed=[0-9]+')" = "failed=0" ]
# https://github.com/ansible/ansible/issues/71222
ansible-playbook test_role_handlers_including_tasks.yml -i ../../inventory -v "$@"
# https://github.com/ansible/ansible/issues/27237
set +e
result="$(ansible-playbook test_handlers_template_run_once.yml -i inventory.handlers "$@" 2>&1)"

View file

@ -0,0 +1,18 @@
---
- name: Verify a role handler can include other tasks from handlers and tasks subdirs
hosts: testhost
roles:
- test_role_handlers_include_tasks
tasks:
- name: notify a role-based handler (include tasks from handler subdir)
debug:
msg: notifying role handler
changed_when: yes
notify: role-based handler from handler subdir
- name: notify a role-based handler (include tasks from tasks subdir)
debug:
msg: notifying another role handler
changed_when: yes
notify: role-based handler from tasks subdir