Ansible + Kubernetes: how to wait for a Job completion

Kubernetes documentation specifies that:

As pods successfully complete, the Job tracks the successful completions. When a specified number of successful completions is reached, the task (ie, Job) is complete.

Based on this and the API specification you already linked - we can assume that Job will have condition type Complete set as True when it was successfully executed that many times as you requested.

Hence:

wait_condition:
  type: Complete
  status: True

Should do the "job".

As it is stated in k8s plugin code, reason is ignored when it is not specified.

I didn't test it. Just based on code and documentation so it would be nice if you could confirm that it works or not.


wait_condition works for me with jobs, as long as timeout/type/status are set appropriately, based on your job average time process:

        wait: yes
        wait_timeout: 300
        wait_condition:
          type: Complete
          status: True

(The other answers were so close that I'd edit them, but it says the edit queues are full.) The status in Job Condition is a string. In YAML a True tag is resolved to boolean type and you need to quote it to get the string. Like in the YAML output of the Job:

$ kubectl -n demo get job jobname -o yaml
apiVersion: batch/v1
kind: Job
metadata: ...
spec: ...
status:
  completionTime: "2021-01-19T16:24:47Z"
  conditions:
  - lastProbeTime: "2021-01-19T16:24:47Z"
    lastTransitionTime: "2021-01-19T16:24:47Z"
    status: "True"
    type: Complete
  startTime: "2021-01-19T16:24:46Z"
  succeeded: 1

Therefore to get completion you need to quote the status in wait_condition.

  k8s:
    wait: yes
    wait_condition:
      type: Complete
      status: "True"

(The wait parameter expects boolean and in YAML yes is a string, but Ansible accepts more values to boolean parameters.)