allow 'ignore_missing' for fileglob lookups

pull/47344/head
Philippe Kueck 6 years ago committed by Philippe Kueck
parent e3ccdaaa2e
commit 88f5af81a4
No known key found for this signature in database
GPG Key ID: E10E57D62DB57A3B

@ -0,0 +1,3 @@
---
minor_changes:
- fileglob - add 'ignore_missing' to suppress 'Unable to find...' warnings

@ -15,6 +15,11 @@ DOCUMENTATION = """
_terms:
description: path(s) of files to read
required: True
ignore_missing:
description: Flag to control whether or not no matches found return a warning
type: boolean
default: False
version_added: "2.14"
notes:
- Patterns are only supported on files, not directory/paths.
- See R(Ansible task paths,playbook_task_paths) to understand how file lookup occurs with paths.
@ -59,12 +64,14 @@ class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
self.set_options(direct=kwargs)
ret = []
for term in terms:
term_file = os.path.basename(term)
found_paths = []
if term_file != term:
found_paths.append(self.find_file_in_search_path(variables, 'files', os.path.dirname(term)))
found_paths.append(self.find_file_in_search_path(variables, 'files', os.path.dirname(term), ignore_missing=self.get_option('ignore_missing')))
else:
# no dir, just file, so use paths and 'files' paths instead
if 'ansible_search_path' in variables:

@ -4,3 +4,22 @@
- name: fileglob should be empty
assert:
that: q("fileglob", seed) | length == 0
- name: Test ignore_missing
vars:
query_with_warnings: "query('fileglob', 'missing/file')"
query_without_warnings: "query('fileglob', 'missing/file', ignore_missing=true)"
warning: "Unable to find 'missing' in expected paths"
block:
- name: Test a warning is given for a missing path
command: ansible localhost -m debug -a msg="{{ '{{' + query_with_warnings + '}}' }}"
register: warn_for_missing
- name: Test ignoring the warning
command: ansible localhost -m debug -a msg="{{ '{{' + query_without_warnings + '}}' }}"
register: ignore_missing
- assert:
that:
- warning in warn_for_missing.stderr
- warning not in ignore_missing.stderr

Loading…
Cancel
Save