How can i get Ansible to wait until a service is active?

Viewed 6527

I can check if a service is started(as below), but how do i use the service facts in Ansible to loop\wait until a service is active?

  - name: Ensure myservice is in a running state
    service:
      name: myservice 
      state: started
3 Answers

This is very similar to @zigarn answer above, In my case it was not enough to know that the service was started or running, i needed to know it was active, hence im posting what i went with. Thank you to all for your help

- name: Ensure myservice  is in a running state
    service:
      name: myservice 
      state: started
    register: myserviceDetails
    until: myserviceDetails.status.ActiveState == "active"
    retries: 15
    delay: 20

Use wait_for. For example, if the service is a web server

  - wait_for:
      port: 80

For a generic solution with any type of service, the solution would be to loop on getting service status until it's 'running'.

To achieve that, you can use the service_facts module with an until loop:

- name: Wait for service 'myservice' to be running
  service_facts:
  register: result
  until: result.ansible_facts.services['some.service'].state == 'running'
  retries: 10
  delay: 5
Related