[stable-2.7] Fix logic to not re-download existing files when force=no (#45495) (#45509)

* [stable-2.7] Fix logic to not re-download existing files when force=no (#45495)

* Fix logic to not re-download existing files when force=no. Fixes #45491

* Reduce logic complexity.
(cherry picked from commit 5785de582f)

Co-authored-by: Matt Martz <matt@sivel.net>

* Backport of get_url fix cannot use result

result was only added in 2.8+
pull/45570/head
Matt Martz 6 years ago committed by Toshio Kuratomi
parent c8fe1126a1
commit 99171a9c6f

@ -0,0 +1,2 @@
bugfixes:
- get_url - Don't re-download files unnecessarily when force=no (https://github.com/ansible/ansible/issues/45491)

@ -466,7 +466,11 @@ def main():
if not force and checksum != '': if not force and checksum != '':
destination_checksum = module.digest_from_file(dest, algorithm) destination_checksum = module.digest_from_file(dest, algorithm)
if checksum == destination_checksum: if checksum != destination_checksum:
checksum_mismatch = True
# Not forcing redownload, unless checksum does not match
if not force and not checksum_mismatch:
# Not forcing redownload, unless checksum does not match # Not forcing redownload, unless checksum does not match
# allow file attribute changes # allow file attribute changes
module.params['path'] = dest module.params['path'] = dest
@ -477,8 +481,6 @@ def main():
module.exit_json(msg="file already exists but file attributes changed", dest=dest, url=url, changed=changed) module.exit_json(msg="file already exists but file attributes changed", dest=dest, url=url, changed=changed)
module.exit_json(msg="file already exists", dest=dest, url=url, changed=changed) module.exit_json(msg="file already exists", dest=dest, url=url, changed=changed)
checksum_mismatch = True
# If the file already exists, prepare the last modified time for the # If the file already exists, prepare the last modified time for the
# request. # request.
mtime = os.path.getmtime(dest) mtime = os.path.getmtime(dest)

@ -241,7 +241,7 @@
# https://github.com/ansible/ansible/issues/29614 # https://github.com/ansible/ansible/issues/29614
- name: Change mode on an already downloaded file and specify checksum - name: Change mode on an already downloaded file and specify checksum
get_url: get_url:
url: 'https://{{ httpbin_host }}/' url: 'https://{{ httpbin_host }}/get'
dest: '{{ output_dir }}/test' dest: '{{ output_dir }}/test'
checksum: 'sha256:7036ede810fad2b5d2e7547ec703cae8da61edbba43c23f9d7203a0239b765c4.' checksum: 'sha256:7036ede810fad2b5d2e7547ec703cae8da61edbba43c23f9d7203a0239b765c4.'
mode: '0775' mode: '0775'
@ -257,6 +257,17 @@
- result is changed - result is changed
- "stat_result.stat.mode == '0775'" - "stat_result.stat.mode == '0775'"
- name: Get a file that already exists
get_url:
url: 'https://{{ httpbin_host }}/get'
dest: '{{ output_dir }}/test'
register: result
- name: Assert that we didn't re-download unnecessarily
assert:
that:
- result is not changed
# https://github.com/ansible/ansible/issues/27617 # https://github.com/ansible/ansible/issues/27617
- name: set role facts - name: set role facts

Loading…
Cancel
Save