Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include in handler requires full path #71222

Closed
nomadcz opened this issue Aug 12, 2020 · 7 comments · Fixed by #73809
Closed

include in handler requires full path #71222

nomadcz opened this issue Aug 12, 2020 · 7 comments · Fixed by #73809
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. P3 Priority 3 - Approved, No Time Limitation python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@nomadcz
Copy link

nomadcz commented Aug 12, 2020

SUMMARY

To include tasks for handlers I have to specify full path instead of relative to the role's directory
Similar to: #19609

ISSUE TYPE
  • Bug Report
COMPONENT NAME

lib/ansible/playbook/included_file.py

ANSIBLE VERSION
ansible 2.9.4
  config file = /srv/data/ansible-dev/ansible.cfg
  configured module search path = ['/home/spider/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /srv/app/prod/ansible/core/ansible-2.9/lib64/python3.6/site-packages/ansible
  executable location = /srv/app/prod/ansible/core/ansible-2.9/bin/ansible
  python version = 3.6.3 (default, Apr 10 2019, 14:37:36) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]


CONFIGURATION

OS / ENVIRONMENT

RedHat 7

STEPS TO REPRODUCE

in role's handlers directory I have:

cat main.yml

---
# handlers file for sshd

- name: Include proper handlers for services
  include_tasks: "{{ ansible_distribution }}{{ ansible_distribution_major_version }}/handlers-services.yml"
  listen: 'Handle sshd services'

The full path works ok:

---
# handlers file for sshd

- name: Include proper handlers for services
  include_tasks: "{{ role_path }}/handlers/{{ ansible_distribution }}{{ ansible_distribution_major_version }}/handlers-services.yml"
  listen: 'Handle sshd services'
EXPECTED RESULTS

Tasks should be included from path relative to role

ACTUAL RESULTS

Include fails as ansible tries to include handlers-services.yml directly from playbook directory

RUNNING HANDLER [sshd : Include proper handlers] ***************************************************************************************************************************************
fatal: [tpinsas03]: FAILED! => {"reason": "Could not find or access '/srv/data/ansible-dev/playbooks/RedHat7/handlers-services.yml' on the Ansible Controller."}
        to retry, use: --limit @/srv/data/ansible-dev/retry/cm.retry

@ansibot
Copy link
Contributor

ansibot commented Aug 12, 2020

Files identified in the description:
None

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Aug 12, 2020
@bmillemathias
Copy link
Contributor

!component handlers

@sivel
Copy link
Member

sivel commented Aug 12, 2020

I've done a little research here, and I think it is the result of IncludedFile.process_include_results not using original_task.get_search_path when defining candidates which uses loader.path_dwim_relative restricting only to the tasks directory.

I'm not saying I've decided this is a bug yet, but just adding context for future discussion.

https://github.com/ansible/ansible/blob/devel/lib/ansible/playbook/included_file.py#L146-L149

@bcoca bcoca added P3 Priority 3 - Approved, No Time Limitation and removed needs_triage Needs a first human triage before being processed. labels Aug 18, 2020
@bcoca
Copy link
Member

bcoca commented Aug 18, 2020

dwim should be able to use the subdirs as it falls back to current directory for the file you are in 'handlers/main.yml'.

@sivel
Copy link
Member

sivel commented Aug 18, 2020

This may be sufficient, I haven't tested extensively. If anyone is interested, feel free to test this, and get a PR up:

diff --git a/lib/ansible/playbook/included_file.py b/lib/ansible/playbook/included_file.py
index 7870d83a6a..a734a5739c 100644
--- a/lib/ansible/playbook/included_file.py
+++ b/lib/ansible/playbook/included_file.py
@@ -23,6 +23,7 @@ import os
 
 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
@@ -165,7 +166,12 @@ 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'])

I'm not actively working this further right now.

@ansibot
Copy link
Contributor

ansibot commented Oct 13, 2020

@nomadcz: Greetings! Thanks for taking the time to open this issue. In order for the community to handle your issue effectively, we need a bit more information.

Here are the items we could not find in your description:

  • component name

Please set the description of this issue with an appropriate template from:
https://github.com/ansible/ansible/tree/devel/.github/ISSUE_TEMPLATE

click here for bot help

@ansibot ansibot added needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. labels Oct 13, 2020
@ansibot
Copy link
Contributor

ansibot commented Oct 14, 2020

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot removed needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. labels Oct 14, 2020
@ansible ansible locked and limited conversation to collaborators Apr 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. P3 Priority 3 - Approved, No Time Limitation python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants