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

@ -17,11 +17,9 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
from ansible.errors import AnsibleError
from ansible.plugins.action import ActionBase
from ansible.utils.unicode import to_str
class ActionModule(ActionBase):
@ -33,25 +31,22 @@ class ActionModule(ActionBase):
result = super(ActionModule, self).run(tmp, task_vars)
source = self._task.args.get('_raw_params')
try:
source = self._find_needle('vars', self._task.args.get('_raw_params'))
except AnsibleError as e:
result['failed'] = True
result['message'] = to_str(e)
return result
if self._task._role:
source = self._loader.path_dwim_relative(self._task._role._role_path, 'vars', source)
(data, show_content) = self._loader._get_file_contents(source)
data = self._loader.load(data, show_content)
if data is None:
data = {}
if not isinstance(data, dict):
result['failed'] = True
result['message'] = "%s must be stored as a dictionary/hash" % source
else:
source = self._loader.path_dwim_relative(self._loader.get_basedir(), 'vars', source)
if os.path.exists(source):
(data, show_content) = self._loader._get_file_contents(source)
data = self._loader.load(data, show_content)
if data is None:
data = {}
if not isinstance(data, dict):
raise AnsibleError("%s must be stored as a dictionary/hash" % source)
result['ansible_facts'] = data
result['_ansible_no_log'] = not show_content
else:
result['failed'] = True
result['msg'] = "Source file not found."
result['file'] = source
return result