From 8789d7968d49fca031c91a44ce963969ad05e5bc Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 10 Jul 2020 15:37:52 -0400 Subject: [PATCH] try to capture better winrm/put_file error (#70508) * try to capture better winrm/put_file error fixes #70361 * Update lib/ansible/plugins/connection/winrm.py Co-authored-by: Abhijeet Kasurde Co-authored-by: Matt Davis --- changelogs/fragments/better_winrm_putfile_error.yml | 2 ++ lib/ansible/plugins/connection/winrm.py | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/better_winrm_putfile_error.yml diff --git a/changelogs/fragments/better_winrm_putfile_error.yml b/changelogs/fragments/better_winrm_putfile_error.yml new file mode 100644 index 00000000000..469bfd347c4 --- /dev/null +++ b/changelogs/fragments/better_winrm_putfile_error.yml @@ -0,0 +1,2 @@ +bugfixes: + - winrm - preserve winrm forensic data on put_file failures diff --git a/lib/ansible/plugins/connection/winrm.py b/lib/ansible/plugins/connection/winrm.py index ed5b58dd760..66c22fa515a 100644 --- a/lib/ansible/plugins/connection/winrm.py +++ b/lib/ansible/plugins/connection/winrm.py @@ -616,9 +616,16 @@ class Connection(ConnectionBase): if result.status_code != 0: raise AnsibleError(to_native(result.std_err)) - put_output = json.loads(result.std_out) - remote_sha1 = put_output.get("sha1") + try: + put_output = json.loads(result.std_out) + except ValueError: + # stdout does not contain a valid response + stderr = to_bytes(result.std_err, encoding='utf-8') + if stderr.startswith(b"#< CLIXML"): + stderr = _parse_clixml(stderr) + raise AnsibleError('winrm put_file failed; \nstdout: %s\nstderr %s' % (to_native(result.std_out), to_native(stderr))) + remote_sha1 = put_output.get("sha1") if not remote_sha1: raise AnsibleError("Remote sha1 was not returned")