Wait for service activation during Ansible playbook execution.
Most of the time I see such simple construct.
- name: Retry a task until a certain condition is met systemd: name: cron state: started - name: Wait for service activation wait_for: timeout: 50
Which is a time waste as it takes 50 second every time playbook is executed.
TASK [Retry a task until a certain condition is met] ******************************** changed: [localhost] TASK [Wait for service activation] ************************************************** ok: [localhost]
A much better solution is to use until
keyword to wait until a service state condition is met.
- name: Start nginx service systemd: name: nginx state: started register: local__service_nginx until: local__service_nginx.status.ActiveState == "active" retries: 5 delay: 10
Now it will take less time as it will be tried up to 5 times with a delay of 10 seconds.
TASK [Start nginx service] ********************************************************** FAILED - RETRYING: Start nginx service (5 retries left). ok: [localhost]
Nice.