slurp - better error handling for unreadable files and directories (#68608)
This commit is contained in:
parent
26827f5039
commit
e70fc88656
6 changed files with 75 additions and 6 deletions
2
changelogs/fragments/67340-slurp_error_message.yml
Normal file
2
changelogs/fragments/67340-slurp_error_message.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
bugfixes:
|
||||
- "slurp - Fix error messages for unreadable files and directories(https://github.com/ansible/ansible/issues/67340)."
|
|
@ -77,6 +77,7 @@ source:
|
|||
'''
|
||||
|
||||
import base64
|
||||
import errno
|
||||
import os
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
@ -91,13 +92,16 @@ def main():
|
|||
)
|
||||
source = module.params['src']
|
||||
|
||||
if not os.path.exists(source):
|
||||
module.fail_json(msg="file not found: %s" % source)
|
||||
if not os.access(source, os.R_OK):
|
||||
module.fail_json(msg="file is not readable: %s" % source)
|
||||
|
||||
try:
|
||||
os.stat(source)
|
||||
with open(source, 'rb') as source_fh:
|
||||
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)
|
||||
|
||||
data = base64.b64encode(source_content)
|
||||
|
||||
module.exit_json(content=data, source=source, encoding='base64')
|
||||
|
|
1
test/integration/targets/slurp/defaults/main.yml
Normal file
1
test/integration/targets/slurp/defaults/main.yml
Normal file
|
@ -0,0 +1 @@
|
|||
become_test_user: testuser
|
6
test/integration/targets/slurp/handlers/main.yml
Normal file
6
test/integration/targets/slurp/handlers/main.yml
Normal file
|
@ -0,0 +1,6 @@
|
|||
- name: remove test user and their home dir
|
||||
user:
|
||||
name: "{{ become_test_user }}"
|
||||
state: absent
|
||||
remove: yes
|
||||
force: yes
|
|
@ -96,3 +96,7 @@
|
|||
- "slurp_no_args is failed"
|
||||
- "slurp_no_args.msg"
|
||||
- "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'
|
||||
|
|
52
test/integration/targets/slurp/tasks/test_unreadable.yml
Normal file
52
test/integration/targets/slurp/tasks/test_unreadable.yml
Normal 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"
|
Loading…
Reference in a new issue