slurp - handle error when path is a directory (#74930)

This commit is contained in:
Sam Doran 2021-06-10 00:55:48 -04:00 committed by GitHub
parent 36287e9810
commit 0a5cc80ce2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 8 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- slurp - handle error when ``path`` is a directory and not a file (https://github.com/ansible/ansible/pull/74930).

View file

@ -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)

View file

@ -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

View file

@ -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 }}"