From 0a5cc80ce22debfe9879ab95e3d332522b3ea6ec Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Thu, 10 Jun 2021 00:55:48 -0400 Subject: [PATCH] slurp - handle error when path is a directory (#74930) --- changelogs/fragments/slurp-handle-error-with-dir.yml | 2 ++ lib/ansible/modules/slurp.py | 5 +++++ test/integration/targets/slurp/tasks/main.yml | 6 +++--- .../targets/slurp/tasks/test_unreadable.yml | 10 +++++----- 4 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 changelogs/fragments/slurp-handle-error-with-dir.yml diff --git a/changelogs/fragments/slurp-handle-error-with-dir.yml b/changelogs/fragments/slurp-handle-error-with-dir.yml new file mode 100644 index 00000000000..6b9812d4b8a --- /dev/null +++ b/changelogs/fragments/slurp-handle-error-with-dir.yml @@ -0,0 +1,2 @@ +bugfixes: +- slurp - handle error when ``path`` is a directory and not a file (https://github.com/ansible/ansible/pull/74930). diff --git a/lib/ansible/modules/slurp.py b/lib/ansible/modules/slurp.py index fd64860d32e..fdfe66a9a9e 100644 --- a/lib/ansible/modules/slurp.py +++ b/lib/ansible/modules/slurp.py @@ -81,6 +81,7 @@ import errno import os from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.common.text.converters import to_native def main(): @@ -101,6 +102,10 @@ def main(): module.fail_json(msg="file not found: %s" % source) elif e.errno == errno.EACCES: module.fail_json(msg="file is not readable: %s" % source) + elif e.errno == errno.EISDIR: + module.fail_json(msg="source is a directory and must be a file: %s" % source) + else: + module.fail_json(msg="unable to slurp file: %s" % to_native(e, errors='surrogate_then_replace')) data = base64.b64encode(source_content) diff --git a/test/integration/targets/slurp/tasks/main.yml b/test/integration/targets/slurp/tasks/main.yml index 97f3561e215..4c0e733953f 100644 --- a/test/integration/targets/slurp/tasks/main.yml +++ b/test/integration/targets/slurp/tasks/main.yml @@ -81,9 +81,9 @@ - name: check slurp directory result assert: that: - - "slurp_dir is failed" - - "slurp_dir.msg" - - "slurp_dir is not changed" + - slurp_dir is failed + - slurp_dir.msg is search('source is a directory and must be a file') + - slurp_dir is not changed - name: test slurp with missing argument action: slurp diff --git a/test/integration/targets/slurp/tasks/test_unreadable.yml b/test/integration/targets/slurp/tasks/test_unreadable.yml index 12a8e5c844e..f7b6bf8590c 100644 --- a/test/integration/targets/slurp/tasks/test_unreadable.yml +++ b/test/integration/targets/slurp/tasks/test_unreadable.yml @@ -8,13 +8,13 @@ - name: create unreadable file copy: content: "Hello, World!" - dest: /tmp/qux.txt - mode: 0600 + dest: "{{ output_dir }}/qux.txt" + mode: '0600' owner: root - name: test slurp unreadable file slurp: - src: '/tmp/qux.txt' + src: "{{ output_dir }}/qux.txt" register: slurp_unreadable_file become: true become_user: "{{ become_test_user }}" @@ -30,14 +30,14 @@ - name: create unreadable directory file: - path: /tmp/test_data + path: "{{ output_dir }}/test_data" state: directory mode: 0700 owner: root - name: test slurp unreadable directory slurp: - src: /tmp/test_data + src: "{{ output_dir }}/test_data" register: slurp_unreadable_dir become: true become_user: "{{ become_test_user }}"