In Ansible, I want to fetch a remote file, using the Fetch module, only if the file does not already exist at the destination. How could I do this? It doesn't seem like Fetch has an existing parameter to check for this.
My task:
- name: Fetch file from remote host
fetch:
dest: /path/filename.{{groups['host'][0]}}.pub
src: /path/filename.pub
flat: yes
delegate_to: "{{groups['host'][0]}}"
Thanks in advance!
Q: "Fetch module, only if the file does not already exist at the destination."
A: The fetch
module is idempotent and downloads only changed file. For example the first run of the playbook
- hosts: test_01
tasks:
- fetch:
dest: /tmp/fetched
src: /etc/passwd
flat: yes
gives
TASK [fetch] *********************************************************************************************************************************
changed: [test_01]
PLAY RECAP ***********************************************************************************************************************************
test_01 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Repeated run of the same playbook gives
TASK [fetch] *********************************************************************************************************************************
ok: [test_01]
PLAY RECAP ***********************************************************************************************************************************
test_01 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The source shows that the file will be fetched only if the remote and local checksums differ.