Home > Blockchain >  Wait for a couple of minutes to check if an URL is responding
Wait for a couple of minutes to check if an URL is responding

Time:05-25

I run a start script using Ansible as below:

- name: Start service
  raw: "source ~/.profile; ~/start.sh"

Now, I want to keep checking the HTTP status for the services I started above until it is successful — 200.

Below is how I check for HTTP status 200, but, I do not know how to keep checking for 2 minutes and report if the URL still does not return 200.

- name: Detect if HTTp response is '200'
  uri:
    url: 'http://myhost/iportal/'
    return_content: yes
    validate_certs: no
    status_code:
      - 200
  register: uri_output

CodePudding user response:

Use an until loop:

- name: Wait until HTTP status is 200
  uri:
    url: 'http://myhost/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

Please mind that: in this case, you might see different timing if your request ends up timing out. The default timeout of the uri module is 30 seconds.

In this case, you will have 24 retries * 5 seconds delay 24 retries * 30 seconds until timeout, so, around 14 minutes.
From there on, either adapt the timeout or the number of retries, or a mix of both.

  • Related