From 52d06204bbec6dd393d76a6c01de41fbb8b190bb Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sat, 14 Nov 2026 22:11:36 +0000 Subject: [PATCH] feat(system_baseline): add an update toggle and pin the pve unattended-upgrades origins system_baseline_update (default true) lets the main flow skip the package update for hosts patched by a separate, operator-run flow. The update entry point does not read it: calling it is the request to update. The new profile key unattended_upgrade_origins is rendered into /etc/apt/apt.conf.d/90unattended-upgrades-origins, which #clears both origin lists before setting the pinned patterns, because apt appends list entries across files and a plain drop-in could only widen them. The pve profile pins Debian's point-release and security origins, never the Proxmox repository; the ubuntu profiles pin nothing and are unchanged. Refs defame1297/ansible-homelab-mono#349 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm --- .../system-baseline-update-toggle.yml | 11 ++++ roles/system_baseline/README.md | 22 ++++++- roles/system_baseline/defaults/main.yml | 19 ++++++ roles/system_baseline/meta/argument_specs.yml | 26 ++++++-- .../molecule/default/converge.yml | 4 ++ .../molecule/default/verify.yml | 60 +++++++++++++++++++ roles/system_baseline/tasks/main.yml | 2 + .../tasks/unattended_upgrades.yml | 19 ++++++ roles/system_baseline/tasks/validate.yml | 14 +++++ .../templates/unattended-upgrades-origins.j2 | 11 ++++ 10 files changed, 181 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/system-baseline-update-toggle.yml create mode 100644 roles/system_baseline/templates/unattended-upgrades-origins.j2 diff --git a/changelogs/fragments/system-baseline-update-toggle.yml b/changelogs/fragments/system-baseline-update-toggle.yml new file mode 100644 index 0000000..3298a5a --- /dev/null +++ b/changelogs/fragments/system-baseline-update-toggle.yml @@ -0,0 +1,11 @@ +--- +minor_changes: + - >- + system_baseline - add ``system_baseline_update`` (default ``true``). With ``false`` the main flow skips the package + update, for hosts patched by a separate flow. The ``update`` entry point does not read it. + - >- + system_baseline - add the profile key ``unattended_upgrade_origins`` (resolved as ``system_baseline_unattended_upgrade_origins``). + A non-empty list is written to ``/etc/apt/apt.conf.d/90unattended-upgrades-origins``, which clears the earlier + ``Allowed-Origins`` and ``Origins-Pattern`` lists and allows only the listed patterns. The ``pve`` profile now pins Debian's + point-release and security origins, so the Proxmox repository is never upgraded unattended and a future Debian default + cannot widen the set. The ``ubuntu_lxc`` and ``ubuntu_vm`` profiles pin nothing and are unchanged. diff --git a/roles/system_baseline/README.md b/roles/system_baseline/README.md index a259d79..a57711e 100644 --- a/roles/system_baseline/README.md +++ b/roles/system_baseline/README.md @@ -18,7 +18,7 @@ Ubuntu 24.04 (noble, covered by the molecule scenario), Ubuntu 22.04 (jammy) and ## Role variables The resolved `system_baseline_*` inputs derived from the active profile (`_profile`, `_essential_packages`, `_remove_packages`, `_apt_periodic_settings`, -`_unattended_upgrade_settings`, `_sysctl_hardening`, `_secure_tmp_mounts`) are listed below but +`_unattended_upgrade_settings`, `_unattended_upgrade_origins`, `_sysctl_hardening`, `_secure_tmp_mounts`) are listed below but deliberately not in the spec: their defaults index the profile by `system_baseline_system_type`, and the spec is templated before `validate.yml` runs, so an invalid `system_baseline_system_type` would otherwise fail with a templating error instead of the role's own message. @@ -33,6 +33,7 @@ templating error instead of the role's own message. | `system_baseline_remove_packages` | from profile | no | Packages to purge when present | | `system_baseline_apt_periodic_settings` | from profile | no | Keys for `20auto-upgrades` | | `system_baseline_unattended_upgrade_settings` | from profile | no | Keys for `50unattended-upgrades` | +| `system_baseline_unattended_upgrade_origins` | from profile | no | Pinned `Origins-Pattern` entries; `[]` keeps the distribution default (see below) | | `system_baseline_sysctl_hardening` | from profile | no | List of `name` and `value` sysctl pairs | | `system_baseline_secure_tmp_mounts` | from profile | no | Paths remounted as tmpfs with `noexec,nosuid,nodev` | | `system_baseline_journal_max_use` | `200M` | no | Upper bound of the persistent journal (journald `SystemMaxUse`). | @@ -42,6 +43,7 @@ templating error instead of the role's own message. | `system_baseline_timezone` | `Etc/UTC` | no | Timezone | | `system_baseline_locale` | `en_US.UTF-8` | no | Locale | | `system_baseline_reboot_if_required` | `true` | no | Reboot when `/var/run/reboot-required` exists | +| `system_baseline_update` | `true` | no | Run the package update in the main flow; `false` skips it (the `update` entry point ignores it) | A flat `system_baseline_*` input replaces that value entirely. To change one key of a profile dict, use `system_baseline_profile_overrides`, for example to let unattended-upgrades reboot a group on its own: @@ -56,7 +58,7 @@ system_baseline_profile_overrides: Besides `main`, two task files are a supported interface, for a caller that drives its own reboot (for example a Kubernetes node patch flow that cordons and drains first). Their names, the fact name and the behaviour below follow the Collection's semantic versioning: changing them is a breaking change. -- `tasks_from: update`: apt update, upgrade, autoremove and autoclean, nothing else. +- `tasks_from: update`: apt update, upgrade, autoremove and autoclean, nothing else. It does not read `system_baseline_update`: calling the entry point is the request to update. - `tasks_from: reboot`: sets the fact `system_baseline_reboot_required` (bool, from `/var/run/reboot-required`) on Debian family hosts, then reboots when `system_baseline_reboot_if_required` is true and the fact is true. With `system_baseline_reboot_if_required: false` it only computes the fact. On other families the fact is not set, so read it with `default(false)`. ```yaml @@ -113,11 +115,25 @@ system_baseline_tmux_windows: - The windows are made when the session is created; a login that re-attaches adds none, so a change applies from the next new session. A window closes when its command exits. A window command runs with `sh -c` from the tmux server of the first login, before `~/.profile` is read, so it does not see `~/.local/bin` and other additions made there: use absolute paths. - Security: the name and commands are validated (`A-Za-z0-9_-`, one line) and shell-quoted, and come from the inventory, never from a user. The file is root-owned and not writable by its users. The commands run as the user who logs in, are readable by every user and show in `ps`: no secrets in a command. A session outlives the SSH connection, so a root session stays open until it is closed. +## Skipping the package update + +`system_baseline_update: false` skips the package update of the main flow, for hosts patched by a separate, operator-run flow (for example a full upgrade with a rolling reboot). Everything else, unattended-upgrades included, still runs. The reboot check still runs as well, so set `system_baseline_reboot_if_required: false` too when that flow owns the reboot. Running the play with `--skip-tags update` skips the same task for one run only, and also the reboot. + +## Unattended-upgrades origins + +A profile's `unattended_upgrade_origins` list pins the origins unattended-upgrades may install from. apt merges every file in `/etc/apt/apt.conf.d` and appends list entries across files, so a later file that only lists origins would widen the distribution's `50unattended-upgrades`, never narrow it. A non-empty list is therefore written to `/etc/apt/apt.conf.d/90unattended-upgrades-origins`, which sorts after `50unattended-upgrades`, clears `Unattended-Upgrade::Allowed-Origins` and `Unattended-Upgrade::Origins-Pattern` (apt's `#clear` directive) and sets only the listed patterns. The origin lists in `50unattended-upgrades` itself are not edited. An empty or missing list removes the drop-in. + +- `pve` pins Debian's own default set: `origin=Debian,codename=${distro_codename},label=Debian` (point releases) and the two `Debian-Security` patterns. The Proxmox repository is never allowed, and a future Debian default cannot widen the set. +- `ubuntu_lxc` and `ubuntu_vm` pin nothing (`[]`): Ubuntu's default origins apply. +- `${distro_codename}` is expanded by unattended-upgrades, not by Ansible. An entry may not hold a double quote, a backslash or a line break. +- A file in `/etc/apt/apt.conf.d` that sorts after `90unattended-upgrades-origins` can still add origins. +- Check the merged result on a host with `apt-config dump | grep -E 'Unattended-Upgrade::(Origins-Pattern|Allowed-Origins)'`. + ## Notes - The `pve` profile has no `ufw`; the `ubuntu_*` profiles install it and `fail2ban`. - No profile lets unattended-upgrades reboot on its own (`Automatic-Reboot: false`); a host opts in through `system_baseline_profile_overrides`. - Set `system_baseline_reboot_if_required: false` for hosts whose reboot takes other machines down (hypervisors) or needs a drain first (cluster nodes). -- Molecule skips `sysctl`, `secure_tmp`, `update` and `timezone` (no privileged container); see `molecule/default/molecule.yml`. +- Molecule skips `sysctl`, `secure_tmp`, `update` and `timezone` (no privileged container); see `molecule/default/molecule.yml`. So `system_baseline_update` is checked only for its default; the skip itself is not exercised by a test. The `pve` origins pin is applied to the Ubuntu container and read back with `apt-config dump`. - Policy lives in defaults, not `vars/`, so group_vars and host_vars can override it. - `no_log` is not used: the role handles no secrets. diff --git a/roles/system_baseline/defaults/main.yml b/roles/system_baseline/defaults/main.yml index 64a213b..213d7be 100644 --- a/roles/system_baseline/defaults/main.yml +++ b/roles/system_baseline/defaults/main.yml @@ -4,6 +4,9 @@ system_baseline_timezone: "Etc/UTC" system_baseline_locale: "en_US.UTF-8" system_baseline_reboot_if_required: true +# Run the package update (tasks/update.yml) in the main flow. false skips it, for hosts that are patched by a separate, +# operator-run flow; the tasks_from: update entry point does not read it. +system_baseline_update: true # Upper bound of the persistent journal (journald SystemMaxUse); older archived files are removed above it. system_baseline_journal_max_use: 200M # Attach an interactive SSH login to a tmux session (see tasks/tmux.yml). Off by default: it changes what every login does. @@ -70,6 +73,14 @@ system_baseline_profiles: "Unattended-Upgrade::Remove-Unused-Dependencies": true "Unattended-Upgrade::MinimalSteps": true + # The only origins unattended-upgrades may install from: Debian's point releases and Debian-Security, never the + # Proxmox repository. This is Debian 13's own default set, pinned so that a future default cannot widen it. + # ${distro_codename} is expanded by unattended-upgrades, not by Ansible. [] leaves the distribution default alone. + unattended_upgrade_origins: + - "origin=Debian,codename=${distro_codename},label=Debian" + - "origin=Debian,codename=${distro_codename},label=Debian-Security" + - "origin=Debian,codename=${distro_codename}-security,label=Debian-Security" + sysctl_hardening: - { name: "net.ipv4.tcp_syncookies", value: 1 } - { name: "kernel.randomize_va_space", value: 2 } @@ -129,6 +140,9 @@ system_baseline_profiles: "Unattended-Upgrade::Remove-Unused-Dependencies": true "Unattended-Upgrade::MinimalSteps": true + # Not pinned: the distribution default origins apply. + unattended_upgrade_origins: [] + sysctl_hardening: - { name: "net.ipv4.tcp_syncookies", value: 1 } - { name: "net.ipv4.conf.all.accept_source_route", value: 0 } @@ -184,6 +198,9 @@ system_baseline_profiles: "Unattended-Upgrade::Remove-Unused-Dependencies": true "Unattended-Upgrade::MinimalSteps": true + # Not pinned: the distribution default origins apply. + unattended_upgrade_origins: [] + sysctl_hardening: - { name: "net.ipv4.tcp_syncookies", value: 1 } - { name: "kernel.randomize_va_space", value: 2 } @@ -211,5 +228,7 @@ system_baseline_essential_packages: "{{ system_baseline_profile.essential_packag system_baseline_remove_packages: "{{ system_baseline_profile.remove_packages }}" system_baseline_apt_periodic_settings: "{{ system_baseline_profile.apt_periodic_settings }}" system_baseline_unattended_upgrade_settings: "{{ system_baseline_profile.unattended_upgrade_settings }}" +# default([]): a system_baseline_profiles written before this key existed keeps the distribution default. +system_baseline_unattended_upgrade_origins: "{{ system_baseline_profile.unattended_upgrade_origins | default([]) }}" system_baseline_sysctl_hardening: "{{ system_baseline_profile.sysctl_hardening }}" system_baseline_secure_tmp_mounts: "{{ system_baseline_profile.secure_tmp_mounts }}" diff --git a/roles/system_baseline/meta/argument_specs.yml b/roles/system_baseline/meta/argument_specs.yml index 0ead5bd..afd2112 100644 --- a/roles/system_baseline/meta/argument_specs.yml +++ b/roles/system_baseline/meta/argument_specs.yml @@ -5,13 +5,14 @@ argument_specs: description: - >- Validates C(system_baseline_system_type) and, when C(system_baseline_tmux_autostart) is true, the tmux session name and - windows, then runs the update, packages, unattended-upgrades, locale, timezone, journald, - sysctl, tmpfs, tmux and reboot task files, in that order. + windows, then runs the update (unless C(system_baseline_update) is false), packages, unattended-upgrades, locale, timezone, + journald, sysctl, tmpfs, tmux and reboot task files, in that order. - Needs gathered facts, because the reboot check reads C(ansible_os_family). - >- The resolved inputs C(system_baseline_profile), C(system_baseline_essential_packages), C(system_baseline_remove_packages), C(system_baseline_apt_periodic_settings), C(system_baseline_unattended_upgrade_settings), - C(system_baseline_sysctl_hardening) and C(system_baseline_secure_tmp_mounts) are deliberately not listed. Their defaults are + C(system_baseline_unattended_upgrade_origins), C(system_baseline_sysctl_hardening) and C(system_baseline_secure_tmp_mounts) are + deliberately not listed. Their defaults are Jinja that indexes C(system_baseline_profiles) with C(system_baseline_system_type), and the argument spec is validated (templated) before tasks/validate.yml runs, so listing them would fail on an invalid C(system_baseline_system_type) with a templating error ahead of the role's own C(is invalid) message. They are derived from the profile and may be set directly to replace one resolved value @@ -30,7 +31,14 @@ argument_specs: description: - >- Policy per system type. Each profile holds C(essential_packages), C(remove_packages), C(apt_periodic_settings), - C(unattended_upgrade_settings), C(sysctl_hardening) and C(secure_tmp_mounts). + C(unattended_upgrade_settings), C(unattended_upgrade_origins), C(sysctl_hardening) and C(secure_tmp_mounts). + - >- + C(unattended_upgrade_origins) is a list of unattended-upgrades C(Origins-Pattern) entries. A non-empty list is written to + C(/etc/apt/apt.conf.d/90unattended-upgrades-origins), which clears C(Unattended-Upgrade::Allowed-Origins) and + C(Unattended-Upgrade::Origins-Pattern) and sets only these entries, so they replace the distribution default instead of adding + to it. An empty or missing list removes that file and leaves the distribution default. The C(pve) profile pins Debian's + point-release and security origins; the C(ubuntu_*) profiles pin nothing. Entries may not hold a double quote, a backslash or a + line break. - Overriding it replaces all profiles. To change part of one profile use C(system_baseline_profile_overrides). system_baseline_profile_overrides: type: dict @@ -94,10 +102,20 @@ argument_specs: default: true description: - Reboot when C(/var/run/reboot-required) exists after the run. + system_baseline_update: + type: bool + default: true + description: + - >- + Run the package update (apt update, upgrade, autoremove and autoclean) in this flow. false skips it, for hosts patched by a + separate flow; the reboot check still runs, so set C(system_baseline_reboot_if_required) as well when that flow also owns + the reboot. + - The C(update) entry point does not read it. update: short_description: Update and upgrade apt packages description: - Runs an apt update, upgrade, autoremove and autoclean, and nothing else. + - Calling it is a request to update, so it does not read C(system_baseline_update), which only gates the main flow. - >- A supported entry point (C(tasks_from=update)) for a caller that drives its own reboot, for example a Kubernetes node patch flow that drains the node first. diff --git a/roles/system_baseline/molecule/default/converge.yml b/roles/system_baseline/molecule/default/converge.yml index 771f485..d554981 100644 --- a/roles/system_baseline/molecule/default/converge.yml +++ b/roles/system_baseline/molecule/default/converge.yml @@ -53,6 +53,9 @@ - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot'] == false - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot-Time'] == '03:00' - system_baseline_secure_tmp_mounts == ['/dev/shm', '/tmp', '/var/tmp'] + - system_baseline_update + - system_baseline_unattended_upgrade_origins | length == 3 + - system_baseline_unattended_upgrade_origins | select('search', 'Proxmox') | list == [] quiet: true - name: The ubuntu_vm profile resolves @@ -74,6 +77,7 @@ - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot'] == false - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot-Time'] == '04:00' - system_baseline_sysctl_hardening | selectattr('name', 'equalto', 'net.ipv4.conf.all.log_martians') | list | length == 1 + - system_baseline_unattended_upgrade_origins == [] quiet: true # Consumer play vars override the profile. diff --git a/roles/system_baseline/molecule/default/verify.yml b/roles/system_baseline/molecule/default/verify.yml index 1d1589d..8303d0d 100644 --- a/roles/system_baseline/molecule/default/verify.yml +++ b/roles/system_baseline/molecule/default/verify.yml @@ -209,3 +209,63 @@ ansible.builtin.assert: that: not system_baseline_verify_removed.stat.exists quiet: true + +# The pve origins pin, run on this Ubuntu host: Ubuntu's 50unattended-upgrades sets Allowed-Origins, so the merged +# configuration showing only the pinned patterns proves the drop-in clears the earlier lists instead of appending to them. +# The patterns match no Ubuntu origin, which only means nothing would be upgraded; the role's dry run still passes. Here +# and not in converge, because switching profiles and back would not be idempotent. +- name: The pve profile pins the unattended-upgrades origins + hosts: all + gather_facts: false + tasks: + - name: Stat the origins drop-in after the ubuntu_lxc converge + ansible.builtin.stat: + path: /etc/apt/apt.conf.d/90unattended-upgrades-origins + register: system_baseline_verify_origins_before + + - name: The ubuntu_lxc profile pins nothing + ansible.builtin.assert: + that: not system_baseline_verify_origins_before.stat.exists + quiet: true + + - name: Run the unattended-upgrades tasks with the pve profile + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + tasks_from: unattended_upgrades + vars: + system_baseline_system_type: pve + + - name: Dump the merged apt configuration + ansible.builtin.command: apt-config dump + register: system_baseline_verify_apt_config + changed_when: false + + - name: Only the pinned Debian origins are allowed + ansible.builtin.assert: + that: + - >- + system_baseline_verify_apt_config.stdout_lines | select('match', 'Unattended-Upgrade::Origins-Pattern::\s') | list == + ['Unattended-Upgrade::Origins-Pattern:: "origin=Debian,codename=${distro_codename},label=Debian";', + 'Unattended-Upgrade::Origins-Pattern:: "origin=Debian,codename=${distro_codename},label=Debian-Security";', + 'Unattended-Upgrade::Origins-Pattern:: "origin=Debian,codename=${distro_codename}-security,label=Debian-Security";'] + - system_baseline_verify_apt_config.stdout_lines | select('match', 'Unattended-Upgrade::Allowed-Origins::\s') | list == [] + quiet: true + + - name: Run the unattended-upgrades tasks with the ubuntu_lxc profile again + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + tasks_from: unattended_upgrades + vars: + system_baseline_system_type: ubuntu_lxc + + - name: Dump the merged apt configuration again + ansible.builtin.command: apt-config dump + register: system_baseline_verify_apt_config_restored + changed_when: false + + - name: The drop-in is gone and the distribution origins are back + ansible.builtin.assert: + that: + - system_baseline_verify_apt_config_restored.stdout_lines | select('match', 'Unattended-Upgrade::Allowed-Origins::\s') | list | length > 0 + - system_baseline_verify_apt_config_restored.stdout is not search('origin=Debian') + quiet: true diff --git a/roles/system_baseline/tasks/main.yml b/roles/system_baseline/tasks/main.yml index 28b8707..3be8052 100644 --- a/roles/system_baseline/tasks/main.yml +++ b/roles/system_baseline/tasks/main.yml @@ -3,8 +3,10 @@ ansible.builtin.import_tasks: validate.yml tags: always +# Only the main flow honours system_baseline_update: the tasks_from: update entry point is an explicit request to update. - name: Update & Upgrade Dist ansible.builtin.import_tasks: update.yml + when: system_baseline_update | bool tags: update - name: Manage packages diff --git a/roles/system_baseline/tasks/unattended_upgrades.yml b/roles/system_baseline/tasks/unattended_upgrades.yml index dd50b95..8409820 100644 --- a/roles/system_baseline/tasks/unattended_upgrades.yml +++ b/roles/system_baseline/tasks/unattended_upgrades.yml @@ -21,6 +21,25 @@ mode: "0644" loop: "{{ system_baseline_unattended_upgrade_settings | dict2items }}" +# apt appends list entries across apt.conf.d files, so a later file cannot narrow the origins of 50unattended-upgrades by +# listing its own: the drop-in clears both origin lists first (#clear) and then sets the pinned ones. It sorts after 50. +- name: Pin the unattended-upgrades origins + become: true + ansible.builtin.template: + src: unattended-upgrades-origins.j2 + dest: /etc/apt/apt.conf.d/90unattended-upgrades-origins + owner: root + group: root + mode: "0644" + when: system_baseline_unattended_upgrade_origins | length > 0 + +- name: Remove the origins pin when none is set + become: true + ansible.builtin.file: + path: /etc/apt/apt.conf.d/90unattended-upgrades-origins + state: absent + when: system_baseline_unattended_upgrade_origins | length == 0 + - name: Validate unattended-upgrades config become: true ansible.builtin.command: unattended-upgrades --dry-run --debug diff --git a/roles/system_baseline/tasks/validate.yml b/roles/system_baseline/tasks/validate.yml index daaf510..576e125 100644 --- a/roles/system_baseline/tasks/validate.yml +++ b/roles/system_baseline/tasks/validate.yml @@ -34,3 +34,17 @@ quiet: true when: system_baseline_tmux_autostart | bool tags: always + +# Each origin is written inside a double-quoted apt.conf string: a quote or a line break would end it, a backslash could escape. +- name: Validate the unattended-upgrades origins + ansible.builtin.assert: + that: + - system_baseline_unattended_upgrade_origins is sequence + - system_baseline_unattended_upgrade_origins is not string + - system_baseline_unattended_upgrade_origins | select('string') | select('match', '^[^"\\\\\n\r]+\Z') | list | length + == system_baseline_unattended_upgrade_origins | length + fail_msg: >- + system_baseline_unattended_upgrade_origins must be a list of non-empty one-line strings without a double quote + or a backslash. + quiet: true + tags: always diff --git a/roles/system_baseline/templates/unattended-upgrades-origins.j2 b/roles/system_baseline/templates/unattended-upgrades-origins.j2 new file mode 100644 index 0000000..00d55af --- /dev/null +++ b/roles/system_baseline/templates/unattended-upgrades-origins.j2 @@ -0,0 +1,11 @@ +// Managed by Ansible (mortis.kamino.system_baseline, tasks/unattended_upgrades.yml). Do not edit: the next run overwrites it. +// apt appends list entries across apt.conf.d files, so listing origins here would only widen those of 50unattended-upgrades. +// This file sorts after it, clears both origin lists, and sets the only origins unattended-upgrades may install from. +// A file that sorts after this one can still add origins. +#clear Unattended-Upgrade::Allowed-Origins; +#clear Unattended-Upgrade::Origins-Pattern; +Unattended-Upgrade::Origins-Pattern { +{% for origin in system_baseline_unattended_upgrade_origins %} + "{{ origin }}"; +{% endfor %} +};