ansible/playbooks/utils.yml

634 lines
22 KiB
YAML
Raw Normal View History

2026-10-11 22:29:14 +02:00
---
# Baseline for every machine, plus an opt-in system upgrade.
2026-10-11 23:02:48 +02:00
# Settings: inventories/group_vars/all/utils.yml.
2026-10-11 22:29:14 +02:00
#
2026-10-11 23:02:48 +02:00
# ansible-playbook playbooks/utils.yml -CD # dry run first
2026-10-11 22:29:14 +02:00
# ansible-playbook playbooks/utils.yml
2026-10-11 23:02:48 +02:00
# ansible-playbook playbooks/utils.yml --tags ssh,motd # only some parts
# ansible-playbook playbooks/utils.yml --tags upgrade -l lxc
2026-10-11 22:29:14 +02:00
# ansible-playbook playbooks/utils.yml --tags upgrade -l k3s_workers -e utils_reboot=true
#
2026-10-11 23:13:58 +02:00
# Tags: apt, packages, locale, time, journald, disk, shell, ssh, hostname,
# motd, proxmox, docker, k9s, upgrade (opt-in).
#
# Ported from ViaRézo's util role: CLI tools, locales, timezone, chrony,
# zsh + oh-my-zsh + oh-my-viarezo, vimrc, screenrc, apt-listchanges, Ubuntu motd
# cleanup, AZERTY on bare metal, k9s. Left out: fail2ban (every WireGuard peer
# reaches the guests as 10.0.0.250, so one ban locks everyone out) and the
# ViaRézo resolvers (the host keeps 1.1.1.1).
2026-10-11 23:02:48 +02:00
#
# 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.
2026-10-11 23:13:58 +02:00
# - chrony, fstrim, guest agent: VMs and the host. LXCs use the host's clock
# and kernel (chrony is stopped there if present).
2026-10-11 23:02:48 +02:00
# - reboot after an upgrade: VMs only. LXCs run the host's kernel.
2026-10-11 22:29:14 +02:00
- name: Baseline
hosts: all
gather_facts: true
gather_subset: [min]
vars:
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
2026-10-11 23:02:48 +02:00
handlers:
- name: Restart journald
ansible.builtin.systemd_service:
name: systemd-journald
state: restarted
2026-10-11 23:13:58 +02:00
- name: Restart chrony
ansible.builtin.systemd_service:
name: chrony
state: restarted
2026-10-11 23:23:23 +02:00
# In a dry run chrony may not be installed yet (VM 107 had timesyncd).
ignore_errors: "{{ ansible_check_mode }}"
2026-10-11 23:13:58 +02:00
2026-10-11 22:29:14 +02:00
tasks:
2026-10-11 23:02:48 +02:00
# ---------------------------------------------------------------- apt
2026-10-11 22:29:14 +02:00
- 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
2026-10-11 23:02:48 +02:00
tags: [apt]
2026-10-11 22:29:14 +02:00
- 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 }}"
2026-10-11 23:02:48 +02:00
register: utils_mirror
tags: [apt]
2026-10-11 22:29:14 +02:00
2026-10-11 23:02:48 +02:00
# ----------------------------------------------------------- packages
- name: Install admin tools
2026-10-11 22:29:14 +02:00
ansible.builtin.apt:
name: "{{ utils_packages }}"
state: present
install_recommends: false
update_cache: true
2026-10-11 23:02:48 +02:00
cache_valid_time: "{{ 0 if utils_mirror is changed else 3600 }}"
tags: [packages]
2026-10-11 22:29:14 +02:00
- name: Install the QEMU guest agent (VMs only)
ansible.builtin.apt:
name: qemu-guest-agent
state: present
install_recommends: false
when: host_type == 'vm'
2026-10-11 23:02:48 +02:00
tags: [packages]
2026-10-11 22:29:14 +02:00
- 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
2026-10-11 23:02:48 +02:00
tags: [packages]
# ------------------------------------------------------------- locale
- name: Install the locales package
ansible.builtin.apt:
name: locales
state: present
tags: [locale]
2026-10-11 22:29:14 +02:00
2026-10-11 23:02:48 +02:00
- 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
2026-10-11 22:29:14 +02:00
- name: Set the timezone
community.general.timezone:
name: "{{ utils_timezone }}"
when: utils_timezone is defined
2026-10-11 23:02:48 +02:00
tags: [time]
2026-10-11 23:13:58 +02:00
- name: List services
2026-10-11 23:02:48 +02:00
ansible.builtin.service_facts:
tags: [time]
2026-10-11 23:13:58 +02:00
- name: Install chrony (not on LXCs)
2026-10-11 23:02:48 +02:00
ansible.builtin.apt:
2026-10-11 23:13:58 +02:00
name: chrony
2026-10-11 23:02:48 +02:00
state: present
2026-10-11 23:13:58 +02:00
when: host_type != 'lxc'
tags: [time]
# Installed after chrony, so the clock is never left without a sync daemon.
- name: Remove the other NTP clients (not on LXCs)
ansible.builtin.apt:
name: [ntp, ntpdate, systemd-timesyncd]
state: absent
when: host_type != 'lxc'
tags: [time]
- name: Configure chrony (not on LXCs)
ansible.builtin.template:
src: chrony.conf.j2
dest: /etc/chrony/chrony.conf
owner: root
group: root
mode: "0644"
when: host_type != 'lxc'
notify: Restart chrony
tags: [time]
- name: Start chrony (not on LXCs)
ansible.builtin.systemd_service:
name: chrony
enabled: true
state: started
when: host_type != 'lxc' and not ansible_check_mode
tags: [time]
- name: Stop chrony on LXCs (they use the host's clock)
ansible.builtin.systemd_service:
name: chrony
enabled: false
state: stopped
when: host_type == 'lxc' and 'chrony.service' in ansible_facts.services
2026-10-11 23:02:48 +02:00
tags: [time]
# ------------------------------------------------------- journald, disk
2026-10-11 23:23:23 +02:00
- name: Create /etc/systemd/journald.conf.d
ansible.builtin.file:
path: /etc/systemd/journald.conf.d
state: directory
owner: root
group: root
mode: "0755"
tags: [journald]
2026-10-11 23:02:48 +02:00
- 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]
2026-10-11 23:13:58 +02:00
- name: Trim free space weekly (not on LXCs)
2026-10-11 23:02:48 +02:00
ansible.builtin.systemd_service:
name: fstrim.timer
enabled: true
state: started
2026-10-11 23:13:58 +02:00
when: host_type != 'lxc'
2026-10-11 23:02:48 +02:00
tags: [disk]
2026-10-11 23:13:58 +02:00
# --------------------------------------------------------------- shell
- name: Install zsh, screen and apt-listchanges
ansible.builtin.apt:
name: [zsh, screen, apt-listchanges]
state: present
tags: [shell]
- name: Configure apt-listchanges
ansible.builtin.copy:
src: listchanges.conf
dest: /etc/apt/listchanges.conf
owner: root
group: root
mode: "0644"
tags: [shell]
- name: Configure vim for everyone
ansible.builtin.copy:
src: vimrc
dest: /etc/vim/vimrc
owner: root
group: root
mode: "0644"
tags: [shell]
- name: Configure screen for root
ansible.builtin.copy:
src: screenrc
dest: /root/.screenrc
owner: root
group: root
mode: "0644"
tags: [shell]
# Cloned once; -e utils_zsh_update=true pulls the latest versions.
- name: Clone oh-my-zsh, the oh-my-viarezo theme and the ViaRézo plugins
ansible.builtin.git:
repo: "{{ item.repo }}"
dest: "{{ item.dest }}"
version: master
update: "{{ utils_zsh_update | default(false) | bool }}"
force: "{{ utils_zsh_update | default(false) | bool }}"
loop:
- repo: https://github.com/ohmyzsh/ohmyzsh.git
dest: /etc/zsh/.oh-my-zsh
- repo: https://gitlab.viarezo.fr/ViaRezo/oh-my-viarezo.git
dest: /etc/zsh/.oh-my-viarezo/themes/oh-my-viarezo
- repo: https://gitlab.viarezo.fr/ViaRezo/zsh-plugins.git
dest: /etc/zsh/.oh-my-viarezo/plugins
loop_control:
label: "{{ item.dest }}"
# In a dry run git may not be installed yet.
ignore_errors: "{{ ansible_check_mode }}"
tags: [shell]
- name: Install the global zshrc
ansible.builtin.copy:
src: zshrc
dest: /etc/zsh/zshrc
owner: root
group: root
mode: "0644"
tags: [shell]
- name: Create an empty .zshrc for root (skips zsh's first-run wizard)
ansible.builtin.copy:
content: ""
dest: /root/.zshrc
force: false
owner: root
group: root
mode: "0644"
tags: [shell]
# un-oeil's shell is set by add_user.yml (users.yml).
- name: Make zsh root's shell
ansible.builtin.user:
name: root
shell: /bin/zsh
tags: [shell]
- name: Disable Ubuntu's motd news
ansible.builtin.copy:
src: motd-news
dest: /etc/default/motd-news
owner: root
group: root
mode: "0644"
when: ansible_facts.distribution == 'Ubuntu'
tags: [shell, motd]
- name: Remove Ubuntu's motd ads
ansible.builtin.file:
path: /etc/update-motd.d/{{ item }}
state: absent
loop: [10-help-text, 50-motd-news, 51-cloudguest, 80-livepatch]
when: ansible_facts.distribution == 'Ubuntu'
tags: [shell, motd]
2026-10-11 23:02:48 +02:00
# ----------------------------------------------------------------- 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
2026-10-11 23:32:22 +02:00
# Restart, not reload: on the Debian 13 LXCs ssh.socket holds port 22
# and passes it to sshd. A reload (SIGHUP) re-execs sshd without that
# socket, it fails to bind and dies. Open sessions survive a restart
# (KillMode=process), and systemd runs sshd -t first (ExecStartPre).
- name: Restart sshd
2026-10-11 23:02:48 +02:00
ansible.builtin.systemd_service:
name: ssh
2026-10-11 23:32:22 +02:00
state: restarted
2026-10-11 23:02:48 +02:00
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:
2026-10-11 23:32:22 +02:00
msg: "sshd -t or the sshd restart failed with the hardening drop-in, so it was removed. Check `systemctl status ssh` on this host."
2026-10-11 23:02:48 +02:00
# ------------------------------------------------------------ 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: (?<![\w.-]){{ utils_old_hostname | regex_escape }}(?![\w.-])
replace: "{{ inventory_hostname }}"
# ---------------------------------------------------------------- motd
- name: Write the motd
ansible.builtin.template:
src: motd.j2
dest: /etc/motd
owner: root
group: root
mode: "0644"
tags: [motd]
- name: Proxmox host
hosts: hypervisors
gather_facts: false
tags: [proxmox]
vars:
# Written exactly as `iptables -t nat -S` prints them, minus "-A ".
utils_public_nat_rules:
- PREROUTING -i vmbr0 -p tcp -m tcp --dport 443 -j DNAT --to-destination 10.0.0.10:443
- PREROUTING -i vmbr0 -p tcp -m tcp --dport 80 -j DNAT --to-destination 10.0.0.10:80
- PREROUTING -i vmbr0 -p tcp -m tcp --dport 2222 -j DNAT --to-destination 10.0.0.14:2222
- PREROUTING -d 138.195.128.200/32 -i vmbr1 -p tcp -m multiport --dports 80,443 -j DNAT --to-destination 10.0.0.10
- PREROUTING -d 138.195.128.200/32 -i vmbr1 -p tcp -m tcp --dport 2222 -j DNAT --to-destination 10.0.0.14:2222
- POSTROUTING -s 10.0.0.0/24 -m conntrack --ctstate DNAT --ctorigdst 138.195.128.200 -j MASQUERADE
2026-10-11 23:02:48 +02:00
tasks:
# The file was fully commented out, so the host got no PVE updates at all.
- name: Enable the pve-no-subscription repository
ansible.builtin.copy:
dest: /etc/apt/sources.list.d/proxmox.sources
content: |
Types: deb
URIs: http://download.proxmox.com/debian/pve
Suites: trixie
Components: pve-no-subscription
Signed-By: /usr/share/keyrings/proxmox-archive-keyring.gpg
owner: root
group: root
mode: "0644"
register: utils_pve_repo
- name: Refresh apt
ansible.builtin.apt:
update_cache: true
when: utils_pve_repo is changed
2026-10-11 23:13:58 +02:00
# The console (BMC / physical) keyboard. Already fr via console-setup.
- name: AZERTY keyboard on the console
ansible.builtin.lineinfile:
path: /etc/default/keyboard
regexp: ^XKBLAYOUT=
line: XKBLAYOUT="fr"
2026-10-11 23:02:48 +02:00
# WireGuard on udp/443 for networks that block 51820. Live since
# 2026-10-11 but never saved, so a reboot would drop it.
- name: Forward udp/443 to the WireGuard hub (live)
ansible.builtin.iptables:
table: nat
chain: PREROUTING
in_interface: vmbr0
protocol: udp
match: [udp]
destination_port: "443"
jump: DNAT
to_destination: 10.0.0.250:51820
- name: Forward udp/443 to the WireGuard hub (rules.v4)
ansible.builtin.lineinfile:
path: /etc/iptables/rules.v4
line: -A PREROUTING -i vmbr0 -p udp -m udp --dport 443 -j DNAT --to-destination 10.0.0.250:51820
insertafter: ^-A PREROUTING -i vmbr0 -p udp -m udp --dport 51820 -j DNAT
# Public web (2026-10-30): tcp 80/443 -> Caddy, 2222 -> Forgejo SSH. Caddy
# aborts every site not meant for the internet (wg_only snippet), so only
# vault, forgejo, md, pad, pterodactyl and wings answer from outside.
# The vmbr1 rules are hairpin NAT: guests that resolve a public name reach
# Caddy too, and show up as 10.0.0.1 (allowed by wg_only).
- name: Public web and Forgejo SSH (live)
ansible.builtin.command: iptables -t nat -C {{ item }}
register: utils_public_nat
changed_when: false
failed_when: false
loop: "{{ utils_public_nat_rules }}"
- name: Public web and Forgejo SSH (add missing live rules)
ansible.builtin.command: iptables -t nat -A {{ item.item }}
loop: "{{ utils_public_nat.results | selectattr('rc', '!=', 0) | list }}"
loop_control:
label: "{{ item.item }}"
changed_when: true
- name: Public web and Forgejo SSH (rules.v4)
ansible.builtin.lineinfile:
path: /etc/iptables/rules.v4
line: -A {{ item }}
insertbefore: ^-A POSTROUTING -s 10.0.0.0/24 -o vmbr0 -j MASQUERADE
firstmatch: true
loop: "{{ utils_public_nat_rules }}"
- name: WireGuard hub
hosts: wireguard
gather_facts: false
tags: [wireguard]
tasks:
# The hub routes 10.10.10.0/24 to 10.0.0.0/24. Forwarding had only been
# switched on by hand, so after the 2026-10-14 reboot the tunnel came up but
# no traffic reached the LAN. systemd-sysctl applies this file at boot (the
# LXC has no sysctl binary, hence the /proc write for the live value).
- name: Enable IP forwarding at boot
ansible.builtin.copy:
dest: /etc/sysctl.d/99-wireguard-forward.conf
content: "net.ipv4.ip_forward = 1\n"
owner: root
group: root
mode: "0644"
- name: Enable IP forwarding now
ansible.builtin.shell: |
if [ "$(cat /proc/sys/net/ipv4/ip_forward)" != 1 ]; then
echo 1 > /proc/sys/net/ipv4/ip_forward && echo changed
fi
register: utils_wg_forward
changed_when: "'changed' in utils_wg_forward.stdout"
2026-10-11 23:02:48 +02:00
- name: Docker hosts
hosts: all:!hypervisors:!k3s
gather_facts: false
tags: [docker]
tasks:
- name: Look for Docker
ansible.builtin.stat:
path: /usr/bin/docker
register: utils_docker
# Dangling images only (old layers left by updates). `prune -a` would also
# delete images no container uses right now, like Overleaf's 9 GB TeX Live
# image that only throwaway compile containers use, and break compiles.
- name: Prune dangling Docker images weekly
ansible.builtin.copy:
dest: /etc/cron.weekly/docker-prune
content: |
#!/bin/sh
# Managed by Ansible (utils.yml)
docker image prune -f >/dev/null
docker builder prune -f >/dev/null
owner: root
group: root
mode: "0755"
when: utils_docker.stat.exists
2026-10-11 22:29:14 +02:00
2026-10-11 23:13:58 +02:00
- name: K9s on the k3s masters
hosts: k3s_masters
gather_facts: false
tags: [k9s]
tasks:
- name: Read the installed k9s version
ansible.builtin.command: k9s version --short
register: utils_k9s_installed
changed_when: false
failed_when: false
check_mode: false
- name: Install k9s {{ utils_k9s_version }}
ansible.builtin.apt:
deb: https://github.com/derailed/k9s/releases/download/{{ utils_k9s_version }}/k9s_linux_amd64.deb
when: utils_k9s_version not in utils_k9s_installed.stdout | default('')
# So `sudo k9s` (and kubectl) find the cluster without KUBECONFIG.
- name: Create /root/.kube
ansible.builtin.file:
path: /root/.kube
state: directory
owner: root
group: root
mode: "0700"
- name: Point root's kubeconfig at k3s's admin config
ansible.builtin.file:
src: /etc/rancher/k3s/k3s.yaml
dest: /root/.kube/config
state: link
2026-10-11 22:29:14 +02:00
- 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
- 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)