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

Windows主机使用loop/with_items创建目录失败的原因咨询

问题原因分析与解决方案

这个报错的核心问题很明确:你把循环配置(loop或with_items)直接放在了Ansible Play(剧本)的顶层,而不是单个Task(任务)的内部。

Ansible的Play结构是有严格层级的:一个Play由hosts、tasks等顶层属性组成,所有具体的操作任务都必须放在tasks列表下。循环属性(loop/with_items)是Task级别的配置,只能属于某个具体的任务,不能直接作为Play的属性存在。你之前的代码应该是遗漏了tasks层级,导致Ansible把循环参数误判为Play的无效属性,从而抛出错误。

正确的Playbook写法

推荐使用loop的版本(Ansible 2.5+ 官方推荐)

---
- name: Play to create Windows directories
  hosts: your_windows_host  # 替换成你的Windows主机/主机组名称
  gather_facts: false
  tasks:
    - name: Creating folders DBF & INDEX
      win_file:
        path: "{{ item.path }}"
        state: directory
      loop:
        - {path: 'C:/XOFFICEDB/DBF'}
        - {path: 'C:/XOFFICEDB/INDEX'}

使用with_items的兼容版本

如果你习惯旧语法,也可以保留with_items,但同样要把它放在Task内部:

---
- name: Play to create Windows directories
  hosts: your_windows_host
  gather_facts: false
  tasks:
    - name: Creating folders DBF & INDEX
      win_file:
        path: "{{ item.path }}"
        state: directory
      with_items:
        - {path: 'C:/XOFFICEDB/DBF'}
        - {path: 'C:/XOFFICEDB/INDEX'}

另外补充个小提示:Ansible官方从2.5版本开始就推荐使用loop替代旧的with_*系列插件,loop的语法更统一直观,后续建议优先使用loop写法。

内容的提问来源于stack exchange,提问作者user12951310

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 08:03:14