等待几分钟检查 URL 是否响应

Ash*_*har 6 url wait http-response-codes ansible until-loop

我使用 Ansible 运行启动脚本,如下所示:

\n
- name: Start service\n  raw: "source ~/.profile; ~/start.sh"\n
Run Code Online (Sandbox Code Playgroud)\n

现在,我想继续检查上面启动的服务的 HTTP 状态,直到成功 \xe2\x80\x94 200

\n

下面是我如何检查 HTTP 状态200,但是,我不知道如何继续检查 2 分钟并报告 URL 是否仍然没有返回200

\n
- name: Detect if HTTp response is \'200\'\n  uri:\n    url: \'http://example.org/iportal/\'\n    return_content: yes\n    validate_certs: no\n    status_code:\n      - 200\n  register: uri_output\n
Run Code Online (Sandbox Code Playgroud)\n

β.ε*_*.βε 13

使用until循环:

- name: Wait until HTTP status is 200
  uri:
    url: 'http://example.org/iportal/'
    return_content: yes
    validate_certs: no
    status_code:
      - 200
  until: uri_output.status == 200
  retries: 24 # Retries for 24 * 5 seconds = 120 seconds = 2 minutes
  delay: 5 # Every 5 seconds
  register: uri_output
Run Code Online (Sandbox Code Playgroud)

请注意:在这种情况下,如果您的请求最终超时,您可能会看到不同的时间。timeout模块默认uri为30秒。

在这种情况下,您将有 24 次重试 * 5 秒延迟 + 24 次重试 * 30 秒直到超时,因此大约需要 14 分钟。
从那时起,要么调整超时或重试次数,要么两者结合。