Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unarchive: Keep stderr when pick_handler fails #76365

Merged
merged 6 commits into from Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,2 @@
bugfixes:
- unarchive - include the original error when a handler cannot manage the archive (https://github.com/ansible/ansible/issues/28977).
16 changes: 8 additions & 8 deletions lib/ansible/modules/unarchive.py
Expand Up @@ -391,9 +391,9 @@ def files_in_archive(self):
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
Expand Down Expand Up @@ -739,7 +739,7 @@ def can_handle_archive(self):
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):
Expand Down Expand Up @@ -788,7 +788,7 @@ def files_in_archive(self):
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
Expand Down Expand Up @@ -907,8 +907,8 @@ def can_handle_archive(self):
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
Expand Down Expand Up @@ -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():
Expand Down
1 change: 1 addition & 0 deletions test/integration/targets/unarchive/tasks/main.yml
Expand Up @@ -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
27 changes: 27 additions & 0 deletions test/integration/targets/unarchive/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