mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-27 07:01:22 -07:00
Add support for relative paths in the file lookup plugin for roles
Fixes #7628
This commit is contained in:
parent
b3bbca03d5
commit
ad97c618cf
1 changed files with 18 additions and 4 deletions
|
@ -35,11 +35,25 @@ class LookupModule(object):
|
||||||
terms = [ terms ]
|
terms = [ terms ]
|
||||||
|
|
||||||
for term in terms:
|
for term in terms:
|
||||||
path = utils.path_dwim(self.basedir, term)
|
basedir_path = utils.path_dwim(self.basedir, term)
|
||||||
if not os.path.exists(path):
|
relative_path = None
|
||||||
raise errors.AnsibleError("%s does not exist" % path)
|
playbook_path = None
|
||||||
|
|
||||||
|
# Special handling of the file lookup, used primarily when the
|
||||||
|
# lookup is done from a role. If the file isn't found in the
|
||||||
|
# basedir of the current file, use dwim_relative to look in the
|
||||||
|
# role/files/ directory, and finally the playbook directory
|
||||||
|
# itself (which will be relative to the current working dir)
|
||||||
|
if '_original_file' in inject:
|
||||||
|
relative_path = utils.path_dwim_relative(inject['_original_file'], 'files', term, self.basedir, check=False)
|
||||||
|
if 'playbook_dir' in inject:
|
||||||
|
playbook_path = os.path.join(inject['playbook_dir'], term)
|
||||||
|
|
||||||
|
for path in (basedir_path, relative_path, playbook_path):
|
||||||
|
if path and os.path.exists(path):
|
||||||
ret.append(codecs.open(path, encoding="utf8").read().rstrip())
|
ret.append(codecs.open(path, encoding="utf8").read().rstrip())
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise errors.AnsibleError("could not locate file in lookup: %s" % term)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue