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

使用Ansible Playbook从Linux向Windows传文件失败,报错求助

问题解决步骤

核心错误原因

你的Playbook存在语法缩进错误,导致Ansible将win_copy识别为Play的顶级属性而非tasks内的任务,这是报错的直接原因。此外win_copy的参数格式也有误。

修正后的Playbook

- name: testing windows files
  hosts: "{{ hostname }}"  # 变量需用双引号包裹,避免语法解析问题
  
  tasks:
    - name: Copy a single file
      win_copy:
        src: /path/to/src/test.txt  # 控制节点(Linux)上的源文件路径
        dest: C:\Temp\renamed-test.txt  # 目标Windows主机的本地路径,无需加user@hostname

关键修正点

  • 缩进规范:所有任务必须缩进在tasks下方(保持与tasks同级的缩进深度),否则Ansible无法识别为任务项。
  • 变量包裹:hosts: {{ hostname }}需改为hosts: "{{ hostname }}",避免YAML语法解析异常。
  • dest参数格式:win_copy是在目标Windows主机上执行的模块,dest直接写Windows本地路径即可,无需添加user@hostname前缀(目标主机已由hosts指定)。
  • 模块选择:Windows主机必须用win_copy模块,不能用针对Linux的copy模块,后者不支持Windows环境。

额外检查项

  1. 确认Windows主机的Inventory配置正确,需包含WinRM连接参数:
    [windows]
    your-windows-host ansible_host=hostname.domain.name.com ansible_connection=winrm ansible_winrm_transport=ntlm ansible_winrm_server_cert_validation=ignore ansible_user=your-user ansible_password=your-pass
    
  2. 验证ansible.windows模块已正确安装在AWX执行节点上:
    ansible-galaxy collection install ansible.windows
    

内容的提问来源于stack exchange,提问作者Habbakuk barrera gomez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 06:05:25