ansible kubectl wait for nodes to be READY

Viewed 3229

Is there any existing ansible module I can use for the following. I can wait for kubectl get nodes STATUS=Ready?

$ kubectl get nodes
NAME      STATUS     ROLES     AGE       VERSION
master1   NotReady   master    42s       v1.8.4
3 Answers

The kubectl wait command

Kubernetes supports the use of kubectl wait from version v1.11.
It waits for a specific condition on one or many resources.

Using the kubectl wait command with ansible tasks:

  - name: Wait for all k8s nodes to be ready
    shell: kubectl wait --for=condition=Ready nodes --all --timeout=600s
    register: nodes_ready

  - debug: var=nodes_ready.stdout_lines

If you want to check the condition for some particular nodes only, you can use a --selector instead of --all like this:

  - name: Wait for k8s nodes with node label 'purpose=test' to be ready
    shell: kubectl wait --for=condition=Ready nodes --selector purpose=test --timeout=600s
    register: nodes_ready

  - debug: var=nodes_ready.stdout_lines

I didn't know any existing module for this. you can do something like this.

---
- hosts: localhost
  gather_facts: no
  tasks:
  - name: Wait for nodes to be ready
    shell: "/usr/bin/kubectl get nodes"
    register: nodes
    until:      
      - '" Ready "  in nodes.stdout'      
    retries: 6
    delay: 2

If you want to check a specific node to be ready you can do this. In this case it will check if the current node is ready.

- name: Wait for node to be ready
  become: yes
  ansible.builtin.shell: $([[ $(kubectl get node $(cat /etc/hostname) -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}') == "True" ]])
  args:
    executable: /bin/bash
  register: kubelet_ready
  until: kubelet_ready.rc == 0
  retries: 60
  delay: 3
Related