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

Ansible lookup插件在AWX中无法正常工作问题求助

AWX中Ansible lookup('file')插件无法找到文件的解决方法

问题原因

本地运行Ansible时,默认会在当前执行playbook的目录下查找文件,但AWX执行playbook时,会将代码仓库内容同步到服务器的临时工作目录,直接用相对路径file.txt无法匹配AWX的目录结构,导致找不到文件。

解决方案

方法1:利用playbook_dir变量定位文件

使用Ansible内置的playbook_dir变量(指向当前playbook所在目录)拼接文件路径,确保在本地和AWX环境都能正确找到文件:

- name: get file content
  set_fact:
    policer: "{{ lookup('file', playbook_dir ~ '/file.txt') }}"

方法2:配置文件查找路径

先设置ansible_search_path变量将playbook目录加入查找路径,再使用相对路径:

- name: set file search path
  set_fact:
    ansible_search_path: "{{ playbook_dir }}"

- name: get file content
  set_fact:
    policer: "{{ lookup('file', 'file.txt') }}"

确认远程主机文件路径的方法

注意:lookup('file')仅在控制节点(本地或AWX服务器)查找文件,若要操作远程主机的文件,需用以下方式:

  • 使用stat模块检查远程文件状态与路径:
- name: check remote file
  stat:
    path: "/path/to/remote/file.txt"
  register: remote_file_stats

- name: show remote file info
  debug:
    msg: "File exists: {{ remote_file_stats.stat.exists }}, Absolute path: {{ remote_file_stats.stat.path }}"
  • 使用command模块查找远程文件:
- name: find remote file location
  command: find / -name "file.txt"
  register: remote_file_paths

- name: print remote file paths
  debug:
    msg: "{{ remote_file_paths.stdout_lines }}"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 12:31:02