How to restart Jenkins using Ansible and wait for it to come back?
I have found that Jenkins always return a 503 response (Service not available) when it's up but not ready. So the most reliable way would be to isolate and examine the response code header alone.
- name: Wait until Jenkins web interface is available
wait_for: "host={{ jenkins_host }} port={{ jenkins_port }} state=present delay=5 timeout=300"
- name: Wait until Jenkins web interface is ready
command: 'curl -s -o /dev/null -w "%{http_code}" http://{{ jenkins_host }}:{{ jenkins_port}}/cli/'
register: result
until: 'result.stdout[0] in ["2", "3"]' # 2xx or 3xx status code
retries: 50
delay: 3
changed_when: false
I solved it using curl + Ansible's until
, which I just learned about. It will request the page until the http status code is 200 OK.
- name: Wait untils Jenkins web API is available
shell: curl --head --silent http://localhost:8080/cli/
register: result
until: result.stdout.find("200 OK") != -1
retries: 12
delay: 5
Using the URI module http://docs.ansible.com/ansible/uri_module.html
- 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