slurp - better error handling for unreadable files and directories (#68608)

This commit is contained in:
Hideki Saito 2021-06-04 23:38:23 +09:00 committed by GitHub
parent 26827f5039
commit e70fc88656
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 6 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- "slurp - Fix error messages for unreadable files and directories(https://github.com/ansible/ansible/issues/67340)."

View file

@ -77,6 +77,7 @@ source:
''' '''
import base64 import base64
import errno
import os import os
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
@ -91,13 +92,16 @@ def main():
) )
source = module.params['src'] source = module.params['src']
if not os.path.exists(source): try:
module.fail_json(msg="file not found: %s" % source) os.stat(source)
if not os.access(source, os.R_OK): with open(source, 'rb') as source_fh:
module.fail_json(msg="file is not readable: %s" % source) source_content = source_fh.read()
except (IOError, OSError) as e:
if e.errno == errno.ENOENT:
module.fail_json(msg="file not found: %s" % source)
elif e.errno == errno.EACCES:
module.fail_json(msg="file is not readable: %s" % source)
with open(source, 'rb') as source_fh:
source_content = source_fh.read()
data = base64.b64encode(source_content) data = base64.b64encode(source_content)
module.exit_json(content=data, source=source, encoding='base64') module.exit_json(content=data, source=source, encoding='base64')

View file

@ -0,0 +1 @@
become_test_user: testuser

View file

@ -0,0 +1,6 @@
- name: remove test user and their home dir
user:
name: "{{ become_test_user }}"
state: absent
remove: yes
force: yes

View file

@ -96,3 +96,7 @@
- "slurp_no_args is failed" - "slurp_no_args is failed"
- "slurp_no_args.msg" - "slurp_no_args.msg"
- "slurp_no_args is not changed" - "slurp_no_args is not changed"
# Ensure unreadable file and directory handling and error messages
# https://github.com/ansible/ansible/issues/67340
- import_tasks: 'test_unreadable.yml'

View file

@ -0,0 +1,52 @@
- name: create test user
user:
name: "{{ become_test_user }}"
create_home: yes
notify:
- "remove test user and their home dir"
- name: create unreadable file
copy:
content: "Hello, World!"
dest: /tmp/qux.txt
mode: 0600
owner: root
- name: test slurp unreadable file
slurp:
src: '/tmp/qux.txt'
register: slurp_unreadable_file
become: true
become_user: "{{ become_test_user }}"
become_method: su
ignore_errors: true
- name: check slurp unreadable file result
assert:
that:
- "slurp_unreadable_file is failed"
- "slurp_unreadable_file.msg is regex('^file is not readable:')"
- "slurp_unreadable_file is not changed"
- name: create unreadable directory
file:
path: /tmp/test_data
state: directory
mode: 0700
owner: root
- name: test slurp unreadable directory
slurp:
src: /tmp/test_data
register: slurp_unreadable_dir
become: true
become_user: "{{ become_test_user }}"
become_method: su
ignore_errors: true
- name: check slurp unreadable directory result
assert:
that:
- "slurp_unreadable_dir is failed"
- "slurp_unreadable_dir.msg is regex('^file is not readable:')"
- "slurp_unreadable_dir is not changed"