k0s install with Ansible

April 30, 2022

I wanted to use Ansible to install k0s so that it could be easily replicated across multiple machines. There is a k0s Ansible playbook available at: https://docs.k0sproject.io/v1.21.0+k0s.0/examples/ansible-playbook/ I took this and modified it to my needs a bit.

I ended up with the following files, building on the Ansible setup from my previous blog post.

supported-k0s-version.json

This file was supplied as part of the repository and defined which version to install

{
  "schemaVersion": 1,
  "label": "supports k0s",
  "message": "v1.22.4+k0s.1",
  "color": "green" 
}

install-k0s.yml

This playbook was used to install k0s onto the nodes defined in inventory.yml in my previous post.

---

- hosts: all
  name: Download k0s on all nodes
  become: yes
  roles:
    - role: download
      tags: download
    - role: prereq
      tags: prereq

- hosts: initial_controller
  gather_facts: yes
  become: yes
  name: Configure initial k0s control plane node
  roles:
    - role: k0s/initial_controller
      tags: init

- hosts: controller
  gather_facts: yes
  become: yes
  serial: 1
  name: Configure k0s control plane nodes
  roles:
    - role: k0s/controller
      tags: server

- hosts: worker
  become: yes
  name: Configure k0s worker nodes
  roles:
    - role: k0s/worker
      tags: worker

This made use of the roles folder, which was also copied from the link above. I did have to change a timeout in the roles/k0s/initial_controller/tasks/main.yml file, the delay in this following block was set to 15 seconds, and I was getting a timeout on setting up the worker join token. I tried 30, but that was no good and went for 60 in the end which succeeded.

- name: Wait for k8s apiserver
  wait_for:
    host: localhost
    port: 6443
    delay: 60
    timeout: 180

remove-k0s.yml

Finally, I created a playbook to remove k0s from the nodes:

---

- hosts: all
  gather_facts: yes
  become: yes
  roles:
    - role: reset

Running Playbooks

These playbooks can be run as follows. To install k0s on the cluster:

$ ansible-playbook install-k0s.yml --ask-become-pass

To remove it again

$ ansible-playbook remove-k0s.yml --ask-become-pass