summaryrefslogtreecommitdiff
path: root/lib/ansible/plugins/lookup/first_found.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ansible/plugins/lookup/first_found.py')
-rw-r--r--lib/ansible/plugins/lookup/first_found.py31
1 files changed, 20 insertions, 11 deletions
diff --git a/lib/ansible/plugins/lookup/first_found.py b/lib/ansible/plugins/lookup/first_found.py
index a882db01..68628801 100644
--- a/lib/ansible/plugins/lookup/first_found.py
+++ b/lib/ansible/plugins/lookup/first_found.py
@@ -15,9 +15,9 @@ DOCUMENTATION = """
to the containing locations of role / play / include and so on.
- The list of files has precedence over the paths searched.
For example, A task in a role has a 'file1' in the play's relative path, this will be used, 'file2' in role's relative path will not.
- - Either a list of files C(_terms) or a key C(files) with a list of files is required for this plugin to operate.
+ - Either a list of files O(_terms) or a key O(files) with a list of files is required for this plugin to operate.
notes:
- - This lookup can be used in 'dual mode', either passing a list of file names or a dictionary that has C(files) and C(paths).
+ - This lookup can be used in 'dual mode', either passing a list of file names or a dictionary that has O(files) and O(paths).
options:
_terms:
description: A list of file names.
@@ -35,16 +35,19 @@ DOCUMENTATION = """
type: boolean
default: False
description:
- - When C(True), return an empty list when no files are matched.
+ - When V(True), return an empty list when no files are matched.
- This is useful when used with C(with_first_found), as an empty list return to C(with_) calls
causes the calling task to be skipped.
- - When used as a template via C(lookup) or C(query), setting I(skip=True) will *not* cause the task to skip.
+ - When used as a template via C(lookup) or C(query), setting O(skip=True) will *not* cause the task to skip.
Tasks must handle the empty list return from the template.
- - When C(False) and C(lookup) or C(query) specifies I(errors='ignore') all errors (including no file found,
+ - When V(False) and C(lookup) or C(query) specifies O(ignore:errors='ignore') all errors (including no file found,
but potentially others) return an empty string or an empty list respectively.
- - When C(True) and C(lookup) or C(query) specifies I(errors='ignore'), no file found will return an empty
+ - When V(True) and C(lookup) or C(query) specifies O(ignore:errors='ignore'), no file found will return an empty
list and other potential errors return an empty string or empty list depending on the template call
- (in other words return values of C(lookup) v C(query)).
+ (in other words return values of C(lookup) vs C(query)).
+ seealso:
+ - ref: playbook_task_paths
+ description: Search paths used for relative paths/files.
"""
EXAMPLES = """
@@ -180,8 +183,9 @@ class LookupModule(LookupBase):
for term in terms:
if isinstance(term, Mapping):
self.set_options(var_options=variables, direct=term)
+ files = self.get_option('files')
elif isinstance(term, string_types):
- self.set_options(var_options=variables, direct=kwargs)
+ files = [term]
elif isinstance(term, Sequence):
partial, skip = self._process_terms(term, variables, kwargs)
total_search.extend(partial)
@@ -189,7 +193,6 @@ class LookupModule(LookupBase):
else:
raise AnsibleLookupError("Invalid term supplied, can handle string, mapping or list of strings but got: %s for %s" % (type(term), term))
- files = self.get_option('files')
paths = self.get_option('paths')
# NOTE: this is used as 'global' but can be set many times?!?!?
@@ -206,8 +209,8 @@ class LookupModule(LookupBase):
f = os.path.join(path, fn)
total_search.append(f)
elif filelist:
- # NOTE: this seems wrong, should be 'extend' as any option/entry can clobber all
- total_search = filelist
+ # NOTE: this is now 'extend', previouslly it would clobber all options, but we deemed that a bug
+ total_search.extend(filelist)
else:
total_search.append(term)
@@ -215,6 +218,10 @@ class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):
+ if not terms:
+ self.set_options(var_options=variables, direct=kwargs)
+ terms = self.get_option('files')
+
total_search, skip = self._process_terms(terms, variables, kwargs)
# NOTE: during refactor noticed that the 'using a dict' as term
@@ -230,6 +237,8 @@ class LookupModule(LookupBase):
try:
fn = self._templar.template(fn)
except (AnsibleUndefinedVariable, UndefinedError):
+ # NOTE: backwards compat ff behaviour is to ignore errors when vars are undefined.
+ # moved here from task_executor.
continue
# get subdir if set by task executor, default to files otherwise