How to copy file between two ansible clients

Currently ansible control machine: A
Currently ansible working host: B
Another host in inventory: C
I want to copy a file from C to B

At first, I tried rsync without ssh.

I create a rsync daemon in C and try to add this in the host B ansible tasks:

  • name: copy file
    command: rsync -az user@hostC.com::module/TheFile /home/userinhostB

When ansible run this task, it hangs without the prompt “password”

But I can input this same command in the host B terminal and got the prompt “password”, and the file copy success.
$ rsync -az user@hostC.com::module/TheFile /home/userinhostB

Then I tried to use rsync over ssh, like this

  • name: copy file
    command: rsync -az -rsh “ssh” userinhostC@hostC.com:DirToTheFile/TheFile /home/userinhostB

Still hanging.

Can anyone tell me where did I do this wrong?
Or what should I do to copy the file.

Isn’t rsh a long opt? --rsh=ssh

-e, --rsh=COMMAND specify the remote shell to use

Try this:

command: rsync -avz -e ssh user@hostC.com:/module/TheFile /home/userinhostB

Using -v with rsync will at least let us know how far rsync actually gets in the execution, if at all.

Increase the verbosity on your ansible execution as well with -vvv

rsync is naturally going to ask for passwords unless you are forwarding SSH credentials.

You could consider a combination of fetch + copy.

I also recommend SSH agent for local rsyncs.