--- # Baseline for every machine, plus an opt-in system upgrade. # Settings: inventories/group_vars/all/utils.yml. # # ansible-playbook playbooks/utils.yml -CD # dry run first # ansible-playbook playbooks/utils.yml # 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 # # 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). # # 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. # - chrony, fstrim, guest agent: VMs and the host. LXCs use the host's clock # and kernel (chrony is stopped there if present). # - reboot after an upgrade: VMs only. LXCs run the host's kernel. - 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 handlers: - name: Restart journald ansible.builtin.systemd_service: name: systemd-journald state: restarted - name: Restart chrony ansible.builtin.systemd_service: name: chrony state: restarted # In a dry run chrony may not be installed yet (VM 107 had timesyncd). ignore_errors: "{{ ansible_check_mode }}" 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: 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 }}" register: utils_mirror tags: [apt] # ----------------------------------------------------------- packages - name: Install admin tools ansible.builtin.apt: name: "{{ utils_packages }}" state: present install_recommends: false update_cache: true cache_valid_time: "{{ 0 if utils_mirror is changed else 3600 }}" tags: [packages] - name: Install the QEMU guest agent (VMs only) ansible.builtin.apt: name: qemu-guest-agent state: present install_recommends: false when: host_type == 'vm' tags: [packages] - 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 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 ansible.builtin.service_facts: tags: [time] - name: Install chrony (not on LXCs) ansible.builtin.apt: name: chrony state: present 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 tags: [time] # ------------------------------------------------------- journald, disk - 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] - 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 weekly (not on LXCs) ansible.builtin.systemd_service: name: fstrim.timer enabled: true state: started when: host_type != 'lxc' tags: [disk] # --------------------------------------------------------------- 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] # ----------------------------------------------------------------- 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 # 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 ansible.builtin.systemd_service: name: ssh state: restarted 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 or the sshd restart failed with the hardening drop-in, so it was removed. Check `systemctl status ssh` on this host." # ------------------------------------------------------------ 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: 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 - 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)