Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: How to copy files remote to remote

Tags:

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?

like image 793
Hall K Avatar asked Jan 05 '18 12:01

Hall K


People also ask

How do you copy multiple files into remote nodes by Ansible in a task?

You can use the copy module in your Ansible playbook. This module can copy one or more files to the remote system. But you need to use the item keyword in your playbook for multiple files as shown below.

How do I use rsync in Ansible?

Specify the rsync command to run on the remote host. See --rsync-path on the rsync man page. To specify the rsync command to run on the local host, you need to set this your task var ansible_rsync_path . Specify a --timeout for the rsync command in seconds.

How do I transfer my host from one host to another?

Step 1: Get login information for each server. Step 2: Get file path of the files to be copied. Step 3: Login to the second server and use scp command to copy files.


2 Answers

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.

like image 67
larsks Avatar answered Oct 24 '22 20:10

larsks


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.

like image 22
Namit Agarwal Avatar answered Oct 24 '22 22:10

Namit Agarwal