---
- name: microk8s | ensure snapd is installed
  ansible.builtin.apt:
    name:
      - snapd
    update_cache: true
    state: present

- name: microk8s | Install microk8s
  become: true
  ansible.builtin.snap:
    name: microk8s
    classic: true
    channel: "{{ microk8s_version }}"

- name: microk8s | Wait for microk8s to be ready
  become: true
  ansible.builtin.command: microk8s.status --wait-ready
  changed_when: false
  register: mk8sstatusout
  failed_when:
      - "'This MicroK8s deployment is acting as a node in a cluster.' not in mk8sstatusout.stdout_lines"
      - mk8sstatusout.rc > 0

- name: microk8s | create kubectl alias
  become: true
  ansible.builtin.command: "snap alias microk8s.kubectl kubectl"
  changed_when: false
  register: aliaskubectlout

- name: microk8s | create helm3 alias
  become: true
  ansible.builtin.command: "snap alias microk8s.helm3 helm"
  changed_when: false
  register: aliashelmout
  when:
    - "'helm3' in microk8s_plugins"
    - microk8s_plugins.helm3
