From c30e340094ed9bb8c75fb289e13059f2cb5329f4 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Wed, 4 Dec 2019 04:16:36 +0530 Subject: [PATCH] [2.9] include_vars: Check NoneType for raw_params (#65301) Lookup 'first_found' returns empty list which results in raw_params checking. Check NoneType for 'raw_params' before proceeding. Fixes: #64939 Signed-off-by: Abhijeet Kasurde (cherry picked from commit 8d0c2cd4d50307f96893464f425fc0e9d303f890) --- changelogs/fragments/include_vars_fix_none.yml | 2 ++ lib/ansible/errors/__init__.py | 6 ++++-- lib/ansible/plugins/action/include_vars.py | 4 +++- .../targets/include_vars/tasks/main.yml | 14 ++++++++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/include_vars_fix_none.yml diff --git a/changelogs/fragments/include_vars_fix_none.yml b/changelogs/fragments/include_vars_fix_none.yml new file mode 100644 index 00000000000..f17d8bc7268 --- /dev/null +++ b/changelogs/fragments/include_vars_fix_none.yml @@ -0,0 +1,2 @@ +bugfixes: +- Check NoneType for raw_params before proceeding in include_vars (https://github.com/ansible/ansible/issues/64939). diff --git a/lib/ansible/errors/__init__.py b/lib/ansible/errors/__init__.py index d165720f866..437154dbba0 100644 --- a/lib/ansible/errors/__init__.py +++ b/lib/ansible/errors/__init__.py @@ -257,10 +257,12 @@ class AnsibleFileNotFound(AnsibleRuntimeError): self.file_name = file_name self.paths = paths + if message: + message += "\n" if self.file_name: - if message: - message += "\n" message += "Could not find or access '%s'" % to_text(self.file_name) + else: + message += "Could not find file" if self.paths and isinstance(self.paths, Sequence): searched = to_text('\n\t'.join(self.paths)) diff --git a/lib/ansible/plugins/action/include_vars.py b/lib/ansible/plugins/action/include_vars.py index 458c4ba7111..25cf9dccdbf 100644 --- a/lib/ansible/plugins/action/include_vars.py +++ b/lib/ansible/plugins/action/include_vars.py @@ -50,7 +50,9 @@ class ActionModule(ActionBase): self.source_dir = self._task.args.get('dir', None) self.source_file = self._task.args.get('file', None) if not self.source_dir and not self.source_file: - self.source_file = self._task.args.get('_raw_params').rstrip('\n') + self.source_file = self._task.args.get('_raw_params') + if self.source_file: + self.source_file = self.source_file.rstrip('\n') self.depth = self._task.args.get('depth', None) self.files_matching = self._task.args.get('files_matching', None) diff --git a/test/integration/targets/include_vars/tasks/main.yml b/test/integration/targets/include_vars/tasks/main.yml index 813801aa590..ab2442c6fa2 100644 --- a/test/integration/targets/include_vars/tasks/main.yml +++ b/test/integration/targets/include_vars/tasks/main.yml @@ -138,3 +138,17 @@ assert: that: - "service_name == 'my_custom_service'" + +- name: Check NoneType for raw params and file + include_vars: + file: "{{ lookup('first_found', possible_files, errors='ignore') }}" + vars: + possible_files: + - "does_not_exist.yml" + ignore_errors: True + register: include_with_non_existent_file + +- name: Verify that file and raw_params provide correct error message to user + assert: + that: + - "'Could not find file' in include_with_non_existent_file.message"