summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Shrewsbury <Shrews@users.noreply.github.com>2021-03-15 15:39:59 -0400
committerGitHub <noreply@github.com>2021-03-15 15:39:59 -0400
commit1e5ccb326fa49046da7a06e42734836b99e2d5cc (patch)
treea35d86e9e1884645bf3f69b495cd61285a4500fe /lib
parent309802214616b3981a4d2a6b3bdcf4077bd0e062 (diff)
downloadansible-1e5ccb326fa49046da7a06e42734836b99e2d5cc.tar.gz
Allow for searching handler subdir for included tasks (#73809)
* Allow for searching handler subdir for included tasks
Diffstat (limited to 'lib')
-rw-r--r--lib/ansible/playbook/included_file.py102
1 files changed, 53 insertions, 49 deletions
diff --git a/lib/ansible/playbook/included_file.py b/lib/ansible/playbook/included_file.py
index 2d209deb60..36108c07e0 100644
--- a/lib/ansible/playbook/included_file.py
+++ b/lib/ansible/playbook/included_file.py
@@ -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,60 +115,63 @@ class IncludedFile:
if original_task.action in C._ACTION_ALL_INCLUDE_TASKS:
include_file = None
- if original_task:
- if original_task.static:
- continue
-
- if original_task._parent:
- # handle relative includes by walking up the list of parent include
- # tasks and checking the relative result to see if it exists
- parent_include = original_task._parent
- cumulative_path = None
- while parent_include is not None:
- if not isinstance(parent_include, TaskInclude):
- parent_include = parent_include._parent
- continue
- if isinstance(parent_include, IncludeRole):
- parent_include_dir = parent_include._role_path
- else:
+ if original_task.static:
+ continue
+
+ if original_task._parent:
+ # handle relative includes by walking up the list of parent include
+ # tasks and checking the relative result to see if it exists
+ parent_include = original_task._parent
+ cumulative_path = None
+ while parent_include is not None:
+ if not isinstance(parent_include, TaskInclude):
+ parent_include = parent_include._parent
+ continue
+ if isinstance(parent_include, IncludeRole):
+ parent_include_dir = parent_include._role_path
+ else:
+ try:
+ parent_include_dir = os.path.dirname(templar.template(parent_include.args.get('_raw_params')))
+ except AnsibleError as e:
+ parent_include_dir = ''
+ display.warning(
+ 'Templating the path of the parent %s failed. The path to the '
+ 'included file may not be found. '
+ 'The error was: %s.' % (original_task.action, to_text(e))
+ )
+ if cumulative_path is not None and not os.path.isabs(cumulative_path):
+ cumulative_path = os.path.join(parent_include_dir, cumulative_path)
+ else:
+ 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)]
+ for include_file in candidates:
try:
- parent_include_dir = os.path.dirname(templar.template(parent_include.args.get('_raw_params')))
- except AnsibleError as e:
- parent_include_dir = ''
- display.warning(
- 'Templating the path of the parent %s failed. The path to the '
- 'included file may not be found. '
- 'The error was: %s.' % (original_task.action, to_text(e))
- )
- if cumulative_path is not None and not os.path.isabs(cumulative_path):
- cumulative_path = os.path.join(parent_include_dir, cumulative_path)
- else:
- 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)]
- for include_file in candidates:
- try:
- # may throw OSError
- os.stat(include_file)
- # or select the task file if it exists
- break
- except OSError:
- pass
- else:
- include_file = loader.path_dwim_relative(loader.get_basedir(), cumulative_path, include_target)
-
- if os.path.exists(include_file):
- break
- else:
- parent_include = parent_include._parent
+ # may throw OSError
+ os.stat(include_file)
+ # or select the task file if it exists
+ break
+ except OSError:
+ pass
+ else:
+ include_file = loader.path_dwim_relative(loader.get_basedir(), cumulative_path, include_target)
+
+ if os.path.exists(include_file):
+ break
+ else:
+ parent_include = parent_include._parent
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'])