Ansible执行script模块任务失败:无法找到远程脚本文件
问题排查:Ansible执行远程脚本时提示找不到文件
问题场景
执行命令:
ansible-playbook playbooks/install_aap_platform.yml -f 10 -vvv --private-key $ID_RSA -u ec2-user -i installer_node
其中Run installer script任务失败,报错:
Could not find or access '/opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh' on the Ansible Controller.
If you are using a module and expect the file to exist on the remote, see the remote_src option
已知信息:
populate /etc/hosts file任务可正常执行- SSH登录远程主机后手动执行该脚本无异常
/opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/目录归ansible用户所有,且之前的文件上传任务中become_user配置正常
核心原因
Ansible默认会将任务中指定的文件路径当作本地控制器上的路径,而你的setup.sh实际存储在远程主机上,因此需要显式告知模块去远程查找文件。
解决步骤
1. 添加remote_src: yes参数(针对script模块)
如果你的任务使用script模块执行远程脚本,必须添加remote_src: yes参数,明确指定脚本在远程主机上:
- name: Run installer script ansible.builtin.script: cmd: /opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh remote_src: yes become: yes become_user: ansible
2. 直接用shell/command模块执行远程路径
如果使用shell或command模块,直接填写远程主机上的脚本路径即可,同时确保become配置正确,切换到ansible用户执行:
- name: Run installer script ansible.builtin.shell: /opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh become: yes become_user: ansible
3. 验证远程文件路径有效性
可以先添加debug任务,确认远程主机上的脚本确实存在:
- name: Check if setup.sh exists ansible.builtin.stat: path: /opt/ansible-automation-platform-setup-bundle-2.4-1.4-x86_64/setup.sh register: script_check become: yes become_user: ansible - name: Print script status ansible.builtin.debug: var: script_check.stat.exists
如果输出为true,说明路径无误,再回到脚本执行任务调整配置即可。
内容的提问来源于stack exchange,提问作者Danilo Patrucco
相关产品推荐
相关产品推荐

