mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-31 05:19:09 -07:00
fixed lookup search path (#16630)
* fixed lookup search path added ansible_search_path var that contains the proper list and in order removed roledir var which was only used by first_found, rest used role_path added needle function for lookups that mirrors the action plugin one, now both types of plugins use same pathing. * added missing os import * renamed as per feedback * fixed missing rename in first_found * also fixed first_found * fixed import to match new error class * fixed getattr ref
This commit is contained in:
parent
221520cbad
commit
3c39bb5633
11 changed files with 87 additions and 84 deletions
|
@ -19,7 +19,6 @@ __metaclass__ = type
|
|||
|
||||
import os
|
||||
|
||||
from ansible import constants as C
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
from ansible.utils.unicode import to_unicode
|
||||
|
@ -36,26 +35,25 @@ class LookupModule(LookupBase):
|
|||
def run(self, terms, variables, **kwargs):
|
||||
|
||||
convert_data_p = kwargs.get('convert_data', True)
|
||||
basedir = self.get_basedir(variables)
|
||||
|
||||
ret = []
|
||||
|
||||
for term in terms:
|
||||
display.debug("File lookup term: %s" % term)
|
||||
|
||||
lookupfile = self._loader.path_dwim_relative(basedir, 'templates', term)
|
||||
lookupfile = self.find_file_in_search_path(variables, 'templates', term)
|
||||
display.vvvv("File lookup using %s as file" % lookupfile)
|
||||
if lookupfile and os.path.exists(lookupfile):
|
||||
if lookupfile:
|
||||
with open(lookupfile, 'r') as f:
|
||||
template_data = to_unicode(f.read())
|
||||
|
||||
searchpath = [self._loader._basedir, os.path.dirname(lookupfile)]
|
||||
if 'role_path' in variables:
|
||||
if C.DEFAULT_ROLES_PATH:
|
||||
searchpath[:0] = C.DEFAULT_ROLES_PATH
|
||||
searchpath.insert(1, variables['role_path'])
|
||||
|
||||
# set jinja2 internal search path for includes
|
||||
if 'ansible_search_path' in variables:
|
||||
searchpath = variables['ansible_search_path']
|
||||
else:
|
||||
searchpath = [self._loader._basedir, os.path.dirname(lookupfile)]
|
||||
self._templar.environment.loader.searchpath = searchpath
|
||||
|
||||
# do the templating
|
||||
res = self._templar.template(template_data, preserve_trailing_newlines=True,convert_data=convert_data_p)
|
||||
ret.append(res)
|
||||
else:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue