<p>Thanks in advance for your time that you spent reading this.</p>

<p>I'm playing with Kubernetes and use Ansible for any interactions with my cluster. Have some playbooks that successfully deploy applications.</p>

<p>My main ansible component I use for deployment is <a href="https://docs.ansible.com/ansible/latest/modules/k8s_module.html" rel="noreferrer">k8s</a> that allow me to apply my yaml configs.</p>

<p>I can successfully wait until deployment completes using</p>

<pre><code>k8s:
    state: present
    definition: config.yaml
    wait: yes
    wait_timeout: 10
</code></pre>

<p>But, unfortunately, the same trick doesn't work by default with Kubernetes Jobs. The module simply exits immediately that is clearly described in ansible module, that's true:</p>

<blockquote>
  <p>For resource kinds without an implementation, wait returns immediately unless wait_condition is set.</p>
</blockquote>

<p>To cover such a case, module spec suggests to specify</p>

<pre><code>wait_condition:
  reason: REASON
  type: TYPE
  status: STATUS
</code></pre>

<p>The doc also says:</p>

<blockquote>
  <p>The possible types for a condition are specific to each resource type in Kubernetes. See the API documentation of the status field for a given resource to see possible choices.</p>
</blockquote>

<p>I checked API specification and found the same as stated <a href="https://stackoverflow.com/a/51251664/5114117">in the following answer</a>: </p>

<blockquote>
  <p>the only type values are “Complete” and “Failed”, and that they may have a ”True” or ”False” status</p>
</blockquote>

<p>So, my <strong>QUESTION</strong> is simple: is there anyone who know how to use this <strong>wait_condition</strong> properly? Did you try it already (as for now, it's relatively new feature)?</p>

<p>Any ideas where to look are also appreciated.</p>

<p><strong>UPDATE:</strong></p>

<p>That's a kind of workaround I use now:</p>

<pre><code>- name: Run Job
  k8s:
   state: present
   definition: job_definition.yml

- name: Wait Until Job Is Done
  k8s_facts:
    name: job_name
    kind: Job
  register: job_status
  until: job_status.resources[0].status.active != 1
  retries: 10
  delay: 10
  ignore_errors: yes

- name: Get Final Job Status
  k8s_facts:
    name: job_name
    kind: Job
  register: job_status

- fail:
    msg: "Job Has Been Failed!"
  when: job_status.resources[0].status.failed == 1
</code></pre>

<p>But would be better to use the proper module feature directly.</p>

<p>(The other answers were so close that I'd edit them, but it says the edit queues are full.) The status in <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#jobcondition-v1-batch" rel="noreferrer">Job Condition</a> is a string. In YAML a <a href="https://yaml.org/spec/1.2/spec.html#id2805071" rel="noreferrer">True</a> tag is resolved to boolean type and you need to quote it to get the string. Like in the YAML output of the Job:</p>
<pre><code>$ kubectl -n demo get job jobname -o yaml
apiVersion: batch/v1
kind: Job
metadata: ...
spec: ...
status:
  completionTime: &quot;2021-01-19T16:24:47Z&quot;
  conditions:
  - lastProbeTime: &quot;2021-01-19T16:24:47Z&quot;
    lastTransitionTime: &quot;2021-01-19T16:24:47Z&quot;
    status: &quot;True&quot;
    type: Complete
  startTime: &quot;2021-01-19T16:24:46Z&quot;
  succeeded: 1
</code></pre>
<p>Therefore to get completion you need to quote the status in <code>wait_condition</code>.</p>
<pre><code>  k8s:
    wait: yes
    wait_condition:
      type: Complete
      status: &quot;True&quot;
</code></pre>
<p>(The <code>wait</code> parameter expects boolean and in YAML <code>yes</code> is a string, but Ansible accepts more values to <a href="https://docs.ansible.com/ansible/2.10/reference_appendices/YAMLSyntax.html#truthiness" rel="noreferrer">boolean parameters</a>.)</p>
