How to Copy file1 from server A to server B; then Copy file2 from server C to D in Ansible ?

I want to copy files from one server to another using Ansible. Below is the example

Server A ------> Server B
Server C ------> Server D

I have my one file on Server A and specifically want to copy that file on to the server B, and same for server C to D. Folder to save files to the destination is the same. I can do it for 2 or 3 hosts. But how can I create dynamically for let us say 100 nodes to copy particular single file assign to desired server only.

You should start with a data definition that is consumable by Ansible. I’m still not quite clear on what you want to do with 100 servers but I’ll start by assuming “For each server, copy one file, unique to server X onto server Y. Destination path = source path.”

In this example I would create an inventory like this:

`

[source_hosts]
host1 file_path=/etc/… dest_host=another_hostname

`

With that data structure you could create a Task like

`

  • hosts: source_hosts
    tasks:
  • fetch: src={{src_path}} dest=/tmp/…
  • delegate_to:“{{dest_host}}”
    copy: src={{/tmp/…}} dest={{file_path}}

`

Caveat Emptor: I did not try running this.