# How to download file only if it has changed

**URL:** <https://forum.ansible.com/t/how-to-download-file-only-if-it-has-changed/3333>\
**Category:** Get Help\
**Created:** [January 16, 2024, 12:01pm UTC](https://forum.ansible.com/t/how-to-download-file-only-if-it-has-changed/3333 "2024-01-16T12:01:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nemoo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nemoo/32/2000_2.png) [@nemoo](https://forum.ansible.com/u/nemoo)\
**Post date:** [January 16, 2024, 12:01pm UTC](https://forum.ansible.com/t/how-to-download-file-only-if-it-has-changed/3333/1 "2024-01-16T12:01:24Z")

</div>

Hi!

What is the idiomatic way to download a file ONLY IF:

- it does not yet exist locally
- the file has changed on the source url compared to the file that has already been downloaded

Should I write my own module for this or can it be done with standard commands?

Thanks!

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [January 16, 2024, 12:11pm UTC](https://forum.ansible.com/t/how-to-download-file-only-if-it-has-changed/3333/2 "2024-01-16T12:11:59Z")

</div>

Are you aware of the [ansible.builtin.get\_url module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/get_url_module.html)?

---

<div class="post-metadata">

**Author:** ![nemoo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nemoo/32/2000_2.png) [@nemoo](https://forum.ansible.com/u/nemoo)\
**Post date:** [January 16, 2024, 12:32pm UTC](https://forum.ansible.com/t/how-to-download-file-only-if-it-has-changed/3333/3 "2024-01-16T12:32:59Z")

</div>

Yes. The checksum parameter looks like it would be part of the solution. But that assumes I already have a checksum?  
How do I get it? How does ansible get the checksum of the updated remote file? How do I know if they are the same kind of checksums?

ChatGPT suggested something like this:

```auto
- name: Check the checksum of the remote file
  uri:
    url: "{{ archive_url }}"
    return_content: no
    method: HEAD
  register: remote_file

- name: Download the file if the checksum has changed
  get_url:
    url: "{{ archive_url }}"
    dest: "{{ java_home }}"
    checksum: "{{ remote_file.headers['Content-MD5'] }}"

```

When I try that, ansible tells me that the remote\_file dict does not have a headers entry.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [January 16, 2024, 1:00pm UTC](https://forum.ansible.com/t/how-to-download-file-only-if-it-has-changed/3333/4 "2024-01-16T13:00:21Z")

</div>

Doesn’t the `ansible.builtin.get_url` module do what is required when the [force parameter](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/get_url_module.html#parameter-force) is set to `true`?

---

<div class="post-metadata">

**Author:** ![nemoo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nemoo/32/2000_2.png) [@nemoo](https://forum.ansible.com/u/nemoo)\
**Post date:** [January 16, 2024, 1:46pm UTC](https://forum.ansible.com/t/how-to-download-file-only-if-it-has-changed/3333/5 "2024-01-16T13:46:20Z")

</div>

Thanks!

This seems to do what I need. Would be interesting why the docs say the force feature should only be used with small files.

I want to use it with Java SDKs. I wonder if 185 MB is small enough.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [January 16, 2024, 2:06pm UTC](https://forum.ansible.com/t/how-to-download-file-only-if-it-has-changed/3333/6 "2024-01-16T14:06:14Z")

</div>

I expect it will be because the whole file is downloaded in order to checksum it.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 15, 2024, 2:06pm UTC](https://forum.ansible.com/t/how-to-download-file-only-if-it-has-changed/3333/7 "2024-02-15T14:06:31Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
