Skip to content

Instantly share code, notes, and snippets.

@superherointj
Last active May 1, 2024 14:48
Show Gist options
  • Save superherointj/d496714ddf218bdcd1c303dbfd834a5b to your computer and use it in GitHub Desktop.
Save superherointj/d496714ddf218bdcd1c303dbfd834a5b to your computer and use it in GitHub Desktop.
Ansible Playbook for Resetting K3s
- hosts: k3s-etcd # Alias for etcd hosts, defined in ansible inventory.
tasks:
- name: "stop etcd"
remote_user: root
shell: "systemctl stop etcd || true"
- name: "rm -rf /var/lib/etcd/"
remote_user: root
shell: "rm -rf /var/lib/etcd/ || true"
- name: Reboot host and wait for it to restart
remote_user: root
reboot:
msg: "Reboot initiated by Ansible"
connect_timeout: 5
reboot_timeout: 600
pre_reboot_delay: 0
post_reboot_delay: 90
test_command: whoami
reboot_command: systemctl reboot -i
search_paths: [ "/run/current-system/sw/bin/" ]
- hosts: k3s-dev # It's an alias for host names, k3s-dev is defined in the ansible inventory.
tasks:
# Disable `services.k3s.enable` in your NixOS configuration.
# On my case, I've **monkey patched** using import this way:
# services.k3s.enable = import ./k3s-enable;
# Adapt this as needed.
- name: disable k3s config
remote_user: root
shell: echo "false" > /etc/nixos/mynixos/profiles/sbc/shared/k3s/k3s-enable
- name: "mynixos: nixos-rebuild switch --no-write-lock-file"
remote_user: root
shell: "cd /etc/nixos/mynixos/; nixos-rebuild switch --no-write-lock-file"
- name: stop k3s
remote_user: root
shell: "systemctl stop k3s || true"
- name: umount kubelet
remote_user: root
shell: "KUBELET_PATH=$(mount | grep kubelet | cut -d' ' -f3); \
${KUBELET_PATH:+umount $KUBELET_PATH}; sleep 1"
- name: Delete k3s data
remote_user: root
shell: "rm -rf /etc/rancher/{k3s,node}; \
rm -rf /var/lib/{rancher/k3s,kubelet,longhorn,etcd,cni}"
# Cleaning up logs is important. But I find `journalctl` terrible.
# I don't know how to delete logs for a single unit. So I had to globally erase logs.
# Which I did as:
- name: clean-up logs
remote_user: root
shell: "journalctl --rotate --vacuum-time=1s"
- name: Reboot host and wait for it to restart
remote_user: root
reboot:
msg: Reboot initiated by Ansible
connect_timeout: 5
reboot_timeout: 600
pre_reboot_delay: 0
post_reboot_delay: 90
test_command: whoami
reboot_command: systemctl reboot -i
search_paths: [ "/run/current-system/sw/bin/" ]
# Adapt this monkey patch to your situation.
- name: restore k3s config
remote_user: root
shell: echo true > /etc/nixos/mynixos/profiles/sbc/shared/k3s/k3s-enable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment