--- - name: Install MicroK8s snap command: snap install microk8s --classic --channel=1.35/stable args: creates: /snap/bin/microk8s - name: Wait for MicroK8s to be ready command: microk8s status --wait-ready changed_when: false register: mk8s_status until: mk8s_status.rc == 0 retries: 10 delay: 10 - name: Add user to microk8s group user: name: "{{ ansible_user }}" groups: microk8s append: yes - name: Create .kube directory file: path: "/home/{{ ansible_user }}/.kube" state: directory owner: "{{ ansible_user }}" group: "{{ ansible_user }}" mode: '0755' - name: Generate kubeconfig shell: microk8s config > /home/{{ ansible_user }}/.kube/config changed_when: false # We might want to make this idempotent by checking file existence or content, # but regenerating it is usually fine/safe. - name: Set ownership of kubeconfig file: path: "/home/{{ ansible_user }}/.kube/config" owner: "{{ ansible_user }}" group: "{{ ansible_user }}" mode: '0600' - name: Create .kube directory for root file: path: /root/.kube state: directory mode: '0700' owner: root group: root - name: Generate kubeconfig for root shell: microk8s config > /root/.kube/config changed_when: false - name: Add kubectl alias to .bashrc lineinfile: path: "/home/{{ ansible_user }}/.bashrc" line: "alias kubectl='microk8s kubectl'" regexp: "^alias kubectl='microk8s kubectl'$" state: present - name: Enable MicroK8s addons command: microk8s enable dns hostpath-storage metallb:192.168.50.240-192.168.50.250 register: enable_addons changed_when: "'already enabled' not in enable_addons.stdout" failed_when: - enable_addons.rc != 0 - "'already enabled' not in enable_addons.stdout" - name: Patch hostpath-provisioner to use /data shell: | microk8s kubectl patch deployment hostpath-provisioner -n kube-system --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/volumes/0/hostPath/path", "value": "/data/microk8s-storage"}]' register: patch_hostpath changed_when: "'patched' in patch_hostpath.stdout" retries: 5 delay: 10 until: patch_hostpath.rc == 0