ansible Ansible: Loops and Conditionals Using until for a retry looping alive check


Example

This is an example of using until/retries/delay to implement an alive check for a webapp that is starting up. It assumes that there will be some period of time (up to 3 minutes) where the webapp is refusing socket connections. After that, it checks the /alive page for the word "OK". It also delegates the retrieval of the URL to the localhost running ansible. This makes sense as the final task in a deployment playbook.

---
- hosts: my-hosts
  tasks:
  - action: uri url=http://{{ ansible_all_ipv4_addresses }}:8080/alive return_content=yes
    delegate_to: localhost
    register: result
    until: "'failed' not in result and result.content.find('OK') != -1"
    retries: 18
    delay: 10

The until retry pattern can be used with any action; Ansible documentation provides an example of waiting until a certain shell command returns a desired result: http://docs.ansible.com/ansible/playbooks_loops.html#do-until-loops.