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

使用ansible.builtin.copy模块时出现‘Not a directory’错误求助

问题原因及解决办法

核心错误原因

你遇到的错误本质是两个问题叠加导致:

1. 传递给content的是字典对象而非字符串

debug模块注册的awx_export是一个Ansible结果字典,包含msg、changed、failed等多个键值对,而ansible.builtin.copy的content参数要求传入字符串类型的内容。直接传入字典会导致模块内部处理逻辑混乱,误将目标文件路径当成目录检查,从而抛出"Not a directory"错误。

2. 目标路径的父目录可能不存在

如果backup_filepath指向的父目录(比如/tmp/ansible.in86oad1backup)尚未创建,copy模块无法自动创建父目录,也会触发路径相关的错误。

解决步骤

针对上述问题,按以下方式修改任务:

方案1:仅写入debug的msg内容

如果你只需要把debug输出的test写入文件,修改content参数为字典的msg字段:

---
- debug:
    msg: test
  register: awx_export

- name: Backup the AWX configuration
  ansible.builtin.copy:
    dest: "{{ backup_filepath }}.json"
    content: "{{ awx_export.msg }}"

方案2:写入完整的注册变量(JSON格式)

如果需要把整个awx_export字典的内容以JSON格式保存,使用to_json或to_nice_json过滤器转换为字符串:

---
- debug:
    msg: test
  register: awx_export

# 先确保目标父目录存在
- name: Create backup directory
  ansible.builtin.file:
    path: "{{ backup_filepath | dirname }}"
    state: directory
    mode: '0755'

- name: Backup the AWX configuration
  ansible.builtin.copy:
    dest: "{{ backup_filepath }}.json"
    content: "{{ awx_export | to_nice_json }}"

关键说明

  • to_nice_json会生成格式化的JSON字符串,可读性更强;to_json则生成紧凑的JSON。
  • 提前用file模块创建父目录,避免因目录不存在导致的路径错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 06:54:56