From b543a4c1ad2fe18e6ab21e6dcc1a356188b9b81b Mon Sep 17 00:00:00 2001 From: un-oeil Date: Sun, 11 Oct 2026 23:02:48 +0200 Subject: [PATCH] new utils --- .ansible-lint | 1 + README.md | 4 +- inventories/group_vars/all/users.yml | 2 +- inventories/group_vars/all/utils.yml | 53 +++++ inventories/group_vars/hypervisors.yml | 3 + inventories/host_vars/wireguard.yml | 2 + inventories/hosts.yml | 22 +- playbooks/tasks/k3s-node.yml | 29 ++- playbooks/templates/motd.j2 | 5 + playbooks/utils.yml | 278 +++++++++++++++++++++++-- 10 files changed, 373 insertions(+), 26 deletions(-) create mode 100644 inventories/group_vars/all/utils.yml create mode 100644 playbooks/templates/motd.j2 diff --git a/.ansible-lint b/.ansible-lint index 2653b0a..8418e62 100644 --- a/.ansible-lint +++ b/.ansible-lint @@ -4,3 +4,4 @@ exclude_paths: - env/ skip_list: - run-once[task] # every play uses the default linear strategy + - no-handler # ordered steps (test sshd, then reload) read better inline diff --git a/README.md b/README.md index 0739f15..8bbb6e3 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ then `env/bin/ansible-galaxy collection install -r requirements.yml -p .ansible/ | `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 | +| `external` | `viarezo`, the 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. @@ -41,7 +41,7 @@ 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 `: full-upgrade one host at a time (never the Proxmox host); `-e utils_reboot=true` reboots VMs that need it, never LXCs. | +| `utils.yml` | Settings in `group_vars/all/utils.yml`. apt sources → `prism.viarezo.fr`; CLI tools (ViaRézo's list + a few); en_US/fr_FR locales; Europe/Paris; journald capped (200M, host 1G); sshd keys-only (`sshd -t` before reload); hostname = inventory name (LXC through `pct set`, never k3s/host); motd. VMs only: guest agent, NTP, fstrim. Host: pve-no-subscription repo, udp/443 → WireGuard saved in `rules.v4`. Docker hosts: weekly dangling-image prune. Tags let you run parts (`--tags ssh`). `--tags upgrade -l `: 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. | diff --git a/inventories/group_vars/all/users.yml b/inventories/group_vars/all/users.yml index cbe4806..4706b10 100644 --- a/inventories/group_vars/all/users.yml +++ b/inventories/group_vars/all/users.yml @@ -4,7 +4,7 @@ # 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") +# hosts inventory pattern, default "all" (e.g. "lxc", "k3s:viarezo") # sudo nopasswd (default) | false # shell only applied when set; new accounts default to /bin/bash # groups extra groups, added to the existing ones diff --git a/inventories/group_vars/all/utils.yml b/inventories/group_vars/all/utils.yml new file mode 100644 index 0000000..2e654d2 --- /dev/null +++ b/inventories/group_vars/all/utils.yml @@ -0,0 +1,53 @@ +--- +# Settings of playbooks/utils.yml. Override per group (group_vars/.yml) +# or per host (host_vars/.yml). + +# The CLI utilities of ViaRézo's util role, plus a few more. btop comes from +# apt (Debian 13 / Ubuntu 26.04 have it), emacs without X11. +utils_packages: + - acl # become to an unprivileged user + - bind9-dnsutils + - btop + - build-essential + - bzip2 + - ca-certificates + - cron + - curl + - emacs-nox + - git + - htop + - jq + - ldnsutils + - less + - locate + - lsb-release + - lsof + - man-db + - mosh + - ncdu + - ncurses-term + - psmisc + - python-is-python3 + - rsync + - silversearcher-ag + - sudo + - tmux + - traceroute + - tree + - vim + +# Generated, not made the default: LANG is left as it is. +utils_locales: [en_US.UTF-8, fr_FR.UTF-8] + +# The LXCs were in UTC before 2026-10: their local-time timers (restic at 03:00, +# 03:15...) moved 2 hours earlier in UTC when this was applied. +utils_timezone: Europe/Paris + +utils_journald_max_use: 200M + +# Written to /etc/ssh/sshd_config.d/10-hardening.conf. sshd keeps the first +# value it reads, so this wins over the distro's own drop-ins (50-, 60-...). +utils_sshd_settings: + PermitRootLogin: prohibit-password + PasswordAuthentication: "no" + KbdInteractiveAuthentication: "no" diff --git a/inventories/group_vars/hypervisors.yml b/inventories/group_vars/hypervisors.yml index a4fd32d..76af1d8 100644 --- a/inventories/group_vars/hypervisors.yml +++ b/inventories/group_vars/hypervisors.yml @@ -1,2 +1,5 @@ --- host_type: proxmox + +# The host's journal holds PVE task logs too. +utils_journald_max_use: 1G diff --git a/inventories/host_vars/wireguard.yml b/inventories/host_vars/wireguard.yml index fe00efa..9f9d7c9 100644 --- a/inventories/host_vars/wireguard.yml +++ b/inventories/host_vars/wireguard.yml @@ -5,3 +5,5 @@ ansible_connection: community.proxmox.proxmox_pct_remote ansible_host: 10.0.0.1 ansible_user: root +# Its own address (ansible_host above is the Proxmox host). +guest_ip: 10.0.0.250 diff --git a/inventories/hosts.yml b/inventories/hosts.yml index f7f01a2..854be05 100644 --- a/inventories/hosts.yml +++ b/inventories/hosts.yml @@ -15,47 +15,60 @@ all: hosts: proxmox: ansible_host: 10.0.0.1 + description: Proxmox VE hypervisor lxc: hosts: caddy: ansible_host: 10.0.0.10 + description: Caddy reverse proxy, every vhost (DNS-01) proxmox_vmid: 100 crowdsec: ansible_host: 10.0.0.11 + description: CrowdSec LAPI proxmox_vmid: 101 cloud: ansible_host: 10.0.0.12 + description: Nextcloud AIO (Docker) proxmox_vmid: 102 vault: ansible_host: 10.0.0.13 + description: Vaultwarden (Docker) proxmox_vmid: 103 forgejo: ansible_host: 10.0.0.14 + description: Forgejo git, SSH on 2222 proxmox_vmid: 104 restic: ansible_host: 10.0.0.15 + description: restic rest-server, backup target proxmox_vmid: 105 etherpad: ansible_host: 10.0.0.16 + description: Etherpad + Postgres proxmox_vmid: 106 jellyfin: ansible_host: 10.0.0.18 + description: Jellyfin, media on /hdd-mirror/media proxmox_vmid: 108 uptime-kuma: ansible_host: 10.0.0.19 + description: Uptime Kuma (Docker), status.un-oeil.fr proxmox_vmid: 109 wireguard: # no SSH login, reached through `pct exec` (host_vars/wireguard.yml) ansible_host: 10.0.0.1 + description: WireGuard hub, tunnel 10.10.10.0/24 proxmox_vmid: 110 leaderboard: ansible_host: 10.0.0.251 + description: Leaderboard (Docker) proxmox_vmid: 250 vm: hosts: pelican-panel: ansible_host: 10.0.0.17 + description: Pelican Panel + local Wings proxmox_vmid: 107 children: k3s: @@ -66,27 +79,34 @@ all: hosts: k3s-master-1: ansible_host: 10.0.0.131 + description: k3s server (cluster-init, etcd) proxmox_vmid: 131 k3s_cluster_init: true # bootstraps etcd, the others join it k3s-master-2: ansible_host: 10.0.0.132 + description: k3s server (etcd) proxmox_vmid: 132 k3s-master-3: ansible_host: 10.0.0.133 + description: k3s server (etcd) proxmox_vmid: 133 k3s_workers: hosts: k3s-worker-1: ansible_host: 10.0.0.141 + description: k3s agent + Longhorn proxmox_vmid: 141 k3s-worker-2: ansible_host: 10.0.0.142 + description: k3s agent + Longhorn proxmox_vmid: 142 k3s-worker-3: ansible_host: 10.0.0.143 + description: k3s agent + Longhorn proxmox_vmid: 143 external: hosts: - viarezo-vm: + viarezo: ansible_host: 10.10.10.3 # through the WireGuard tunnel + description: "ViaRézo cloud VM: Wings (viarezo node), Overleaf" diff --git a/playbooks/tasks/k3s-node.yml b/playbooks/tasks/k3s-node.yml index c2d3c7a..c1130d7 100644 --- a/playbooks/tasks/k3s-node.yml +++ b/playbooks/tasks/k3s-node.yml @@ -10,21 +10,32 @@ 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. Fixed first, so +# the template below only changes when the content does (and restarts k3s). +- name: Look for an existing config + ansible.builtin.stat: + path: /etc/rancher/k3s/config.yaml + register: k3s_config_file -# The k3s install leaves it 0644, readable by every local user. -- name: Make the config readable by root only +- name: Make the existing config readable by root only ansible.builtin.file: path: /etc/rancher/k3s/config.yaml owner: root group: root mode: "0600" + when: k3s_config_file.stat.exists + +- name: Write /etc/rancher/k3s/config.yaml + ansible.builtin.template: + src: k3s-config.yaml.j2 + dest: /etc/rancher/k3s/config.yaml + owner: root + group: root + # New file: created 0600. Existing one: fixed by the task above, and left + # out here so a dry run doesn't report a content change (and a restart). + mode: "{{ omit if k3s_config_file.stat.exists else '0600' }}" + diff: false # holds the join token + register: k3s_config - name: Read the installed k3s version ansible.builtin.command: /usr/local/bin/k3s --version diff --git a/playbooks/templates/motd.j2 b/playbooks/templates/motd.j2 new file mode 100644 index 0000000..19b8fa4 --- /dev/null +++ b/playbooks/templates/motd.j2 @@ -0,0 +1,5 @@ + + {{ inventory_hostname }} - {{ description | default('') }} + {{ host_type }}{{ ' ' ~ proxmox_vmid if proxmox_vmid is defined else '' }} · {{ guest_ip | default(ansible_host) }} · groups: {{ group_names | join(', ') }} + Managed by Ansible (homelab-current/ansible). Local changes may be overwritten. + diff --git a/playbooks/utils.yml b/playbooks/utils.yml index 6db9330..cb5315c 100644 --- a/playbooks/utils.yml +++ b/playbooks/utils.yml @@ -1,25 +1,27 @@ --- # Baseline for every machine, plus an opt-in system upgrade. +# Settings: inventories/group_vars/all/utils.yml. # -# ansible-playbook playbooks/utils.yml -CD # baseline, dry run +# ansible-playbook playbooks/utils.yml -CD # dry run first # ansible-playbook playbooks/utils.yml -# ansible-playbook playbooks/utils.yml --tags upgrade -l lxc # apt full-upgrade +# ansible-playbook playbooks/utils.yml --tags ssh,motd # only some parts +# ansible-playbook playbooks/utils.yml --tags upgrade -l lxc # 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). +# Tags: apt, packages, locale, time, journald, disk, ssh, hostname, motd, +# proxmox, docker, upgrade (opt-in). +# +# LXC vs VM: +# - hostname: an LXC's hostname lives in its Proxmox config (pct set), which +# rewrites /etc/hostname and /etc/hosts at every start. A VM's lives inside it. +# - NTP, fstrim, guest agent: VMs only. LXCs use the host's clock and kernel. +# - reboot after an upgrade: VMs only. LXCs run the host's kernel. - 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 @@ -30,12 +32,20 @@ - from: https?://security\.ubuntu\.com/ubuntu(?=[/\s]|$) to: http://prism.viarezo.fr/ubuntu + handlers: + - name: Restart journald + ansible.builtin.systemd_service: + name: systemd-journald + state: restarted + tasks: + # ---------------------------------------------------------------- apt - 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 + tags: [apt] - name: Point Debian/Ubuntu sources at the ViaRézo mirror ansible.builtin.replace: @@ -45,14 +55,18 @@ loop: "{{ utils_apt_files.files | product(utils_mirror_rewrites) | list }}" loop_control: label: "{{ item.0.path }}: {{ item.1.to }}" + register: utils_mirror + tags: [apt] - - name: Install base packages + # ----------------------------------------------------------- packages + - name: Install admin tools ansible.builtin.apt: name: "{{ utils_packages }}" state: present install_recommends: false update_cache: true - cache_valid_time: 3600 + cache_valid_time: "{{ 0 if utils_mirror is changed else 3600 }}" + tags: [packages] - name: Install the QEMU guest agent (VMs only) ansible.builtin.apt: @@ -60,6 +74,7 @@ state: present install_recommends: false when: host_type == 'vm' + tags: [packages] - name: Start the QEMU guest agent (VMs only) ansible.builtin.systemd_service: @@ -67,11 +82,249 @@ 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 + tags: [packages] + # ------------------------------------------------------------- locale + - name: Install the locales package + ansible.builtin.apt: + name: locales + state: present + tags: [locale] + + - name: Generate locales + community.general.locale_gen: + name: "{{ utils_locales }}" + state: present + # In a dry run, the locales package may not be installed yet. + ignore_errors: "{{ ansible_check_mode }}" + tags: [locale] + + # --------------------------------------------------------------- time - name: Set the timezone community.general.timezone: name: "{{ utils_timezone }}" when: utils_timezone is defined + tags: [time] + + - name: List services (VMs only) + ansible.builtin.service_facts: + when: host_type in ['vm', 'external'] + tags: [time] + + # Ubuntu and the ViaRézo VM run chrony, VM 107 systemd-timesyncd: keep + # whichever is there, install timesyncd only when nothing syncs the clock. + - name: Install systemd-timesyncd where no NTP client runs (VMs only) + ansible.builtin.apt: + name: systemd-timesyncd + state: present + when: + - host_type in ['vm', 'external'] + - ansible_facts.services | dict2items + | selectattr('key', 'in', ['chrony.service', 'systemd-timesyncd.service', 'ntpsec.service']) + | selectattr('value.state', '==', 'running') | list | length == 0 + tags: [time] + + # ------------------------------------------------------- journald, disk + - name: Cap the journal size + ansible.builtin.copy: + dest: /etc/systemd/journald.conf.d/size.conf + content: | + [Journal] + SystemMaxUse={{ utils_journald_max_use }} + owner: root + group: root + mode: "0644" + notify: Restart journald + tags: [journald] + + - name: Trim free space on VM disks weekly (VMs only) + ansible.builtin.systemd_service: + name: fstrim.timer + enabled: true + state: started + when: host_type in ['vm', 'external'] + tags: [disk] + + # ----------------------------------------------------------------- ssh + - name: Harden sshd + tags: [ssh] + block: + - name: Write the sshd drop-in + ansible.builtin.copy: + dest: /etc/ssh/sshd_config.d/10-hardening.conf + content: | + # Managed by Ansible (utils.yml). Keys only; root by key only. + {% for k, v in utils_sshd_settings.items() %} + {{ k }} {{ v }} + {% endfor %} + owner: root + group: root + mode: "0644" + register: utils_sshd_dropin + + # sshd -t needs it, and it's missing while only ssh.socket is active. + - name: Create the sshd privilege separation directory + ansible.builtin.file: + path: /run/sshd + state: directory + owner: root + group: root + mode: "0755" + when: utils_sshd_dropin is changed + + - name: Test the whole sshd config + ansible.builtin.command: /usr/sbin/sshd -t + changed_when: false + when: utils_sshd_dropin is changed and not ansible_check_mode + + - name: Reload sshd + ansible.builtin.systemd_service: + name: ssh + state: reloaded + when: utils_sshd_dropin is changed + rescue: + - name: Remove the drop-in that broke the sshd config + ansible.builtin.file: + path: /etc/ssh/sshd_config.d/10-hardening.conf + state: absent + + - name: Stop on this host + ansible.builtin.fail: + msg: "sshd -t failed with the hardening drop-in, so it was removed and sshd not reloaded." + + # ------------------------------------------------------------ hostname + # The inventory name is the hostname. k3s nodes and the Proxmox host are + # only checked: renaming them renames the k8s node / breaks the PVE node. + - name: Refuse to rename a k3s node or the Proxmox host + ansible.builtin.assert: + that: ansible_facts.hostname == inventory_hostname + fail_msg: "{{ inventory_hostname }} is called {{ ansible_facts.hostname }}. Rename it by hand, not with Ansible." + quiet: true + when: host_type == 'proxmox' or inventory_hostname in groups['k3s'] + tags: [hostname] + + # The hostname module updates ansible_facts.hostname, so keep the old name. + - name: Remember the current hostname + ansible.builtin.set_fact: + utils_old_hostname: "{{ ansible_facts.hostname }}" + tags: [hostname] + + - name: Rename the guest + when: + - utils_old_hostname != inventory_hostname + - host_type != 'proxmox' and inventory_hostname not in groups['k3s'] + tags: [hostname] + block: + - name: Set the hostname in the LXC's Proxmox config + ansible.builtin.command: pct set {{ proxmox_vmid }} --hostname {{ inventory_hostname }} + delegate_to: "{{ groups['hypervisors'] | first }}" + changed_when: true + when: host_type == 'lxc' + + - name: Look for cloud-init + ansible.builtin.stat: + path: /etc/cloud/cloud.cfg + register: utils_cloudinit + + # cloud-init (ViaRézo VM) would put the old name back at boot. + - name: Keep cloud-init from resetting the hostname + ansible.builtin.copy: + dest: /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg + content: "preserve_hostname: true\n" + owner: root + group: root + mode: "0644" + when: utils_cloudinit.stat.exists + + - name: Set the hostname now + ansible.builtin.hostname: + name: "{{ inventory_hostname }}" + + - name: Rename the old hostname in /etc/hosts + ansible.builtin.replace: + path: /etc/hosts + regexp: (?/dev/null + docker builder prune -f >/dev/null + owner: root + group: root + mode: "0755" + when: utils_docker.stat.exists - name: Upgrade hosts: all:!hypervisors @@ -97,7 +350,6 @@ 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