---
- hosts: localhost
connection: local
gather_facts: false
tasks:
- shell: |
while true; do
sleep 1
done
timeout: 5
...
并显示如下错误消息:
TASK [shell]**************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "The shell action failed to execute in the expected time frame (5) and was terminated"}
在Ansible 2.9.6(I think since Ansible 2.4)中,您还可以执行其他操作:异步的 通过异步,您提供了一个时间,在此之后,它将超时,在理论上,在秒...我说“在理论上”,因为我发现它没有一个强大的实时约束。
---
- hosts: docker_host
become: yes
become_user: jeanedoe
tasks:
- name: Run docker in the remote machine
shell: docker ps # options if some
register: call_stdout
async: 30 # it will timeout after ~30 seconds (approx)...
- debug:
msg:
- "The output is {{ call_stdout }}"
---
- hosts: docker_host
become: yes
become_user: jeanedoe
tasks:
- name: Run docker in the remote machine
shell: docker ps # options if some
register: call_stdout
async: 30 # it will timeout after ~30 seconds (approx)...
poll: 0 # default is 10 if not specified
- debug:
msg:
- "The output is {{ call_stdout }}"
4条答案
按热度按时间svmlkihl1#
在2.10版本中添加了一个
task
timeout
(以秒为单位),这在此类场景中很有用。例如,以下行动手册在
2.10
版本中失败:并显示如下错误消息:
ds97pgxw2#
Ansible中没有实现任务超时功能。
您可以尝试使用asynchronous call的解决方法,但对于这种情况(显然是一种bug),依赖系统可能更容易,也更合适。
请参见GNU
timeout
command(如果您运行Docker,则该命令可能已存在于您的操作系统中):lzfw57am3#
考虑到这一点,在以后的版本中添加了一个超时选项,允许您在WinRM示例上的清单文件中指定以下变量:
我的用例是一个
docker swarm init
,它在Windows上非常混乱,但在Linux上运行良好。它没有解决我的问题,但它可能会解决你的问题,这取决于你的传输。我也注意到了https://github.com/ansible/ansible/pull/69284/files,但我在任何地方都找不到解释。
fsi0uk1n4#
在Ansible 2.9.6(I think since Ansible 2.4)中,您还可以执行其他操作:异步的
通过异步,您提供了一个时间,在此之后,它将超时,在理论上,在秒...我说“在理论上”,因为我发现它没有一个强大的实时约束。
本期发行的具体做法:它将报告失败,并且不确定这是否是您想要的,也许您应该找到一种方法来捕获它将报告的错误(示例来自我拥有的输出,与您的情况无关):
或者,虽然这可能不是您想要的,但为了避免报告故障,可以只使用命令调用,也可以执行其他操作。
当我调用将在后台运行的其他人的远程服务时(我不能远程创建另一个调用脚本),我会使用这个方法,Ansible不需要应答,这就是Ansible所谓的“触发并忽略”,通过指定轮询值0来完成:
现在,剧本回顾应该是: