How to copy file remotely to remote windows servers via ansible

1

Shortly:

Ansible Control Machine : ServerC
Source Machine : ServerA
Destination Machine : ServerB

I want to send a file from serverA to serverB via serverC(ansible).I tried with win_copy but doesn't work because of remote servers.

---
-
  hosts: ServerA
  tasks:
    -
      delegate_to: ServerB
      name: "Transfer file from ServerA to ServerB"
      synchronize:
        dest: "C:\\Temp\\"
        mode: pull
        src: "C:\\Temp\\test"

Thank you

@imjoseangel When I edited as you said, I get like this error any idea?

PLAY [Sync Files] ***************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [ServerA]

TASK [Sync ServerA to ServerB] **************************************************************************************************************************************************************************************************************
fatal: [ServerA]: FAILED! => {"changed": false, "cmd": " C:\\Temp\\test.gz \\ServerB\\c$\\TEMP /purge /e", "dest": "\\ServerB\\c$\\TEMP", "flags": null, "msg": "2018/07/11 09:11:50 ERROR 123 (0x0000007B) Accessing Source Directory C:\\Temp\\test.gz\\", "output": ["", "-------------------------------------------------------------------------------", "   ROBOCOPY     ::     Robust File Copy for Windows                              ", "-------------------------------------------------------------------------------", "", "  Started : Wednesday, July 11, 2018 9:11:50 AM", "   Source : C:\\Temp\\test.gz\\", "     Dest : C:\\ServerB\\c$\\TEMP\\", "", "    Files : *.*", "\t    ", "  Options : *.* /S /E /DCOPY:DA /COPY:DAT /PURGE /R:1000000 /W:30 ", "", "------------------------------------------------------------------------------", "", "2018/07/11 09:11:50 ERROR 123 (0x0000007B) Accessing Source Directory C:\\Temp\\test.gz\\", "The filename, directory name, or volume label syntax is incorrect.", ""], "purge": true, "rc": 16, "recurse": true, "return_code": 16, "src": "C:\\Temp\\test.gz"}
 [WARNING]: Could not create retry file '/etc/ansible/test/test.retry'.         [Errno 13] Permission denied: u'/etc/ansible/test/test.retry'


PLAY RECAP **********************************************************************************************************************************************************************************************************************************
ServerA              : ok=1    changed=0    unreachable=0    failed=1

My recent yaml:

---
- name: Sync Files
  hosts: ServerA

  tasks:
  - name: Sync ServerA to ServerB
    win_robocopy:
      src: "C:\\Temp\\test.gz"
      dest: "\\ServerB\\c$\\TEMP"
      recurse: true
      purge: true
windows
ansible
asked on Stack Overflow Jun 29, 2018 by Dylan_ • edited Jul 11, 2018 by Dylan_

2 Answers

0

Do as follows:

  ---
  - name: Sync Files
    hosts: ServerA

  - name: Sync ServerA to ServerB
    win_robocopy:
      src: "C:\\Temp\\"
      dest: "\\ServerB\\c$\\Temp"
      recurse: yes
      purge: yes

Output:

"msg": "Files copied successfully!",
"output": [
    "",
    "-------------------------------------------------------------------------------",
    "   ROBOCOPY     ::     Robust File Copy for Windows                              ",
    "-------------------------------------------------------------------------------",
    "",
    "  Started : Thursday, July 5, 2018 12:02:15",
    "   Source : C:\\Temp\\",
    "     Dest : C:\\ServerB\\c$\\Temp\\",
    "",
    "    Files : *.*",
    "\t    ",
    "  Options : *.* /S /E /DCOPY:DA /COPY:DAT /PURGE /R:1000000 /W:30 ",
    "",
    "------------------------------------------------------------------------------",

Note: You don't have to delegate to ServerB as ServerA is doing the job.

answered on Stack Overflow Jul 5, 2018 by imjoseangel
0

It is not very clear in the title if you want to copy from windows to windows, so in case one wants to copy large files from the Ansible machine to a remote one, I wrote this article. Basically the steps are as follow:

  1. creates a network shared folder on the windows machine
  2. mounts that network folder from the linux machine
  3. transfers the files
  4. clean up everything

Step1 can be done like this (general idea, more info in the article itself):

- name: Add share on the remote
  win_share:
    name: ansible-work-dir
    description: for pushing ansible stuff
    path: "{{ destination_folder }}"
    list: yes
    full: "{{ ansible_user_id }}"

while step2 depends on the OS in use. For Ubuntu/Linux, I use the following

- name: Mount local folder
  shell: gio mount smb://{{ ansible_host }}/ansible-work-dir < {{ thefile.path }}
  delegate_to: localhost
  become: False
  register: command_result
  failed_when:
    - command_result.rc != 0
    - not ('Location is already mounted' in command_result.stderr)

The transfer of the file itself in Step3 becomes a file copy on the Ansible machine, as the remote share is mounted.

answered on Stack Overflow Jun 29, 2019 by Raffi

User contributions licensed under CC BY-SA 3.0