Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ansible wait_for http
- name: wait_for http
command: "curl --silent {{ url }}"
register: result
until: result.stdout.find("200 OK") != -1
retries: 60
delay: 1
changed_when: false
@realjktu

This comment has been minimized.

Copy link

@realjktu realjktu commented May 21, 2018

As far is this gist is on top of Google search now :) the best solution is to use uri ansible module like below.

  • name: "wait for ABC to come up"
    uri:
    url: "http://127.0.0.1:8080/ABC"
    status_code: 200
    register: result
    until: result.status == 200
    retries: 60
    delay: 1
@vlakas

This comment has been minimized.

Copy link

@vlakas vlakas commented Jun 15, 2018

@realjiktu nice workaround. But... version with curl is more reliable. uri depends on httplib2 python library which must be installed on remote host.

failed: [boomslang] => {"failed": true}
msg: httplib2 is not installed

FATAL: all hosts have already failed -- aborting
@geerlingguy

This comment has been minimized.

Copy link

@geerlingguy geerlingguy commented Sep 28, 2018

The above comment, formatted a little more nicely:

- name: "wait for ABC to come up"
  uri:
    url: "http://127.0.0.1:8080/ABC"
    status_code: 200
  register: result
  until: result.status == 200
  retries: 60
  delay: 1

This will try the URL every second for up to 60 seconds before failing.

@j-chimienti

This comment has been minimized.

Copy link

@j-chimienti j-chimienti commented Jan 3, 2019

- name: "wait for {{url}} to come up"
  uri:
    url: "{{url}}"
    method: GET
    status: 200
  register: _result
  until: _result.status == 200
  retries: 30 # retry X times  
  delay: 60 # pause for X sec b/w each call

max calls = retries + 1


TASK [URL CALL : wait for https://xxx to come up] ***
FAILED - RETRYING: wait for https://xxx to come up (30 retries left).
FAILED - RETRYING: wait for https://xxx to come up (29 retries left).
FAILED - RETRYING: wait for https://xxx to come up (28 retries left).

@xr09

This comment has been minimized.

Copy link

@xr09 xr09 commented Oct 8, 2019

@realjiktu nice workaround. But... version with curl is more reliable. uri depends on httplib2 python library which must be installed on remote host.

https://docs.ansible.com/ansible/latest/modules/uri_module.html#notes

The dependency on httplib2 was removed in Ansible 2.1

@RickS-C137

This comment has been minimized.

Copy link

@RickS-C137 RickS-C137 commented Feb 12, 2020

The uri module doesn't work for me. I'm deploying a mucroservice on our Kubernetes cluster and have to make additional calls, after the service is reachable. The issue with the uri module is, the dns entry is created when the microservice is deployed. It take up to 2-3 minutes until it is created. The uri module failes, if the dns entry hasn't been created when it's executed. So I'll try the curl/command module thing mentioned above. Does anybody has another idea for this?

@trilom

This comment has been minimized.

Copy link

@trilom trilom commented Jul 5, 2020

Here is a variation of the above that works. Since this constantly returns a 200 it checks content instead. Similar to the above examples you can add status_code: [200, 403] if the health check returns a 200 or a 403 as passing and check that in the until portion.

- name: wait for consul to come up
  uri:
    url: "http://127.0.0.1:8500/v1/status/leader"
    method: GET
    headers:
      Authorization: Bearer {{ consul_acl_master_token }}
    return_content: yes
  register: _result
  until: _result.content != '\"\"'
  retries: 30 # retry X times
  delay: 3 # pause for X sec b/w each call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.