Ansible: How to copy files remote to remote

ansible

I need to copy file /etc/resolv.conf from a remote host and copy it on multiple remote hosts.

my hosts:
Ansible
ubuntu1-4

I want to copy this file from ubuntu1 to ubuntu2, ubuntu3 and ubuntu4
I tried the synchronize module but I can't/don't want to use rsync as a demon on ubuntu1-4.

Is there a better way than copying it on Ansible and from Ansible to ubuntu2 till 4?

Best Answer

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.