docker 可能的任务超时最大长度

yx2lnoni  于 2022-11-02  发布在  Docker
关注(0)|答案(4)|浏览(129)

我在我的一些剧本中执行了一个shell: docker ps ...任务。这通常可以工作,但有时候docker守护进程挂起,docker ps在~2个小时内不返回。
如果docker ps没有返回,我如何配置Ansible在合理的时间内超时?

svmlkihl

svmlkihl1#

在2.10版本中添加了一个tasktimeout(以秒为单位),这在此类场景中很有用。

例如,以下行动手册在2.10版本中失败:

---
- 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"}
ds97pgxw

ds97pgxw2#

Ansible中没有实现任务超时功能。
您可以尝试使用asynchronous call的解决方法,但对于这种情况(显然是一种bug),依赖系统可能更容易,也更合适。
请参见GNU timeout command(如果您运行Docker,则该命令可能已存在于您的操作系统中):

shell: timeout 5m docker ps ...
lzfw57am

lzfw57am3#

考虑到这一点,在以后的版本中添加了一个超时选项,允许您在WinRM示例上的清单文件中指定以下变量:

ansible_winrm_operation_timeout_sec: 120
ansible_winrm_read_timeout_sec: 150

我的用例是一个docker swarm init,它在Windows上非常混乱,但在Linux上运行良好。它没有解决我的问题,但它可能会解决你的问题,这取决于你的传输。
我也注意到了https://github.com/ansible/ansible/pull/69284/files,但我在任何地方都找不到解释。

fsi0uk1n

fsi0uk1n4#

在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 }}"

本期发行的具体做法:它将报告失败,并且不确定这是否是您想要的,也许您应该找到一种方法来捕获它将报告的错误(示例来自我拥有的输出,与您的情况无关):

TASK [Run docker in the remote machine]*****************************************************************************************************************
fatal: [docker_host]: FAILED! => {"ansible_async_watchdog_pid": 2220, "ansible_job_id": "701125428265.6104", "changed": false, "finished": 1, "msg": "timed out waiting for module completion", "results_file": ..., "started": 1}

PLAY RECAP*********************************************************************************************************************************************************************
docker_host                       : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

或者,虽然这可能不是您想要的,但为了避免报告故障,可以只使用命令调用,也可以执行其他操作。
当我调用将在后台运行的其他人的远程服务时(我不能远程创建另一个调用脚本),我会使用这个方法,Ansible不需要应答,这就是Ansible所谓的“触发并忽略”,通过指定轮询值0来完成:

---
- 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 }}"

现在,剧本回顾应该是:

TASK [debug]*******************************************************************************************************************************************************************
ok: [docker_host] => {
    "msg": [
        "Pyton run output is {'started': 1, 'ansible_async_watchdog_pid': 5608, 'results_file': '...', 'finished': 0, 'ansible_job_id': '822653990290.6896', 'failed': False, 'changed': False}"
    ]
}

PLAY RECAP*********************************************************************************************************************************************************************
docker_host                       : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

相关问题