12

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?

the
  • 21,007
  • 11
  • 68
  • 101
Hall K
  • 164
  • 1
  • 1
  • 9
  • Duplicate https://stackoverflow.com/q/25505146/2947502 – techraf Jan 05 '18 at 12:15
  • yes i know this link, but they use synchronize. – Hall K Jan 05 '18 at 12:20
  • It doesn't change the fact that this is the method to do it. Ansible was not written to copy files between target machines. synchronize module is already an exception (breaching concepts) in Ansible modules. And even assuming someone has written another one with the same functionality as `synchronize`, that answer should be posted under that other question, not here. This one should be closed, but I ran out of closed votes for today. – techraf Jan 05 '18 at 12:42
  • The title of this one is misleading for ansible "remote to remote" in this context is on the same host. As such if a new user is searching for "copy files remote to remote" they will get this when they are really after ` tasks: - copy: src: /home/resolv.conf dest: /etc/resolv.conf remote_src: yes ` Please consider changing the title to something like "Ansible: How to copy files between remote hosts" or "Ansible: How to synchronize files between remote hosts" – Timothy c Jun 19 '19 at 14:11

2 Answers2

12

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.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
larsks
  • 277,717
  • 41
  • 399
  • 399
  • @MichallKo How does it meet the requirement you specified "*There is better way than copy it on Ansible and from Ansible to ubuntu2-4?*"? – techraf Jan 05 '18 at 20:50
  • 5
    This isn't the answer for "remote to remote". This is an answer for "remote to host to remote". It may gave worked for OP, but he or she asked the wrong question. – Nicholas Martinez Apr 26 '18 at 16:33
  • 1
    thanks, this is exactly what I was looking for, i prefer ansible to do the copying rather than opening ssh between hosts – tweeks200 Feb 14 '19 at 01:44
  • 1
    @NicholasMartinez This is still remote to remote, but through the controller. A true remote to remote copy would require setting up ssh access between the remote hosts and is not desirable, especially if it is needed for this purpose alone. It may actually be possible to copy over an ad-hoc port (e.g., using rsync or netcat), but corporate environments usually have ACLs restricting ports, so this should be the most reliable method. – haridsv Mar 01 '19 at 04:58
1

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.

Namit Agarwal
  • 81
  • 1
  • 6