From 88f5af81a4d5a2723faff6318c911c4b34dafdb6 Mon Sep 17 00:00:00 2001 From: Philippe Kueck Date: Mon, 19 Nov 2018 12:18:13 +0100 Subject: [PATCH] allow 'ignore_missing' for fileglob lookups --- .../47344-ignore_missing_fileglob.yml | 3 +++ lib/ansible/plugins/lookup/fileglob.py | 9 ++++++++- .../lookup_fileglob/non_existent/play.yml | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/47344-ignore_missing_fileglob.yml diff --git a/changelogs/fragments/47344-ignore_missing_fileglob.yml b/changelogs/fragments/47344-ignore_missing_fileglob.yml new file mode 100644 index 00000000000..b64a99dd642 --- /dev/null +++ b/changelogs/fragments/47344-ignore_missing_fileglob.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - fileglob - add 'ignore_missing' to suppress 'Unable to find...' warnings diff --git a/lib/ansible/plugins/lookup/fileglob.py b/lib/ansible/plugins/lookup/fileglob.py index 5ab730d576f..5cf505359da 100644 --- a/lib/ansible/plugins/lookup/fileglob.py +++ b/lib/ansible/plugins/lookup/fileglob.py @@ -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: diff --git a/test/integration/targets/lookup_fileglob/non_existent/play.yml b/test/integration/targets/lookup_fileglob/non_existent/play.yml index e92dff5accf..67339529917 100644 --- a/test/integration/targets/lookup_fileglob/non_existent/play.yml +++ b/test/integration/targets/lookup_fileglob/non_existent/play.yml @@ -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