通过SSH等待远程文件:Ansible wait_for模块能否指定远程主机路径?
Ansible wait_for模块远程路径使用问题解答
这种实现方式不可行,原因如下:
ansible.builtin.wait_for模块默认在控制节点(运行Ansible的本地机器)上执行,它无法直接识别192.168.1.1:/home/test.txt这种带远程主机IP的路径格式,除非控制节点通过NFS、SMB等方式挂载了远程主机的对应目录,否则会报错找不到路径。
正确的实现方式有两种:
1. 直接在目标远程主机的Play中执行任务
将任务放在针对192.168.1.1的Play里,此时模块会在远程主机上运行,直接填写文件的本地路径即可:
- name: Wait for file on remote host hosts: 192.168.1.1 tasks: - name: Wait until the file is touched ansible.builtin.wait_for: path: /home/test.txt timeout: 300
2. 使用delegate_to指定任务执行目标
如果当前Play的目标是其他主机,但需要等待192.168.1.1上的文件,可通过delegate_to将任务委派到目标远程主机执行:
- name: Wait for file on remote host from another Play hosts: your_target_host tasks: - name: Wait until the file is touched ansible.builtin.wait_for: path: /home/test.txt timeout: 300 delegate_to: 192.168.1.1
内容的提问来源于stack exchange,提问作者mugetsu
相关产品推荐
相关产品推荐

