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 import os
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
def main(): def main():
@ -101,6 +102,10 @@ def main():
module.fail_json(msg="file not found: %s" % source) module.fail_json(msg="file not found: %s" % source)
elif e.errno == errno.EACCES: elif e.errno == errno.EACCES:
module.fail_json(msg="file is not readable: %s" % source) 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) data = base64.b64encode(source_content)

View file

@ -81,9 +81,9 @@
- name: check slurp directory result - name: check slurp directory result
assert: assert:
that: that:
- "slurp_dir is failed" - slurp_dir is failed
- "slurp_dir.msg" - slurp_dir.msg is search('source is a directory and must be a file')
- "slurp_dir is not changed" - slurp_dir is not changed
- name: test slurp with missing argument - name: test slurp with missing argument
action: slurp action: slurp

View file

@ -8,13 +8,13 @@
- name: create unreadable file - name: create unreadable file
copy: copy:
content: "Hello, World!" content: "Hello, World!"
dest: /tmp/qux.txt dest: "{{ output_dir }}/qux.txt"
mode: 0600 mode: '0600'
owner: root owner: root
- name: test slurp unreadable file - name: test slurp unreadable file
slurp: slurp:
src: '/tmp/qux.txt' src: "{{ output_dir }}/qux.txt"
register: slurp_unreadable_file register: slurp_unreadable_file
become: true become: true
become_user: "{{ become_test_user }}" become_user: "{{ become_test_user }}"
@ -30,14 +30,14 @@
- name: create unreadable directory - name: create unreadable directory
file: file:
path: /tmp/test_data path: "{{ output_dir }}/test_data"
state: directory state: directory
mode: 0700 mode: 0700
owner: root owner: root
- name: test slurp unreadable directory - name: test slurp unreadable directory
slurp: slurp:
src: /tmp/test_data src: "{{ output_dir }}/test_data"
register: slurp_unreadable_dir register: slurp_unreadable_dir
become: true become: true
become_user: "{{ become_test_user }}" become_user: "{{ become_test_user }}"