<p>Ansible has a great <code>community.kubernetes</code> module.
One of the useful flags of <code>k8s_info</code> is <a href="https://docs.ansible.com/ansible/latest/collections/community/kubernetes/k8s_info_module.html#parameter-wait" rel="nofollow noreferrer">wait</a> that is implemented for <code>Deployment</code>, <code>DaemonSet</code> and <code>Pod</code>.</p>
<p>For other k8s <code>kind</code>s it will return instantly unless <code>wait_condition</code> is provided.
What <code>wait_condition</code> should be provided to wait for <code>StatefulSet</code>?</p>

Rafal Skolasinski
<p>I would do this using an &quot;helper&quot; tasks file. Say I have some <code>roles/commes/tasks/helpers/wait-for.yaml</code>, that goes something like this:</p>
<pre><code>- name: &quot;Waits for {{ obj_name }} to startup&quot;
  block:     
  - name: &quot;Checks latest {{ obj_name }} status&quot; 
    debug:   
       msg: |    
         Object Kind {{ check_with.kind | default('nothing returned') }}                
    delay: &quot;{{ wait_for | default(10) }}&quot;
    ignore_errors: True
    retries: &quot;{{ retries_for | default(10) }}&quot;
    until:   
    - &gt;          
        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)) &gt;= 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)) &gt;= 1))
</code></pre>
<p>Then, whenever I need to wait for a Kubernetes resource, I would include that tasks file, using:</p>
<pre><code>- include_role:
    name: commons
    tasks_from: helpers/wait-for.yaml
  vars:
    check_with: &quot;{{ lookup('k8s', api_version='apps/v1',
                           kind='StatefulSet', namespace='default',
                           resource_name='my-statefulset') }}&quot;
    obj_name: &quot;Statefulset default/my-statefulset&quot;
    retries_for: 30
    wait_for: 10
</code></pre>
