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
?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With