improving playbooks

This commit is contained in:
un-oeil 2026-10-11 22:29:14 +02:00
parent b1c3f355f4
commit c4975726e8
25 changed files with 888 additions and 3 deletions

6
.ansible-lint Normal file
View file

@ -0,0 +1,6 @@
---
exclude_paths:
- .ansible/
- env/
skip_list:
- run-once[task] # every play uses the default linear strategy

11
.gitignore vendored
View file

@ -1,3 +1,14 @@
# ---> Ansible
*.retry
# secrets: vault password and the k3s admin kubeconfig written by deploy_kube.yml
vault/password
kube/*
# collections, temp files, ssh control sockets
.ansible/
# local virtualenv, if you don't use the Docker image
env/
.venv/
__pycache__/

24
Dockerfile Normal file
View file

@ -0,0 +1,24 @@
# Ansible controller for the homelab, isolated from whatever Ansible is on the
# laptop. Build and run it with ansible-docker.sh (source it first).
FROM python:3.13-slim-trixie
# The container user gets the laptop user's uid/gid, so the forwarded SSH agent
# socket is usable and files written in the repo (kube/config) belong to you.
ARG UID=1000
ARG GID=1000
RUN apt-get update \
&& apt-get install -y --no-install-recommends openssh-client git nano less ca-certificates \
&& rm -rf /var/lib/apt/lists/*
RUN groupadd -g "$GID" ansible && useradd -m -u "$UID" -g "$GID" -s /bin/bash ansible
COPY requirements.txt requirements.yml /tmp/
RUN pip install --no-cache-dir -r /tmp/requirements.txt \
&& ansible-galaxy collection install -r /tmp/requirements.yml -p /usr/share/ansible/collections
USER ansible
RUN mkdir -m 700 /home/ansible/.ssh
# nano -R: ansible-vault edit without backup or swap files
ENV EDITOR="nano -R"
WORKDIR /home/ansible/ansible

View file

@ -1,3 +1,51 @@
# ansible
Ansible repository to manage the infrastructure
Ansible repository to manage the homelab (`../homelab-session-summary-*.md`).
Everything is reached over WireGuard, so bring the tunnel up first.
## Run it from Docker
Ansible runs in a container so it never touches the laptop's own Ansible,
Python or collections (ViaRézo, kubic…).
```bash
source ansible-docker.sh
ansible-docker build # once, and after changing requirements*
ansible-docker run # shell in the repo, then ansible-playbook …
ansible-docker run ansible-playbook playbooks/utils.yml -CD
```
The container uses the host network (for `wg0`) and your SSH agent, reads
`~/.ssh/known_hosts` without writing to it, and mounts this repo, so edits are
live and `kube/config` lands here.
Without Docker: `uv venv env && uv pip install -p env/bin/python -r requirements.txt`,
then `env/bin/ansible-galaxy collection install -r requirements.yml -p .ansible/collections`.
## Inventory (`inventories/hosts.yml`)
| Group | What | Connects as |
|---|---|---|
| `hypervisors` | Proxmox host `10.0.0.1` | root |
| `lxc` | 100–106, 108–110, 250 | root (110 through `pct exec` on the host) |
| `vm` | 107 pelican-panel + the `k3s` VMs | root, k3s nodes as `un-oeil` + sudo |
| `k3s` | `k3s_masters` 131–133, `k3s_workers` 141–143 | `un-oeil` + sudo |
| `external` | ViaRézo VM `10.10.10.3` | root |
`host_type` (`lxc`, `vm`, `proxmox`, `external`) is what the playbooks test to
treat containers and VMs differently.
## Playbooks
Always dry-run first: `-CD` (`--check --diff`).
| Playbook | Does |
|---|---|
| `utils.yml` | apt sources → `prism.viarezo.fr`, base packages, `qemu-guest-agent` on VMs. `--tags upgrade -l <hosts>`: full-upgrade one host at a time (never the Proxmox host); `-e utils_reboot=true` reboots VMs that need it, never LXCs. |
| `add_user.yml` | Accounts from `inventories/group_vars/all/users.yml`: create, keys (exact list), passwordless sudo, remove. Skips system accounts of the same name (Forgejo's `un-oeil`). |
| `deploy_kube.yml` | Longhorn prerequisites on workers, k3s install/upgrade/config one node at a time (init master → masters → workers), writes `kube/config`. Bump `k3s_version` in `group_vars/k3s.yml` to upgrade. |
## Secrets
`vault/password` (gitignored) holds the vault password. Nothing is vaulted yet;
the k3s join token is read from the first master. `kube/config` is gitignored.

51
ansible-docker.sh Normal file
View file

@ -0,0 +1,51 @@
#!/bin/bash
# Run Ansible from a container, so it never touches the laptop's own Ansible,
# Python or collections.
# source ansible-docker.sh
# ansible-docker build
# ansible-docker run # interactive shell
# ansible-docker run ansible-playbook playbooks/utils.yml -CD
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "This script is meant to be sourced: source ansible-docker.sh"
exit 1
fi
_ansible_docker_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
_ansible_docker_image=homelab-ansible
function ansible-docker() {
local dir=$_ansible_docker_dir
case "$1" in
build)
docker build -t "$_ansible_docker_image" \
--build-arg UID="$(id -u)" --build-arg GID="$(id -g)" "$dir"
;;
run)
shift
if [[ -z "$SSH_AUTH_SOCK" || ! -S "$SSH_AUTH_SOCK" ]]; then
echo "No SSH agent (SSH_AUTH_SOCK). Start one and ssh-add your key first."
return 1
fi
if ! ip route get 10.0.0.1 2>/dev/null | grep -q wg; then
echo "Warning: 10.0.0.1 isn't routed through WireGuard. Is the tunnel up?"
fi
# vault/password must exist and be non-empty even unused: ansible.cfg points at it.
mkdir -p "$dir/vault" "$dir/kube"
[[ -s "$dir/vault/password" ]] || { echo "placeholder" > "$dir/vault/password"; chmod 600 "$dir/vault/password"; }
touch ~/.ssh/known_hosts
# --network host: reach 10.0.0.0/24 and 10.10.10.0/24 through the laptop's wg0.
# known_hosts is read-only and copied in, so the laptop's file is never
# written. Hosts first seen here are accepted again on each run.
docker run -it --rm --network host \
--mount type=bind,source="$dir",target=/home/ansible/ansible \
--mount type=bind,source="$SSH_AUTH_SOCK",target=/run/ssh-agent.sock \
--mount type=bind,source="$HOME/.ssh/known_hosts",target=/home/ansible/.ssh/known_hosts.laptop,readonly \
-e SSH_AUTH_SOCK=/run/ssh-agent.sock \
"$_ansible_docker_image" \
bash -c 'cp ~/.ssh/known_hosts.laptop ~/.ssh/known_hosts; exec "${@:-bash}"' _ "$@"
;;
*)
echo "Usage: ansible-docker build | run [command...]"
;;
esac
}

15
ansible.cfg Normal file
View file

@ -0,0 +1,15 @@
[defaults]
inventory = ./inventories
roles_path = ./roles
# ./.ansible for a local venv, /usr/share/ansible for the Docker image
collections_path = ./.ansible/collections:~/.ansible/collections:/usr/share/ansible/collections
local_tmp = ./.ansible/tmp
vault_password_file = ./vault/password
interpreter_python = auto_silent
forks = 20
callback_result_format = yaml
[ssh_connection]
pipelining = True
control_path_dir = ./.ansible/cp
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=accept-new

View file

@ -0,0 +1,4 @@
---
# Debian LXCs/VMs, the Proxmox host and the ViaRézo VM: root over SSH.
# k3s nodes override this (group_vars/k3s.yml).
ansible_user: root

View file

@ -0,0 +1,21 @@
---
# Admin accounts managed by playbooks/add_user.yml.
#
# login required
# ssh_keys required for state=present. authorized_keys is replaced by
# exactly this list (keys not listed are removed)
# hosts inventory pattern, default "all" (e.g. "lxc", "k3s:viarezo-vm")
# sudo nopasswd (default) | false
# shell only applied when set; new accounts default to /bin/bash
# groups extra groups, added to the existing ones
# state present (default) | absent (removes the account and its home)
#
# Accounts that already exist as system users (uid < 1000) are never touched:
# on forgejo, `un-oeil` is the Forgejo service account, and Forgejo writes its
# authorized_keys.
users:
- login: un-oeil
hosts: "all:!forgejo"
sudo: nopasswd
ssh_keys:
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJnyQBvcUTW4YHz5wGBwkT/elDcPsri8Po9MAJCY2P1K un-oeil@archlinux

View file

@ -0,0 +1,2 @@
---
host_type: external

View file

@ -0,0 +1,2 @@
---
host_type: proxmox

View file

@ -0,0 +1,22 @@
---
# Ubuntu cloud-init VMs: no root login, `un-oeil` has passwordless sudo.
ansible_user: un-oeil
ansible_become: true
# Bumping this upgrades the cluster, one node at a time (deploy_kube.yml).
k3s_version: v1.36.5+k3s1
# Extra names/IPs in the API certificate. The cluster-init master also adds its
# own IP. 10.0.0.139 is reserved for a kube-vip address.
k3s_tls_san:
- 10.0.0.139
k3s_master_taint: node-role.kubernetes.io/control-plane:NoSchedule
# Join token. Leave undefined to read it from the cluster-init master
# (/var/lib/rancher/k3s/server/token). Set it (vaulted, e.g. group_vars/all/secrets.yml)
# only to build a new cluster with a known token.
# k3s_token: "{{ vault_k3s_token }}"
# Longhorn data disk on the workers: the 200 GB zvol on hdd-mirror.
k3s_longhorn_disk: /dev/sdb
k3s_longhorn_disk_size: 214748364800
k3s_longhorn_path: /var/lib/longhorn

View file

@ -0,0 +1,2 @@
---
host_type: lxc

View file

@ -0,0 +1,2 @@
---
host_type: vm

View file

@ -0,0 +1,7 @@
---
# LXC 110 is the WireGuard hub and accepts no SSH key. Ansible logs in to the
# Proxmox host as root and runs everything through `pct exec 110`
# (paramiko, so the host key must be in ~/.ssh/known_hosts).
ansible_connection: community.proxmox.proxmox_pct_remote
ansible_host: 10.0.0.1
ansible_user: root

View file

@ -1,2 +0,0 @@
---

92
inventories/hosts.yml Normal file
View file

@ -0,0 +1,92 @@
---
# Every machine of the homelab. Host names are the Proxmox guest names.
# Groups by kind:
# hypervisors the bare-metal Proxmox host
# lxc Proxmox containers: share the host kernel, no guest agent,
# no kernel modules, never need a reboot for a kernel update
# vm Proxmox VMs (QEMU): own kernel, qemu-guest-agent
# external machines outside the Proxmox host (ViaRézo cloud VM)
# Groups by role: k3s (k3s_masters, k3s_workers), all of them VMs.
# proxmox_vmid matches `pct list` / `qm list`. IPs: 1NN -> 10.0.0.(NN+10),
# except k3s (VMID = last octet) and 110/250 (.250/.251).
all:
children:
hypervisors:
hosts:
proxmox:
ansible_host: 10.0.0.1
lxc:
hosts:
caddy:
ansible_host: 10.0.0.10
proxmox_vmid: 100
crowdsec:
ansible_host: 10.0.0.11
proxmox_vmid: 101
cloud:
ansible_host: 10.0.0.12
proxmox_vmid: 102
vault:
ansible_host: 10.0.0.13
proxmox_vmid: 103
forgejo:
ansible_host: 10.0.0.14
proxmox_vmid: 104
restic:
ansible_host: 10.0.0.15
proxmox_vmid: 105
etherpad:
ansible_host: 10.0.0.16
proxmox_vmid: 106
jellyfin:
ansible_host: 10.0.0.18
proxmox_vmid: 108
uptime-kuma:
ansible_host: 10.0.0.19
proxmox_vmid: 109
wireguard: # no SSH login, reached through `pct exec` (host_vars/wireguard.yml)
ansible_host: 10.0.0.1
proxmox_vmid: 110
leaderboard:
ansible_host: 10.0.0.251
proxmox_vmid: 250
vm:
hosts:
pelican-panel:
ansible_host: 10.0.0.17
proxmox_vmid: 107
children:
k3s:
k3s:
children:
k3s_masters:
hosts:
k3s-master-1:
ansible_host: 10.0.0.131
proxmox_vmid: 131
k3s_cluster_init: true # bootstraps etcd, the others join it
k3s-master-2:
ansible_host: 10.0.0.132
proxmox_vmid: 132
k3s-master-3:
ansible_host: 10.0.0.133
proxmox_vmid: 133
k3s_workers:
hosts:
k3s-worker-1:
ansible_host: 10.0.0.141
proxmox_vmid: 141
k3s-worker-2:
ansible_host: 10.0.0.142
proxmox_vmid: 142
k3s-worker-3:
ansible_host: 10.0.0.143
proxmox_vmid: 143
external:
hosts:
viarezo-vm:
ansible_host: 10.10.10.3 # through the WireGuard tunnel

View file

View file

@ -0,0 +1,116 @@
---
# Create, update or remove the admin accounts listed in
# inventories/group_vars/all/users.yml, on every host they target.
#
# ansible-playbook playbooks/add_user.yml -CD # dry run first
# ansible-playbook playbooks/add_user.yml -l lxc
#
# Never touches root, accounts not in the list, or system accounts (uid < 1000).
- name: Manage admin accounts
hosts: all
gather_facts: true
gather_subset: [min]
vars:
add_user_present: "{{ add_user_managed | default([]) | selectattr('state', '==', 'present') | list }}"
add_user_absent: "{{ add_user_managed | default([]) | selectattr('state', '==', 'absent') | list }}"
tasks:
- name: Read existing accounts
ansible.builtin.getent:
database: passwd
- name: Keep the users that target this host, minus system accounts
ansible.builtin.set_fact:
add_user_managed: "{{ add_user_managed | default([]) + [{'state': 'present', 'sudo': 'nopasswd'} | combine(item)] }}"
loop: "{{ users }}"
loop_control:
label: "{{ item.login }}"
when:
- inventory_hostname in query('ansible.builtin.inventory_hostnames', item.hosts | default('all'))
- item.login not in getent_passwd or getent_passwd[item.login][1] | int >= 1000
- name: Warn about system accounts left alone
ansible.builtin.debug:
msg: "Skipping {{ item.login }}: it is a system account here (uid {{ getent_passwd[item.login][1] }})."
loop: "{{ users }}"
loop_control:
label: "{{ item.login }}"
when:
- inventory_hostname in query('ansible.builtin.inventory_hostnames', item.hosts | default('all'))
- item.login in getent_passwd and getent_passwd[item.login][1] | int < 1000
- name: Check the user list
ansible.builtin.assert:
that:
- item.state in ['present', 'absent']
- item.sudo in ['nopasswd', false]
- item.state == 'absent' or item.ssh_keys | default([]) | length > 0
- item.state == 'present' or item.login != ansible_user
fail_msg: >-
{{ item.login }}: state must be present/absent, sudo nopasswd/false,
ssh_keys non-empty, and the account Ansible connects with
({{ ansible_user }}) can't be removed.
quiet: true
loop: "{{ add_user_managed | default([]) }}"
loop_control:
label: "{{ item.login }}"
- name: Install sudo
ansible.builtin.package:
name: sudo
state: present
when: add_user_present | selectattr('sudo', '==', 'nopasswd') | list | length > 0
- name: Create or update accounts
ansible.builtin.user:
name: "{{ item.login }}"
# Only set the shell when asked, or for a new account (useradd's default is /bin/sh).
shell: "{{ item.shell | default(omit if item.login in getent_passwd else '/bin/bash') }}"
groups: "{{ item.groups | default(omit) }}"
append: "{{ true if item.groups is defined else omit }}"
create_home: true
state: present
loop: "{{ add_user_present }}"
loop_control:
label: "{{ item.login }}"
- name: Set authorized keys (exactly the listed ones)
ansible.posix.authorized_key:
user: "{{ item.login }}"
key: "{{ item.ssh_keys | join('\n') }}"
exclusive: true
state: present
loop: "{{ add_user_present }}"
loop_control:
label: "{{ item.login }}"
# In a dry run a new account doesn't exist yet, so there's nothing to compare.
when: not (ansible_check_mode and item.login not in getent_passwd)
- name: Allow passwordless sudo
ansible.builtin.copy:
dest: /etc/sudoers.d/{{ item.login }}
content: "{{ item.login }} ALL=(ALL) NOPASSWD: ALL\n"
owner: root
group: root
mode: "0440"
validate: /usr/sbin/visudo -cf %s
loop: "{{ add_user_present | selectattr('sudo', '==', 'nopasswd') }}"
loop_control:
label: "{{ item.login }}"
- name: Remove sudo rules that are no longer wanted
ansible.builtin.file:
path: /etc/sudoers.d/{{ item.login }}
state: absent
loop: "{{ add_user_absent + add_user_present | rejectattr('sudo', '==', 'nopasswd') }}"
loop_control:
label: "{{ item.login }}"
- name: Remove accounts marked absent
ansible.builtin.user:
name: "{{ item.login }}"
state: absent
remove: true
loop: "{{ add_user_absent }}"
loop_control:
label: "{{ item.login }}"

View file

@ -0,0 +1,231 @@
---
# Build, upgrade or reconfigure the k3s cluster (k3s-design.md): 3 masters with
# embedded etcd, 3 workers carrying Longhorn. Safe to rerun: on the running
# cluster it changes nothing unless the inventory says otherwise.
#
# ansible-playbook playbooks/deploy_kube.yml -CD # dry run first
# ansible-playbook playbooks/deploy_kube.yml
# ansible-playbook playbooks/deploy_kube.yml --tags kubeconfig # only refresh kube/config
#
# Upgrade: bump k3s_version in group_vars/k3s.yml and rerun. The cluster-init
# master goes first, then the other masters, then the workers, one node at a
# time, each waiting for Ready. Restarting k3s leaves the pods running.
#
# Creating the VMs themselves is not done here (k3s-build-vms.md).
- name: Check the k3s inventory
hosts: k3s
gather_facts: true
tags: [always]
tasks:
- name: Only VMs can be k3s nodes
ansible.builtin.assert:
that: host_type == 'vm'
fail_msg: "{{ inventory_hostname }} is a {{ host_type }}. k3s needs a VM (own kernel, cgroups, iSCSI for Longhorn)."
quiet: true
- name: Exactly one master bootstraps the cluster
ansible.builtin.assert:
that: groups['k3s_masters'] | map('extract', hostvars) | selectattr('k3s_cluster_init', 'defined') | selectattr('k3s_cluster_init') | list | length == 1
fail_msg: "Set k3s_cluster_init: true on exactly one host of k3s_masters."
quiet: true
run_once: true
- name: Group the cluster-init master
ansible.builtin.group_by:
key: k3s_init
when: k3s_cluster_init | default(false)
changed_when: false
- name: Longhorn prerequisites
hosts: k3s_workers
gather_facts: false
tags: [longhorn]
tasks:
- name: Install iSCSI and NFS clients
ansible.builtin.apt:
name: [open-iscsi, nfs-common]
state: present
update_cache: true
cache_valid_time: 3600
- name: Start iscsid
ansible.builtin.systemd_service:
name: iscsid
enabled: true
state: started
- name: Load iscsi_tcp at boot
ansible.builtin.copy:
dest: /etc/modules-load.d/longhorn.conf
content: "iscsi_tcp\n"
owner: root
group: root
mode: "0644"
- name: Load iscsi_tcp now
community.general.modprobe:
name: iscsi_tcp
state: present
- name: List services
ansible.builtin.service_facts:
# multipathd grabs Longhorn's block devices.
- name: Disable multipathd
ansible.builtin.systemd_service:
name: multipathd.service
enabled: false
state: stopped
when: "'multipathd.service' in ansible_facts.services"
- name: Stop its socket (static unit, can't be disabled)
ansible.builtin.systemd_service:
name: multipathd.socket
state: stopped
when: "'multipathd.service' in ansible_facts.services"
- name: Check whether the Longhorn disk is already mounted
ansible.builtin.command: findmnt -no SOURCE {{ k3s_longhorn_path }}
register: k3s_longhorn_mount
changed_when: false
failed_when: false
check_mode: false
# Formatting only happens on an empty disk of the expected size, like
# argocd/scripts/prep-worker.sh. Anything else stops the play.
- name: Format the empty Longhorn disk
when: k3s_longhorn_mount.stdout != k3s_longhorn_disk
block:
- name: Inspect {{ k3s_longhorn_disk }}
ansible.builtin.shell: |
set -o pipefail
echo "size=$(lsblk -bdno SIZE {{ k3s_longhorn_disk }})"
echo "parts=$(lsblk -no NAME {{ k3s_longhorn_disk }} | tail -n +2 | wc -l)"
echo "fs=$(blkid -o value -s TYPE {{ k3s_longhorn_disk }} || true)"
args:
executable: /bin/bash
register: k3s_disk_info
changed_when: false
check_mode: false
- name: Refuse anything but an empty disk of the expected size
ansible.builtin.assert:
that:
- "'size=' ~ k3s_longhorn_disk_size in k3s_disk_info.stdout_lines"
- "'parts=0' in k3s_disk_info.stdout_lines"
- "'fs=' in k3s_disk_info.stdout_lines"
fail_msg: >-
{{ k3s_longhorn_disk }} is not an empty {{ k3s_longhorn_disk_size }}-byte disk
({{ k3s_disk_info.stdout_lines | join(', ') }}). Not formatting it.
- name: Create the ext4 filesystem
community.general.filesystem:
dev: "{{ k3s_longhorn_disk }}"
fstype: ext4
opts: -L longhorn
- name: Mount the Longhorn disk
ansible.posix.mount:
src: LABEL=longhorn
path: "{{ k3s_longhorn_path }}"
fstype: ext4
opts: defaults,noatime
dump: "0"
passno: "2"
state: mounted
- name: First master
hosts: k3s_init
gather_facts: false
tags: [k3s]
vars:
k3s_role: server
k3s_service: k3s
k3s_init_host: "{{ inventory_hostname }}"
tasks:
# Without a vaulted k3s_token, a new cluster generates its own.
- name: Use the vaulted token
ansible.builtin.set_fact:
k3s_join_token: "{{ k3s_token }}"
no_log: true
when: k3s_token is defined
- name: Install or update k3s
ansible.builtin.include_tasks: tasks/k3s-node.yml
- name: Join token
hosts: k3s
gather_facts: false
tags: [k3s]
vars:
k3s_init_host: "{{ groups['k3s_init'] | first }}"
tasks:
- name: Read the token from the first master
ansible.builtin.slurp:
src: /var/lib/rancher/k3s/server/token
delegate_to: "{{ k3s_init_host }}"
run_once: true
register: k3s_token_file
no_log: true
when: k3s_token is not defined
# A dry run of a brand-new cluster has no token yet.
ignore_errors: "{{ ansible_check_mode }}"
- name: Set the join token
ansible.builtin.set_fact:
k3s_join_token: "{{ k3s_token | default(k3s_token_file.content | b64decode | trim) }}"
no_log: true
when: k3s_token is defined or k3s_token_file.content is defined
- name: Other masters
hosts: k3s_masters:!k3s_init
gather_facts: false
serial: 1
tags: [k3s]
vars:
k3s_role: server
k3s_service: k3s
k3s_init_host: "{{ groups['k3s_init'] | first }}"
k3s_server_url: https://{{ hostvars[k3s_init_host].ansible_host }}:6443
tasks:
- name: Install or update k3s
ansible.builtin.include_tasks: tasks/k3s-node.yml
- name: Workers
hosts: k3s_workers
gather_facts: false
serial: 1
tags: [k3s]
vars:
k3s_role: agent
k3s_service: k3s-agent
k3s_init_host: "{{ groups['k3s_init'] | first }}"
k3s_server_url: https://{{ hostvars[k3s_init_host].ansible_host }}:6443
tasks:
- name: Install or update k3s
ansible.builtin.include_tasks: tasks/k3s-node.yml
- name: Admin kubeconfig
hosts: k3s_init
gather_facts: false
tags: [k3s, kubeconfig]
tasks:
- name: Read the admin kubeconfig
ansible.builtin.slurp:
src: /etc/rancher/k3s/k3s.yaml
register: k3s_kubeconfig
no_log: true
# Same server and names as ~/.kube/k3s-homelab.yaml.
- name: Write kube/config
ansible.builtin.copy:
dest: "{{ playbook_dir }}/../kube/config"
content: >-
{{ k3s_kubeconfig.content | b64decode
| replace('https://127.0.0.1:6443', 'https://' ~ ansible_host ~ ':6443')
| regex_replace('(?m)^(\s*(?:- )?(?:name|cluster|user|current-context): )default$', '\1k3s-homelab') }}
mode: "0600"
delegate_to: localhost
become: false
diff: false # client certificate and key

View file

@ -0,0 +1,74 @@
---
# Install, upgrade or reconfigure k3s on one node. Included by deploy_kube.yml,
# one node at a time. Does nothing when the version and config already match.
- name: Create /etc/rancher/k3s
ansible.builtin.file:
path: /etc/rancher/k3s
state: directory
owner: root
group: root
mode: "0755"
# Content only: k3s restarts when this task changes, not for a mode fix.
- name: Write /etc/rancher/k3s/config.yaml
ansible.builtin.template:
src: k3s-config.yaml.j2
dest: /etc/rancher/k3s/config.yaml
diff: false # holds the join token
register: k3s_config
# The k3s install leaves it 0644, readable by every local user.
- name: Make the config readable by root only
ansible.builtin.file:
path: /etc/rancher/k3s/config.yaml
owner: root
group: root
mode: "0600"
- name: Read the installed k3s version
ansible.builtin.command: /usr/local/bin/k3s --version
register: k3s_installed
changed_when: false
failed_when: false
check_mode: false
- name: Decide whether k3s must be installed
ansible.builtin.set_fact:
k3s_needs_install: "{{ k3s_installed.rc != 0 or k3s_version not in k3s_installed.stdout }}"
- name: Report the version change
ansible.builtin.debug:
msg: "{{ inventory_hostname }}: {{ k3s_installed.stdout_lines | first | default('k3s not installed') }} -> {{ k3s_version }}"
when: k3s_needs_install
- name: Download the install script of this k3s version
ansible.builtin.get_url:
url: https://raw.githubusercontent.com/k3s-io/k3s/{{ k3s_version }}/install.sh
dest: /usr/local/bin/k3s-install.sh
mode: "0755"
when: k3s_needs_install
- name: Install k3s {{ k3s_version }}
ansible.builtin.command: /usr/local/bin/k3s-install.sh
environment:
INSTALL_K3S_VERSION: "{{ k3s_version }}"
INSTALL_K3S_EXEC: "{{ k3s_role }}"
changed_when: true
when: k3s_needs_install
- name: Restart k3s for the new config
ansible.builtin.systemd_service:
name: "{{ k3s_service }}"
state: restarted
when: k3s_config is changed and not k3s_needs_install
- name: Wait for the node to be Ready
ansible.builtin.command: k3s kubectl wait --for=condition=Ready node/{{ inventory_hostname }} --timeout=30s
delegate_to: "{{ k3s_init_host }}"
register: k3s_ready
until: k3s_ready.rc == 0
retries: 20
delay: 15
changed_when: false
when: (k3s_needs_install or k3s_config is changed) and not ansible_check_mode

View file

@ -0,0 +1,16 @@
{% if k3s_cluster_init | default(false) %}
cluster-init: true
{% else %}
server: {{ k3s_server_url }}
{% endif %}
{% if k3s_join_token is defined %}
token: {{ k3s_join_token }}
{% endif %}
{% if inventory_hostname in groups['k3s_masters'] %}
tls-san:
{% for san in ([ansible_host] if k3s_cluster_init | default(false) else []) + k3s_tls_san %}
- {{ san }}
{% endfor %}
node-taint:
- {{ k3s_master_taint }}
{% endif %}

View file

@ -0,0 +1,127 @@
---
# Baseline for every machine, plus an opt-in system upgrade.
#
# ansible-playbook playbooks/utils.yml -CD # baseline, dry run
# ansible-playbook playbooks/utils.yml
# ansible-playbook playbooks/utils.yml --tags upgrade -l lxc # apt full-upgrade
# ansible-playbook playbooks/utils.yml --tags upgrade -l k3s_workers -e utils_reboot=true
#
# Baseline: apt sources on prism.viarezo.fr, a few base packages,
# qemu-guest-agent on VMs (agent=1 is already set in Proxmox), optional timezone.
# Upgrade: never runs unless --tags upgrade, refuses to run without --limit,
# one host at a time, never on the Proxmox host (upgrade that one by hand).
- name: Baseline
hosts: all
gather_facts: true
gather_subset: [min]
vars:
utils_packages: [curl, ca-certificates, htop, less, rsync]
# Unset = leave it alone. Changing it moves every local-time timer
# (restic at 03:00, ...) on the LXCs, which are in UTC today.
# utils_timezone: Europe/Paris
utils_mirror_rewrites:
- from: https?://deb\.debian\.org/debian(?=[/\s]|$)
to: http://prism.viarezo.fr/debian
- from: https?://security\.debian\.org(/debian-security)?(?=[/\s]|$)
to: http://prism.viarezo.fr/debian-security
- from: https?://([a-z]{2}\.)?archive\.ubuntu\.com/ubuntu(?=[/\s]|$)
to: http://prism.viarezo.fr/ubuntu
- from: https?://security\.ubuntu\.com/ubuntu(?=[/\s]|$)
to: http://prism.viarezo.fr/ubuntu
tasks:
- name: Find apt source files
ansible.builtin.find:
paths: [/etc/apt, /etc/apt/sources.list.d]
patterns: ["sources.list", "*.list", "*.sources"]
register: utils_apt_files
- name: Point Debian/Ubuntu sources at the ViaRézo mirror
ansible.builtin.replace:
path: "{{ item.0.path }}"
regexp: "{{ item.1.from }}"
replace: "{{ item.1.to }}"
loop: "{{ utils_apt_files.files | product(utils_mirror_rewrites) | list }}"
loop_control:
label: "{{ item.0.path }}: {{ item.1.to }}"
- name: Install base packages
ansible.builtin.apt:
name: "{{ utils_packages }}"
state: present
install_recommends: false
update_cache: true
cache_valid_time: 3600
- name: Install the QEMU guest agent (VMs only)
ansible.builtin.apt:
name: qemu-guest-agent
state: present
install_recommends: false
when: host_type == 'vm'
- name: Start the QEMU guest agent (VMs only)
ansible.builtin.systemd_service:
name: qemu-guest-agent
state: started
# The unit has no [Install] section: udev starts it when the virtio port shows up.
when: host_type == 'vm' and not ansible_check_mode
- name: Set the timezone
community.general.timezone:
name: "{{ utils_timezone }}"
when: utils_timezone is defined
- name: Upgrade
hosts: all:!hypervisors
gather_facts: false
serial: "{{ utils_serial | default(1) }}"
tags: [upgrade, never]
tasks:
- name: Refuse to upgrade everything at once
ansible.builtin.assert:
that: ansible_limit is defined
fail_msg: "Pick the hosts with --limit (e.g. -l lxc, -l k3s_workers, -l jellyfin)."
run_once: true
- name: Upgrade packages
ansible.builtin.apt:
upgrade: full
update_cache: true
autoremove: true
- name: Check whether a reboot is needed
ansible.builtin.stat:
path: /var/run/reboot-required
register: utils_reboot_required
# LXCs run the host's kernel: a reboot never applies a kernel update there.
- name: Reboot (VMs only, with -e utils_reboot=true)
ansible.builtin.reboot:
reboot_timeout: 600
when:
- utils_reboot_required.stat.exists
- host_type == 'vm'
- utils_reboot | default(false) | bool
- name: Wait for the k3s node to be Ready again
ansible.builtin.command: k3s kubectl wait --for=condition=Ready node/{{ inventory_hostname }} --timeout=300s
delegate_to: "{{ groups['k3s_masters'] | first }}"
changed_when: false
when:
- utils_reboot_required.stat.exists
- inventory_hostname in groups['k3s']
- utils_reboot | default(false) | bool
- name: Report a pending reboot
ansible.builtin.debug:
msg: >-
{{ inventory_hostname }} ({{ host_type }}) wants a reboot.
{{ 'Rerun with -e utils_reboot=true.' if host_type == 'vm'
else 'LXC: restart the services instead (needrestart), or pct reboot from the host if needed.' if host_type == 'lxc'
else 'Reboot it by hand.' }}
when:
- utils_reboot_required.stat.exists
- not (host_type == 'vm' and utils_reboot | default(false) | bool)

5
requirements.txt Normal file
View file

@ -0,0 +1,5 @@
# Python deps of the Ansible controller (the Docker image, or a local venv).
ansible-core==2.21.5
ansible-lint==26.9.0
# community.proxmox.proxmox_pct_remote (LXC 110 has no sshd we can log in to)
paramiko==5.0.0

9
requirements.yml Normal file
View file

@ -0,0 +1,9 @@
---
# ansible-galaxy collection install -r requirements.yml -p .ansible/collections
collections:
- name: ansible.posix
version: 2.2.2
- name: community.general
version: 13.5.0
- name: community.proxmox
version: 2.1.0

View file