How to copy file from one remote windows server to another remote windows server using Ansible?

130 Views Asked by At

I am trying to copy file from one windows server [serverA] to another windows server [serverB] using ansible but I am getting error that file doesn't exist. Both the windows servers are remote servers and I am using a linux server as my ansible controller.

I tried several ways like fetch, synchronize and copy modules of ansible but not getting any luck. Below is the latest code which I am using:

- name: Copy file between Windows servers
  hosts: serverA
  gather_facts: false

  tasks:
    - set_fact:
        source_path: "D:\\copy_file\\{{copy_file_name}}"
        dest_path: "D:\\new_copy\\{{copy_file_name}}"
    - name: Copy file to destination server
      win_copy:
        src: "{{ source_path }}"
        dest: "{{ dest_path }}"
        remote_src: yes
      delegate_to: serverB

Here {{copy_file_name}} value gets replaced with copyFile.txt. The below code gives me error like:

FAILED! => {
    "changed": false,
    "dest": "D:\\new_copy\\copyFile.txt",
    "msg": "Cannot copy src file: 'D:\\copy_file\\copyFile.txt' as it does not exist",
    "src": "D:\\copy_file\\copyFile.txt"
}

I crossed checked for the file and does exist on the serverA but still it gives me error as above. Interestingly when I remove delegate_to: serverB line then it works and copies the file from serverA src to serverA dest successfully but this is not what I want. I want a solution where it can copy from serverA src to serverB dest where both serverA and serverB are windows remote servers. Lastly as of now I am using a linux server as my ansible controller but going forward will use Ansible tower to achieve the same task. Can anyone help me in this ? Thanks!

0

There are 0 best solutions below