You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible win_robocopy模块无法将远程共享文件夹设置为源路径(src)

Ansible win_robocopy模块无法将远程共享文件夹设置为源路径(src)

我完全理解你被这个问题折腾一周有多崩溃——用Ansible操作Windows远程共享时,路径转义和凭据传递的坑确实容易让人挠头。咱们一步步拆解问题,找到解决方案:

核心问题:路径转义处理不当

你遇到的"\\\\192.168.195.130\\share\\builds does not exist!"报错,本质是YAML对反斜杠的转义逻辑导致实际传递给robocopy的路径出了问题。Windows路径的反斜杠在YAML里需要特殊处理,这里有两个更可靠的写法:

方案1:用正斜杠替代反斜杠

Windows系统本身支持用正斜杠作为路径分隔符,这样完全不需要处理转义,直接写:

src: '//192.168.195.130/share/builds'

方案2:正确转义反斜杠

如果你坚持用反斜杠,在YAML的单引号字符串里,需要把每个反斜杠写成两个(因为单引号里的反斜杠只有连续两个才会被解析为一个实际的反斜杠):

src: '\\\\192.168.195.130\\share\\builds'

确认凭据配置的正确性

你的become配置方向是对的(用runas+new_credentials来传递访问共享的凭据),但有几个细节要注意:

  • ansible_become_user最好带上域名或源服务器名前缀,比如SOURCE-SERVER\\username或DOMAIN\\username,避免凭据解析歧义。
  • become_flags可以简化为一行写法,更清晰:
    become_flags: logon_type=new_credentials logon_flags=netcredentials_only
    

修正后的完整Playbook示例

- name: Copy Builds from Remote Share
  win_robocopy:
    src: '//192.168.195.130/share/builds'
    dest: "{{ destination }}"
    flags: /e
  become: true
  become_method: runas
  become_flags: logon_type=new_credentials logon_flags=netcredentials_only
  vars:
    ansible_become_user: "SOURCE-SERVER\\admin"  # 替换为有权限访问共享的用户
    ansible_become_pass: "{{ ansible_password }}"

额外排查步骤

如果还是报错,先排除Ansible之外的问题:

  • 在目标服务器上,手动用相同凭据运行robocopy命令:
    runas /user:SOURCE-SERVER\admin "robocopy //192.168.195.130/share/builds C:\Install\available /e"
    
    看是否能成功复制,以此确认共享权限、网络连通性(比如SMB端口445是否开放)没问题。
  • 检查源共享的共享权限和NTFS权限,确保指定用户有读取权限。

备注:内容来源于stack exchange,提问作者Ken Anderson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 09:14:29