unarchive: Keep stderr when pick_handler fails (#76365)

* fixes #28977

* Include the original error message when pick_handler fails

* Add a test to unarchive a tar file with an invalid extra option

* add a changelog
pull/76617/head
Jean Raby 3 years ago committed by GitHub
parent fa617fcd7b
commit 594b11dc0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- unarchive - include the original error when a handler cannot manage the archive (https://github.com/ansible/ansible/issues/28977).

@ -391,9 +391,9 @@ class ZipArchive(object):
break
if not exclude_flag:
self._files_in_archive.append(to_native(member))
except Exception:
except Exception as e:
archive.close()
raise UnarchiveError('Unable to list files in the archive')
raise UnarchiveError('Unable to list files in the archive: %s' % to_native(e))
archive.close()
return self._files_in_archive
@ -739,7 +739,7 @@ class ZipArchive(object):
rc, out, err = self.module.run_command(cmd)
if rc == 0:
return True, None
return False, 'Command "%s" could not handle archive.' % self.cmd_path
return False, 'Command "%s" could not handle archive: %s' % (self.cmd_path, err)
class TgzArchive(object):
@ -788,7 +788,7 @@ class TgzArchive(object):
locale = get_best_parsable_locale(self.module)
rc, out, err = self.module.run_command(cmd, cwd=self.b_dest, environ_update=dict(LANG=locale, LC_ALL=locale, LC_MESSAGES=locale, LANGUAGE=locale))
if rc != 0:
raise UnarchiveError('Unable to list files in the archive')
raise UnarchiveError('Unable to list files in the archive: %s' % err)
for filename in out.splitlines():
# Compensate for locale-related problems in gtar output (octal unicode representation) #11348
@ -907,8 +907,8 @@ class TgzArchive(object):
try:
if self.files_in_archive:
return True, None
except UnarchiveError:
return False, 'Command "%s" could not handle archive.' % self.cmd_path
except UnarchiveError as e:
return False, 'Command "%s" could not handle archive: %s' % (self.cmd_path, to_native(e))
# Errors and no files in archive assume that we weren't able to
# properly unarchive it
return False, 'Command "%s" found no files in archive. Empty archive files are not supported.' % self.cmd_path
@ -959,8 +959,8 @@ def pick_handler(src, dest, file_args, module):
if can_handle:
return obj
reasons.add(reason)
reason_msg = ' '.join(reasons)
module.fail_json(msg='Failed to find handler for "%s". Make sure the required command to extract the file is installed. %s' % (src, reason_msg))
reason_msg = '\n'.join(reasons)
module.fail_json(msg='Failed to find handler for "%s". Make sure the required command to extract the file is installed.\n%s' % (src, reason_msg))
def main():

@ -18,3 +18,4 @@
- import_tasks: test_download.yml
- import_tasks: test_unprivileged_user.yml
- import_tasks: test_different_language_var.yml
- import_tasks: test_invalid_options.yml

@ -0,0 +1,27 @@
- name: create our tar unarchive destination
file:
path: '{{remote_tmp_dir}}/test-unarchive-tar'
state: directory
- name: unarchive a tar file with an invalid option
unarchive:
src: '{{remote_tmp_dir}}/test-unarchive.tar'
dest: '{{remote_tmp_dir}}/test-unarchive-tar'
remote_src: yes
extra_opts:
- "--invalid-éxtra-optら"
ignore_errors: yes
register: unarchive
- name: verify that the invalid option is in the error message
assert:
that:
- "unarchive is failed"
- "unarchive['msg'] is search(msg)"
vars:
msg: "Unable to list files in the archive: /.*/(tar|gtar): unrecognized option '--invalid-éxtra-optら'"
- name: remove our tar unarchive destination
file:
path: '{{remote_tmp_dir}}/test-unarchive-tar'
state: absent
Loading…
Cancel
Save