From 872e92ffe5a295124f32d181a690583a80ea3e03 Mon Sep 17 00:00:00 2001 From: Hugo Cartwright Date: Fri, 8 Nov 2019 23:32:58 +0100 Subject: [PATCH] Refactor checksum variables syntaxfor better clarity - is now --- .../modules/net_tools/basics/get_url.py | 52 +++++++++---------- .../targets/get_url/tasks/main.yml | 7 +-- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/lib/ansible/modules/net_tools/basics/get_url.py b/lib/ansible/modules/net_tools/basics/get_url.py index 3c84cb60e96dd3..f7d09fb524afb9 100644 --- a/lib/ansible/modules/net_tools/basics/get_url.py +++ b/lib/ansible/modules/net_tools/basics/get_url.py @@ -529,37 +529,37 @@ def main(): module.fail_json(msg='The checksum format is invalid', **result) if not dest_is_dir and os.path.exists(dest): - checksum_mismatch = False + checksum_match = True - # If the download is not forced and there is a checksum, allow - # checksum match to skip the download. - if not force and checksum != '': - destination_checksum = module.digest_from_file(dest, algorithm) + if not force: + # If the download is not forced and there is a checksum, allow + # checksum match to skip the download. + if checksum != '': + destination_checksum = module.digest_from_file(dest, algorithm) - if checksum != destination_checksum: - checksum_mismatch = True + if checksum != destination_checksum: + checksum_match = False - # Not forcing redownload, unless checksum does not match - if not force and checksum and not checksum_mismatch: # Not forcing redownload, unless checksum does not match # allow file attribute changes - module.params['path'] = dest - file_args = module.load_file_common_arguments(module.params) - file_args['path'] = dest - result['changed'] = module.set_fs_attributes_if_different(file_args, False) - if result['changed']: - module.exit_json(msg="file already exists but file attributes changed", **result) - module.exit_json(msg="file already exists", **result) - - # If the file already exists, prepare the last modified time for the - # request. - mtime = os.path.getmtime(dest) - last_mod_time = datetime.datetime.utcfromtimestamp(mtime) - - # If the checksum does not match we have to force the download - # because last_mod_time may be newer than on remote - if checksum_mismatch: - force = True + if checksum and checksum_match: + module.params['path'] = dest + file_args = module.load_file_common_arguments(module.params) + file_args['path'] = dest + result['changed'] = module.set_fs_attributes_if_different(file_args, False) + if result['changed']: + module.exit_json(msg="file already exists but file attributes changed", **result) + module.exit_json(msg="file already exists", **result) + + # If the file already exists, prepare the last modified time for the + # request. + mtime = os.path.getmtime(dest) + last_mod_time = datetime.datetime.utcfromtimestamp(mtime) + + # If the checksum does not match we have to force the download + # because last_mod_time may be newer than on remote + if not checksum_match: + force = True # download to tmpsrc start = datetime.datetime.utcnow() diff --git a/test/integration/targets/get_url/tasks/main.yml b/test/integration/targets/get_url/tasks/main.yml index eceb431ee3fb16..8ee67c7e32f018 100644 --- a/test/integration/targets/get_url/tasks/main.yml +++ b/test/integration/targets/get_url/tasks/main.yml @@ -482,17 +482,18 @@ - name: Define test file for force==no shouldnt redownload dsturl set_fact: - geturl_dstfile: "{{ remote_tmp_dir }}/force_no_redownload_test.txt" + geturl_dstfile: "{{ remote_tmp_dir }}/test_force_no" - name: Create dstfile by getting dsturl get_url: - url: 'https://{{ httpbin_host }}/html' + url: 'https://{{ httpbin_host }}/get' dest: "{{ geturl_dstfile }}" + force: yes register: download_check - name: Run get_url a second time but this time with dstfile already existing and force==no get_url: - url: 'https://{{ httpbin_host }}/html' + url: 'https://{{ httpbin_host }}/get' dest: "{{ geturl_dstfile }}" force: no register: result