Ansible: How to copy files remote to remote

If you're just talking about a single file you don't need the synchronize module. You can grab a file from a remote host using the fetch module. Once you have the file on your local system, you can just use the copy module to distribute it to other hosts.

Something like:

- hosts: ubuntu1
  tasks:
    - fetch:
        src: /etc/resolv.conf
        dest: ./resolv.conf
        flat: true

- hosts: all:!ubuntu1
  tasks:
    - copy:
        src: ./resolv.conf
        dest: /etc/resolv.conf

While this works, a better solution would be to maintain the appropriate resolv.conf as part of your ansible configuration and distribute it to all hosts, rather than copying it from one remote to others.


In case, you want to copy a file from remote to the ansible control node - we can use the ansible fetch module. This works both for Linux and windows machine.

Tags:

Ansible