donal
donal

Reputation: 363

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

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

Upvotes: 7

Views: 18291

Answers (3)

Vladimir Botka
Vladimir Botka

Reputation: 68024

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

  - wait_for:
      port: 80

The default state is started. If you want to wait for the active connection set state: drained

  - wait_for:
      port: 80
      state: drained

See the parameter active_connection_states.

Upvotes: 4

zigarn
zigarn

Reputation: 11595

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

Upvotes: 4

donal
donal

Reputation: 363

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

Upvotes: 13

Related Questions