Ansible Role中files目录文件复制失败问题求助
问题排查:AWX中Ansible角色复制文件找不到文件的问题
在AWX中基于Git仓库运行Ansible角色时,执行复制files/目录文件到远程节点的任务失败,控制节点提示找不到对应文件。此前已完成过类似角色操作,本次将4个任务合并为一个后出现问题,已确认files/目录下存在目标文件,希望得到解决思路。
相关任务代码
- name: Copy certificate and key files to the destination copy: src: "files/{{ item.netbios | lower }}.{{ item.file_type }}" dest: "{{ nginx_ssl_dest }}/{{ item.netbios | lower }}.{{ item.cert_ext }}.{{ item.file_type }}" when: answer == 'True' and app_netbios == item.netbios register: cert_copy loop: "{{ files_to_copy }}" loop_control: label: "Copying {{ item.netbios }}.{{ item.file_type }}" vars: files_to_copy: - { netbios: 'domain', cert_ext: 'ini', file_type: 'crt' } - { netbios: 'domain', cert_ext: 'ini', file_type: 'key' } - { netbios: 'domain2', cert_ext: 'ini', file_type: 'crt' } - { netbios: 'domain2', cert_ext: 'ini', file_type: 'key' }
错误信息
failed: [testserver] (item=Copying domain.crt) => {"ansible_loop_var": "item", "changed": false, "item": {"cert_ext": "ini", "file_type": "crt", "netbios": "domain"}, "msg": "Could not find or access '/tmp/bwrap_k2q_hi_g/awx_/project/files/domain.ini.crt' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"} An exception occurred during task execution. To see the full traceback, use -vvv. The error was: If you are using a module and expect the file to exist on the remote, see the remote_src failed: [testserver] (item=Copying domain.key) => {"ansible_loop_var": "item", "changed": false, "item": {"cert_ext": "ini", "file_type": "key", "netbios": "domain"}, "msg": "Could not find or access '/tmp/bwrap_k2q_hi_g/awx_/project/files/domain.ini.key' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
解决思路
- 验证实际执行的src路径:添加
-vvv参数运行任务,查看日志中实际拼接的src值。错误信息显示寻找的是domain.ini.crt,但任务定义的src应为domain.crt,需确认是否存在变量替换错误或代码未同步至AWX。 - 确认Git仓库文件结构:检查仓库中角色的
files目录下是否存在domain.crt、domain.key、domain2.crt、domain2.key,注意Ansible角色的files目录需位于角色根目录下,而非项目根目录。 - 排查when条件干扰:临时注释
when条件,测试任务是否能找到文件,排除条件逻辑导致的异常触发或变量判断错误。 - 检查变量污染:确认
cert_ext变量是否被意外引入到src路径的拼接中,排查是否存在其他变量定义覆盖了任务中的src逻辑。 - 同步AWX项目代码:在AWX中手动触发项目同步,确保运行的是最新的Git仓库代码,避免使用旧的任务定义。
内容的提问来源于stack exchange,提问作者SwiperNo
相关产品推荐
相关产品推荐

