Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ansible/ansible.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Coca <bcoca@users.noreply.github.com>2022-10-27 22:28:32 +0300
committerGitHub <noreply@github.com>2022-10-27 22:28:32 +0300
commitaaab0791d9507b64ea04903bf35500c87ebb33b4 (patch)
treecc042fed7a3f9382a3b0df2b266eea0a08dd6ecb
parent0bacea70c4ad73cd7839762dad33924f744aabc8 (diff)
File lookup handle missing file more gracefully (#79230)
previously it would have a 2nd tb due to bad error raising also remove superfluous warning
-rw-r--r--changelogs/fragments/file_lookup_fix.yml2
-rw-r--r--lib/ansible/plugins/lookup/file.py14
2 files changed, 9 insertions, 7 deletions
diff --git a/changelogs/fragments/file_lookup_fix.yml b/changelogs/fragments/file_lookup_fix.yml
new file mode 100644
index 00000000000..0e6ba49da41
--- /dev/null
+++ b/changelogs/fragments/file_lookup_fix.yml
@@ -0,0 +1,2 @@
+bugfixes:
+ - file lookup now handles missing files more gracefully.
diff --git a/lib/ansible/plugins/lookup/file.py b/lib/ansible/plugins/lookup/file.py
index fa9191ee1bb..9657536d73a 100644
--- a/lib/ansible/plugins/lookup/file.py
+++ b/lib/ansible/plugins/lookup/file.py
@@ -50,7 +50,7 @@ RETURN = """
elements: str
"""
-from ansible.errors import AnsibleError, AnsibleParserError
+from ansible.errors import AnsibleError, AnsibleOptionsError
from ansible.plugins.lookup import LookupBase
from ansible.module_utils._text import to_text
from ansible.utils.display import Display
@@ -67,11 +67,10 @@ class LookupModule(LookupBase):
for term in terms:
display.debug("File lookup term: %s" % term)
-
# Find the file in the expected search path
- lookupfile = self.find_file_in_search_path(variables, 'files', term)
- display.vvvv(u"File lookup using %s as file" % lookupfile)
try:
+ lookupfile = self.find_file_in_search_path(variables, 'files', term, ignore_missing=True)
+ display.vvvv(u"File lookup using %s as file" % lookupfile)
if lookupfile:
b_contents, show_data = self._loader._get_file_contents(lookupfile)
contents = to_text(b_contents, errors='surrogate_or_strict')
@@ -81,8 +80,9 @@ class LookupModule(LookupBase):
contents = contents.rstrip()
ret.append(contents)
else:
- raise AnsibleParserError()
- except AnsibleParserError:
- raise AnsibleError("could not locate file in lookup: %s" % term)
+ # TODO: only add search info if abs path?
+ raise AnsibleError("file not found, use -vvvvv to see paths searched")
+ except AnsibleError as e:
+ raise AnsibleOptionsError("The 'file' lookup had an issue accessing the file '%s'" % term, orig_exc=e)
return ret