Search path (#16387)

* smarter function to figure out relative paths

takes list of paths in order of relevance to current task
and does the dwim magic on them

* shared function for action plugins using new dwim

unify path construction and error info/messaging
made include and role non exclusive
corrected order and now smarter about tasks
includes inside roles are currently broken as they don't provide the correct role data
make dirname full match to avoid corner cases

* migrated action plugins to new dwim function

reported plugins to use exceptions instead of info

* clarified needle
This commit is contained in:
Brian Coca 2016-06-28 17:23:30 -04:00 committed by GitHub
parent e04d552bc6
commit 2bb7feec6d
9 changed files with 150 additions and 65 deletions

View file

@ -823,3 +823,31 @@ class ActionBase(with_metaclass(ABCMeta, object)):
diff["after"] = " [[ Diff output has been hidden because 'no_log: true' was specified for this result ]]"
return diff
def _find_needle(self, dirname, needle):
'''
find a needle in haystack of paths, optionally using 'dirname' as a subdir.
This will build the ordered list of paths to search and pass them to dwim
to get back the first existing file found.
'''
path_stack = []
dep_chain = self._task._block.get_dep_chain()
# inside role: add the dependency chain
if dep_chain:
path_stack.extend(reversed([x._role_path for x in dep_chain]))
task_dir = os.path.dirname(self._task.get_path())
# include from diff directory: add it to file path
if not task_dir.endswith('tasks') and task_dir != self._loader.get_basedir():
path_stack.append(task_dir)
result = self._loader.path_dwim_relative_stack(path_stack, dirname, needle)
if result is None:
raise AnsibleError("Unable to find '%s' in expected paths." % needle)
return result