ansible/playbooks/utils.yml

128 lines
4.7 KiB
YAML
Raw Normal View History

2026-10-11 22:29:14 +02:00
---
# 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)