I would do this using an "helper" tasks file. Say I have some roles/commes/tasks/helpers/wait-for.yaml
, that goes something like this:
- name: "Waits for {{ obj_name }} to startup"
block:
- name: "Checks latest {{ obj_name }} status"
debug:
msg: |
Object Kind {{ check_with.kind | default('nothing returned') }}
delay: "{{ wait_for | default(10) }}"
ignore_errors: True
retries: "{{ retries_for | default(10) }}"
until:
- >
check_with.status is defined
and check_with.kind is defined
and check_with.status is defined
and ((check_with.kind == 'Pod'
and (check_with.status.containerStatuses[0].ready | default(False)))
or (check_with.kind == 'DataVolume'
and (check_with.status.phase | default(False)) == 'Succeeded')
or (check_with.kind in [ 'Deployment', 'DeploymentConfig' ]
and (check_with.status.availableReplicas | default(0)) >= 1)
or (check_with.kind == 'Job'
and check_with.status.completionTime is defined
and check_with.status.succeeded is defined)
or (check_with.kind == 'PersistentVolumeClaim'
and (check_with.status.phase | default(False)) == 'Bound')
or (check_with.kind == 'StatefulSet'
and (check_with.status.readyReplicas | default(0)) >= 1))
Then, whenever I need to wait for a Kubernetes resource, I would include that tasks file, using:
- include_role:
name: commons
tasks_from: helpers/wait-for.yaml
vars:
check_with: "{{ lookup('k8s', api_version='apps/v1',
kind='StatefulSet', namespace='default',
resource_name='my-statefulset') }}"
obj_name: "Statefulset default/my-statefulset"
retries_for: 30
wait_for: 10