如何通过Ansible将文件远程复制到远程Windows服务器

时间:2018-06-29 08:19:40

标签: windows ansible

简短地:

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

我想通过serverC(ansible)将文件从serverA发送到serverB。我尝试使用win_copy,但由于远程服务器而无法正常工作。

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

谢谢

@imjoseangel当我按照您所说的进行编辑时,我对这个错误有所了解吗?

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

我最近的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

2 个答案:

答案 0 :(得分:0)

执行以下操作:

  ---
  - name: Sync Files
    hosts: ServerA

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

输出:

"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 ",
    "",
    "------------------------------------------------------------------------------",
  

注意:您不必在ServerA进行工作时委托给ServerB。

答案 1 :(得分:0)

如果您要从Windows复制到Windows,标题中不是很清楚,因此,如果有人想将大型文件从Ansible机器复制到远程机器,我写了this article。基本上,步骤如下:

  1. 在Windows计算机上创建网络共享文件夹
  2. 从linux机器上安装该网络文件夹
  3. 传输文件
  4. 清理一切

Step1可以这样完成(总体思路,文章中的更多信息):

- 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 }}"

而step2取决于使用的操作系统。对于Ubuntu / Linux,我使用以下

- 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)

在安装远程共享时,在步骤3中文件本身的传输将变成Ansible机器上的文件副本。