homelab/build/config/ansible/roles/k3s_node/tasks/prep.yml
Russell Seymour 1250c9cef6
Some checks are pending
deploy / deploy (push) Waiting to run
Initial checkin of code for managing homelab
2026-08-24 20:52:36 +01:00

80 lines
2.9 KiB
YAML

---
# OS-level prerequisites, identical for a server and an agent node. Runs
# before either install so a from-scratch Pi (fresh Ubuntu Server image, SSH
# + the `ansible` user already set up) can go straight to a working cluster
# in one playbook run — the "easy to rebuild" part of the design.
- name: Read the current kernel boot parameters
ansible.builtin.command: cat {{ k3s_boot_cmdline_path }}
register: k3s_cmdline_current
changed_when: false
check_mode: false
- name: Work out which cgroup parameters are missing
ansible.builtin.set_fact:
k3s_cmdline_missing: >-
{{ k3s_cgroup_params | reject('in', k3s_cmdline_current.stdout) | list }}
# cmdline.txt is one line, space-separated — rewritten whole rather than
# appended in place, since there's no line-based anchor to insert after.
# Existing file mode is left alone (no `mode:` here) rather than guessed at.
- name: Add missing cgroup parameters to the boot command line
ansible.builtin.copy:
dest: "{{ k3s_boot_cmdline_path }}"
content: >-
{{ (k3s_cmdline_current.stdout.split() + k3s_cmdline_missing) | join(' ') }}
owner: root
group: root
register: k3s_cmdline_updated
when: k3s_cmdline_missing | length > 0
- name: Reboot to apply updated boot parameters
ansible.builtin.reboot:
reboot_timeout: 300
when: k3s_cmdline_updated is changed
- name: Check active swap devices
ansible.builtin.command: swapon --summary
register: k3s_swap_active
changed_when: false
check_mode: false
- name: Turn off active swap
ansible.builtin.command: swapoff -a
when: k3s_swap_active.stdout | trim | length > 0
- name: Comment out swap entries in fstab
ansible.builtin.replace:
path: /etc/fstab
regexp: '^([^#\n]*\sswap\s.*)$'
replace: '# \1'
# Best-effort: Ubuntu's zram-backed swap ships under different unit names
# across releases, and most won't be present at all. A missing unit is not a
# failure here — only an already-active one that we failed to disable would
# leave swap coming back on the next boot, and swapoff -a above already
# handles the running instance for this boot.
#
# `failed_when: false` rather than `ignore_errors: true`: systemd_service
# raises "Could not find the requested service" as a hard module failure
# when the unit is absent, and that's reported here as a genuine task
# failure regardless of ignore_errors — failed_when overrides the result
# directly instead of trying to catch it after the fact.
- name: Disable Ubuntu's zram-backed swap, if present
ansible.builtin.systemd_service:
name: "{{ item }}"
enabled: false
state: stopped
loop:
- zram-config.service
- systemd-zram-setup@zram0.service
register: k3s_zram_disable
failed_when: false
changed_when: k3s_zram_disable is changed
- name: Ensure curl is installed
ansible.builtin.apt:
name: curl
state: present
update_cache: true
cache_valid_time: 3600