mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-14 21:19:12 -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
|
@ -18,7 +18,6 @@ from __future__ import (absolute_import, division, print_function)
|
|||
__metaclass__ = type
|
||||
|
||||
import shelve
|
||||
import os
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
|
@ -43,9 +42,6 @@ class LookupModule(LookupBase):
|
|||
ret = []
|
||||
|
||||
for term in terms:
|
||||
playbook_path = None
|
||||
relative_path = None
|
||||
|
||||
paramvals = {"file": None, "key": None}
|
||||
params = term.split()
|
||||
|
||||
|
@ -59,24 +55,18 @@ class LookupModule(LookupBase):
|
|||
# In case "file" or "key" are not present
|
||||
raise AnsibleError(e)
|
||||
|
||||
file = paramvals['file']
|
||||
key = paramvals['key']
|
||||
basedir_path = self._loader.path_dwim(file)
|
||||
|
||||
# Search also in the role/files directory and in the playbook directory
|
||||
if 'role_path' in variables:
|
||||
relative_path = self._loader.path_dwim_relative(variables['role_path'], 'files', file)
|
||||
if 'playbook_dir' in variables:
|
||||
playbook_path = self._loader.path_dwim_relative(variables['playbook_dir'],'files', file)
|
||||
shelvefile = self.find_file_in_search_path(variables, 'files', paramvals['file'])
|
||||
|
||||
for path in (basedir_path, relative_path, playbook_path):
|
||||
if path and os.path.exists(path):
|
||||
res = self.read_shelve(path, key)
|
||||
if res is None:
|
||||
raise AnsibleError("Key %s not found in shelve file %s" % (key, file))
|
||||
# Convert the value read to string
|
||||
ret.append(str(res))
|
||||
break
|
||||
if shelvefile:
|
||||
res = self.read_shelve(shelvefile, key)
|
||||
if res is None:
|
||||
raise AnsibleError("Key %s not found in shelve file %s" % (key, file))
|
||||
# Convert the value read to string
|
||||
ret.append(str(res))
|
||||
break
|
||||
else:
|
||||
raise AnsibleError("Could not locate shelve file in lookup: %s" % file)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue