diff --git a/lib/ansible/modules/net_tools/basics/get_url.py b/lib/ansible/modules/net_tools/basics/get_url.py index 2ec50284ea..2c8ce8375a 100644 --- a/lib/ansible/modules/net_tools/basics/get_url.py +++ b/lib/ansible/modules/net_tools/basics/get_url.py @@ -566,12 +566,6 @@ def main(): filename = url_filename(info['url']) dest = os.path.join(dest, filename) - # If the remote URL exists, we're done with check mode - if module.check_mode: - os.remove(tmpsrc) - result['changed'] = True - module.exit_json(msg=info.get('msg', ''), **result) - # raise an error if there is no tmpsrc file if not os.path.exists(tmpsrc): os.remove(tmpsrc) @@ -599,6 +593,13 @@ def main(): os.remove(tmpsrc) module.fail_json(msg="Destination %s is not writable" % (os.path.dirname(dest)), **result) + if module.check_mode: + if os.path.exists(tmpsrc): + os.remove(tmpsrc) + result['changed'] = ('checksum_dest' not in result or + result['checksum_src'] != result['checksum_dest']) + module.exit_json(msg=info.get('msg', ''), **result) + backup_file = None if result['checksum_src'] != result['checksum_dest']: try: diff --git a/test/integration/targets/get_url/tasks/main.yml b/test/integration/targets/get_url/tasks/main.yml index 40a5b683d4..829d55c63f 100644 --- a/test/integration/targets/get_url/tasks/main.yml +++ b/test/integration/targets/get_url/tasks/main.yml @@ -268,6 +268,19 @@ that: - result is not changed +- name: test checksum match in check mode + get_url: + url: 'https://{{ httpbin_host }}/get' + dest: '{{ remote_tmp_dir }}/test' + checksum: 'sha256:7036ede810fad2b5d2e7547ec703cae8da61edbba43c23f9d7203a0239b765c4.' + check_mode: True + register: result + +- name: Assert that check mode was green + assert: + that: + - result is not changed + # https://github.com/ansible/ansible/issues/27617 - name: set role facts