simplified and normalized lookup search path behaviour
This commit is contained in:
parent
857f584ebf
commit
b678b9828c
3 changed files with 33 additions and 26 deletions
|
@ -46,6 +46,12 @@ class LookupModule(LookupBase):
|
||||||
terms = [ terms ]
|
terms = [ terms ]
|
||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
|
|
||||||
|
if 'role_path' in variables:
|
||||||
|
basedir = variables['role_path']
|
||||||
|
else:
|
||||||
|
basedir = self._loader.get_basedir()
|
||||||
|
|
||||||
for term in terms:
|
for term in terms:
|
||||||
params = term.split()
|
params = term.split()
|
||||||
key = params[0]
|
key = params[0]
|
||||||
|
@ -69,9 +75,8 @@ class LookupModule(LookupBase):
|
||||||
if paramvals['delimiter'] == 'TAB':
|
if paramvals['delimiter'] == 'TAB':
|
||||||
paramvals['delimiter'] = "\t"
|
paramvals['delimiter'] = "\t"
|
||||||
|
|
||||||
path = self._loader.path_dwim(paramvals['file'])
|
lookupfile = self._loader.path_dwim_relative(basedir, 'files', term)
|
||||||
|
var = self.read_csv(lookupfile, key, paramvals['delimiter'], paramvals['default'], paramvals['col'])
|
||||||
var = self.read_csv(path, key, paramvals['delimiter'], paramvals['default'], paramvals['col'])
|
|
||||||
if var is not None:
|
if var is not None:
|
||||||
if type(var) is list:
|
if type(var) is list:
|
||||||
for v in var:
|
for v in var:
|
||||||
|
|
|
@ -31,10 +31,13 @@ class LookupModule(LookupBase):
|
||||||
terms = [ terms ]
|
terms = [ terms ]
|
||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
|
|
||||||
|
if 'role_path' in variables:
|
||||||
|
basedir = variables['role_path']
|
||||||
|
else:
|
||||||
|
basedir = self._loader.get_basedir()
|
||||||
|
|
||||||
for term in terms:
|
for term in terms:
|
||||||
basedir_path = self._loader.path_dwim(term)
|
|
||||||
relative_path = None
|
|
||||||
playbook_path = None
|
|
||||||
|
|
||||||
# Special handling of the file lookup, used primarily when the
|
# Special handling of the file lookup, used primarily when the
|
||||||
# lookup is done from a role. If the file isn't found in the
|
# lookup is done from a role. If the file isn't found in the
|
||||||
|
@ -42,23 +45,14 @@ class LookupModule(LookupBase):
|
||||||
# role/files/ directory, and finally the playbook directory
|
# role/files/ directory, and finally the playbook directory
|
||||||
# itself (which will be relative to the current working dir)
|
# itself (which will be relative to the current working dir)
|
||||||
|
|
||||||
if 'role_path' in variables:
|
lookupfile = self._loader.path_dwim_relative(basedir, 'files', term)
|
||||||
relative_path = self._loader.path_dwim_relative(variables['role_path'], 'files', term)
|
try:
|
||||||
|
if lookupfile:
|
||||||
# FIXME: the original file stuff still needs to be worked out, but the
|
contents, show_data = self._loader._get_file_contents(lookupfile)
|
||||||
# playbook_dir stuff should be able to be removed as it should
|
|
||||||
# be covered by the fact that the loader contains that info
|
|
||||||
if 'playbook_dir' in variables:
|
|
||||||
playbook_path = self._loader.path_dwim_relative(variables['playbook_dir'],'files', term)
|
|
||||||
|
|
||||||
for path in (basedir_path, relative_path, playbook_path):
|
|
||||||
try:
|
|
||||||
contents, show_data = self._loader._get_file_contents(path)
|
|
||||||
ret.append(contents.rstrip())
|
ret.append(contents.rstrip())
|
||||||
break
|
else:
|
||||||
except AnsibleParserError:
|
raise AnsibleParserError()
|
||||||
continue
|
except AnsibleParserError:
|
||||||
else:
|
|
||||||
raise AnsibleError("could not locate file in lookup: %s" % term)
|
raise AnsibleError("could not locate file in lookup: %s" % term)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
|
@ -30,16 +30,24 @@ class LookupModule(LookupBase):
|
||||||
if not isinstance(terms, list):
|
if not isinstance(terms, list):
|
||||||
terms = [ terms ]
|
terms = [ terms ]
|
||||||
|
|
||||||
|
ret = []
|
||||||
|
|
||||||
templar = Templar(loader=self._loader, variables=variables)
|
templar = Templar(loader=self._loader, variables=variables)
|
||||||
|
|
||||||
ret = []
|
if 'role_path' in variables:
|
||||||
|
basedir = variables['role_path']
|
||||||
|
else:
|
||||||
|
basedir = self._loader.get_basedir()
|
||||||
|
|
||||||
for term in terms:
|
for term in terms:
|
||||||
path = self._loader.path_dwim(term)
|
|
||||||
if os.path.exists(path):
|
lookupfile = self._loader.path_dwim_relative(basedir, 'templates', term)
|
||||||
with open(path, 'r') as f:
|
if lookupfile and os.path.exists(lookupfile):
|
||||||
|
with open(lookupfile, 'r') as f:
|
||||||
template_data = f.read()
|
template_data = f.read()
|
||||||
res = templar.template(template_data, preserve_trailing_newlines=True)
|
res = templar.template(template_data, preserve_trailing_newlines=True)
|
||||||
ret.append(res)
|
ret.append(res)
|
||||||
else:
|
else:
|
||||||
raise AnsibleError("the template file %s could not be found for the lookup" % term)
|
raise AnsibleError("the template file %s could not be found for the lookup" % term)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
Loading…
Reference in a new issue