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

Ansible角色目录文件列取问题:find模块路径无法识别求助

解决Ansible角色中find模块引用本地templates目录的路径问题

直接使用Ansible内置的{{ role_path }}变量构建路径即可,该变量会自动指向当前执行角色的本地目录,完全适配角色复用场景,无需硬编码绝对路径。

修改后的任务代码如下:

- name: Register local file list before copy to remote
  ansible.builtin.find:
    paths: "{{ role_path }}/templates/services"
  delegate_to: localhost
  register: list_files

问题原因

template模块的src属性会自动在当前角色的templates目录下查找文件,因此可以直接使用相对路径;但find模块在设置delegate_to: localhost后,默认工作目录是执行playbook的根目录而非角色目录,直接写templates/services会导致找不到对应路径。role_path变量能精准定位到当前角色的本地目录,拼接后即可正确指向目标文件夹。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 19:36:20