From 80f09efd03c934ba9780f141adfdb0d7aad18b12 Mon Sep 17 00:00:00 2001 From: Jack Date: Mon, 1 Jun 2020 21:55:38 +0800 Subject: [PATCH] do not return the body even if it failed (#69706) * do not return the body even if it failed * add some tests for this and rebase * import test task * ignore_errors when fails Co-authored-by: Jack Zhang --- .../fragments/21003-uri-return-content.yml | 3 ++ lib/ansible/modules/uri.py | 7 ++- test/integration/targets/uri/tasks/main.yml | 3 ++ .../targets/uri/tasks/return-content.yml | 49 +++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/21003-uri-return-content.yml create mode 100644 test/integration/targets/uri/tasks/return-content.yml diff --git a/changelogs/fragments/21003-uri-return-content.yml b/changelogs/fragments/21003-uri-return-content.yml new file mode 100644 index 00000000000..ebdd1f3a65f --- /dev/null +++ b/changelogs/fragments/21003-uri-return-content.yml @@ -0,0 +1,3 @@ +bugfixes: +- uri - Don't return the body even if it failed + (https://github.com/ansible/ansible/issues/21003) diff --git a/lib/ansible/modules/uri.py b/lib/ansible/modules/uri.py index fec84d83203..21d5bec8689 100644 --- a/lib/ansible/modules/uri.py +++ b/lib/ansible/modules/uri.py @@ -75,7 +75,7 @@ options: return_content: description: - Whether or not to return the body of the response as a "content" key in - the dictionary result. + the dictionary result no matter it succeeded or failed. - Independently of this option, if the reported Content-type is "application/json", then the JSON is always loaded into a key called C(json) in the dictionary results. type: bool @@ -739,7 +739,10 @@ def main(): if resp['status'] not in status_code: uresp['msg'] = 'Status code was %s and not %s: %s' % (resp['status'], status_code, uresp.get('msg', '')) - module.fail_json(content=u_content, **uresp) + if return_content: + module.fail_json(content=u_content, **uresp) + else: + module.fail_json(**uresp) elif return_content: module.exit_json(content=u_content, **uresp) else: diff --git a/test/integration/targets/uri/tasks/main.yml b/test/integration/targets/uri/tasks/main.yml index de41e0117cd..409607af32b 100644 --- a/test/integration/targets/uri/tasks/main.yml +++ b/test/integration/targets/uri/tasks/main.yml @@ -595,3 +595,6 @@ - name: Check unexpected failures import_tasks: unexpected-failures.yml + +- name: Check return-content + import_tasks: return-content.yml diff --git a/test/integration/targets/uri/tasks/return-content.yml b/test/integration/targets/uri/tasks/return-content.yml new file mode 100644 index 00000000000..5a9b97e6411 --- /dev/null +++ b/test/integration/targets/uri/tasks/return-content.yml @@ -0,0 +1,49 @@ +- name: Test when return_content is yes + uri: + url: https://{{ httpbin_host }}/get + return_content: yes + register: result + +- name: Assert content exists when return_content is yes and request succeeds + assert: + that: + - result is successful + - "'content' in result" + +- name: Test when return_content is yes + uri: + url: http://does/not/exist + return_content: yes + register: result + ignore_errors: true + +- name: Assert content exists when return_content is yes and request fails + assert: + that: + - result is failed + - "'content' in result" + +- name: Test when return_content is no + uri: + url: https://{{ httpbin_host }}/get + return_content: no + register: result + +- name: Assert content does not exist when return_content is no and request succeeds + assert: + that: + - result is successful + - "'content' not in result" + +- name: Test when return_content is no + uri: + url: http://does/not/exist + return_content: no + register: result + ignore_errors: true + +- name: Assert content does not exist when return_content is no and request fails + assert: + that: + - result is failed + - "'content' not in result" \ No newline at end of file