How to download file only if it has changed

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!

Are you aware of the ansible.builtin.get_url module?

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:

- 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.

Doesn’t the ansible.builtin.get_url module do what is required when the force parameter is set to true?

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.

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