diff --git a/changelogs/fragments/53491-uri-dont-write-file-after-failure.yaml b/changelogs/fragments/53491-uri-dont-write-file-after-failure.yaml new file mode 100644 index 00000000000..f1ffdb986fb --- /dev/null +++ b/changelogs/fragments/53491-uri-dont-write-file-after-failure.yaml @@ -0,0 +1,2 @@ +bugfixes: + - uri - do not write the file after failure (https://github.com/ansible/ansible/issues/53491) diff --git a/lib/ansible/modules/net_tools/basics/uri.py b/lib/ansible/modules/net_tools/basics/uri.py index c538db84ce5..a0b6b1c0382 100644 --- a/lib/ansible/modules/net_tools/basics/uri.py +++ b/lib/ansible/modules/net_tools/basics/uri.py @@ -599,12 +599,11 @@ def main(): dict_headers, socket_timeout) resp['elapsed'] = (datetime.datetime.utcnow() - start).seconds resp['status'] = int(resp['status']) + resp['changed'] = False # Write the file out if requested if dest is not None: - if resp['status'] == 304: - resp['changed'] = False - else: + if resp['status'] in status_code and resp['status'] != 304: write_file(module, url, dest, content, resp) # allow file attribute changes resp['changed'] = True @@ -613,8 +612,6 @@ def main(): file_args['path'] = dest resp['changed'] = module.set_fs_attributes_if_different(file_args, resp['changed']) resp['path'] = dest - else: - resp['changed'] = False # Transmogrify the headers, replacing '-' with '_', since variables don't # work with dashes. diff --git a/test/integration/targets/uri/tasks/main.yml b/test/integration/targets/uri/tasks/main.yml index 3458fc437f8..9b8c23a27ca 100644 --- a/test/integration/targets/uri/tasks/main.yml +++ b/test/integration/targets/uri/tasks/main.yml @@ -514,6 +514,31 @@ that: - result.json.json[0] == 'JSON Test Pattern pass1' +- name: Create a testing file + copy: + content: "content" + dest: "{{ output_dir }}/output" + +- name: Download a file from non existing location + uri: + url: http://does/not/exist + dest: "{{ output_dir }}/output" + ignore_errors: yes + +- name: Save testing file's output + command: "cat {{ output_dir }}/output" + register: file_out + +- name: Test the testing file was not overwritten + assert: + that: + - "'content' in file_out.stdout" + +- name: Clean up + file: + dest: "{{ output_dir }}/output" + state: absent + - name: Test follow_redirects=none import_tasks: redirect-none.yml