From 7092d68822ac94b76c3ac1730b8ab06a11598a1e Mon Sep 17 00:00:00 2001 From: Dragonish Date: Mon, 23 Nov 2026 21:01:40 +0000 Subject: [PATCH] feat(system_baseline): reboot one host at a time and bound a hung apt-daily.service Add system_baseline_reboot_throttle and system_baseline_reboot_post_delay, passed to the throttle and post_reboot_delay of the reboot task, read by the main flow and by tasks_from: reboot, both defaulting to 0 (today's behaviour) and validated in both. Add system_baseline_apt_daily_timeout: when set, write a TimeoutStartSec drop-in for apt-daily.service and reload systemd. Empty by default, which leaves the host alone. Closes #336 Closes #337 Assisted-by: Claude Code --- ...tem-baseline-reboot-throttle-apt-daily.yml | 11 ++++++ roles/system_baseline/README.md | 27 ++++++++++++- roles/system_baseline/defaults/main.yml | 7 ++++ roles/system_baseline/handlers/main.yml | 5 +++ roles/system_baseline/meta/argument_specs.yml | 35 ++++++++++++++++- .../molecule/default/converge.yml | 38 +++++++++++++++++++ .../molecule/default/tasks/refuse_reboot.yml | 11 ++++++ .../default/tasks/refuse_reboot_one.yml | 19 ++++++++++ .../molecule/default/verify.yml | 24 ++++++++++++ .../tasks/apt_daily_timeout.yml | 24 ++++++++++++ roles/system_baseline/tasks/main.yml | 5 +++ roles/system_baseline/tasks/reboot.yml | 6 +++ roles/system_baseline/tasks/validate.yml | 17 +++++++++ .../system_baseline/tasks/validate_reboot.yml | 13 +++++++ 14 files changed, 239 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/system-baseline-reboot-throttle-apt-daily.yml create mode 100644 roles/system_baseline/molecule/default/tasks/refuse_reboot.yml create mode 100644 roles/system_baseline/molecule/default/tasks/refuse_reboot_one.yml create mode 100644 roles/system_baseline/tasks/apt_daily_timeout.yml create mode 100644 roles/system_baseline/tasks/validate_reboot.yml diff --git a/changelogs/fragments/system-baseline-reboot-throttle-apt-daily.yml b/changelogs/fragments/system-baseline-reboot-throttle-apt-daily.yml new file mode 100644 index 0000000..34a0bb9 --- /dev/null +++ b/changelogs/fragments/system-baseline-reboot-throttle-apt-daily.yml @@ -0,0 +1,11 @@ +--- +minor_changes: + - >- + system_baseline - Add the inputs ``system_baseline_reboot_throttle`` and ``system_baseline_reboot_post_delay`` (whole numbers, default + ``0``, which keeps today's behaviour). They are passed to the ``throttle`` and ``post_reboot_delay`` of the reboot task, so a caller can + reboot one host at a time and wait before the next. The main flow and the ``reboot`` entry point read them and refuse a negative or + non-integer value before anything runs. + - >- + system_baseline - Add the input ``system_baseline_apt_daily_timeout``. When set (a systemd time span, for example ``1h``; the default is + empty, which leaves the host alone) the role writes a ``TimeoutStartSec`` drop-in for ``apt-daily.service`` and reloads systemd, so a hung + apt run is stopped. Tag ``apt-daily-timeout``. diff --git a/roles/system_baseline/README.md b/roles/system_baseline/README.md index 2f8d46a..38a8321 100644 --- a/roles/system_baseline/README.md +++ b/roles/system_baseline/README.md @@ -45,6 +45,9 @@ 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_reboot_throttle` | `0` | no | Hosts that reboot at the same time; `0` is no limit, `1` one at a time (see "Rebooting one host at a time") | +| `system_baseline_reboot_post_delay` | `0` | no | Seconds to wait after a rebooted host answers again | +| `system_baseline_apt_daily_timeout` | empty | no | Systemd time span written as `TimeoutStartSec` of `apt-daily.service`; empty leaves the host alone (see "Bounding a hung apt-daily.service") | | `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: @@ -61,7 +64,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. It does not read `system_baseline_update`: calling the entry point is the request to update. In check mode it previews the upgrade; the main flow does not (see "Check mode"). -- `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)`. +- `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. It reads `system_baseline_reboot_throttle` and `system_baseline_reboot_post_delay` as the main flow does, and refuses a bad value before anything runs. On other families the fact is not set, so read it with `default(false)`. ```yaml - name: Upgrade packages @@ -85,7 +88,7 @@ Besides `main`, two task files are a supported interface, for a caller that driv ## Tags -`always` (validation), `update` (apt upgrade and the reboot), `packages`, `unattended-upgrades`, `locale`, `timezone`, `journald`, `sysctl`, `secure_tmp`, `tmux`. +`always` (validation), `update` (apt upgrade and the reboot), `packages`, `unattended-upgrades`, `locale`, `timezone`, `journald`, `apt-daily-timeout`, `sysctl`, `secure_tmp`, `tmux`. ## Example play @@ -117,6 +120,26 @@ 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. +## Rebooting one host at a time + +`system_baseline_reboot_throttle` is the `throttle` of the reboot task: how many hosts of the play reboot at once. The default `0` is no limit, which is what the role did before. `1` makes the hosts reboot one after the other, for a group whose members must not be down together (a cluster, a replicated pair). `system_baseline_reboot_post_delay` is the `post_reboot_delay` of the same task: the seconds to wait after a host answers again, so a service on it can settle before the next host goes down. Both are whole numbers, 0 or more; anything else stops the run before a host reboots, in the main flow and in `tasks_from: reboot`. + +```yaml +system_baseline_reboot_throttle: 1 +system_baseline_reboot_post_delay: 45 +``` + +The throttle applies to the reboot task only, and to hosts that need a reboot: it does not serialise the rest of the role. A play-level `serial` still works as before. + +## Bounding a hung apt-daily.service + +With `system_baseline_apt_daily_timeout: 1h` the role writes `/etc/systemd/system/apt-daily.service.d/10-timeout.conf` (`[Service]`, `TimeoutStartSec=1h`) and runs `systemctl daemon-reload` when it changed. A hung `apt-daily.service` (a stuck mirror, a lock that never frees) is then stopped after that time instead of blocking every later apt run. Only `apt-daily.service` is changed, not `apt-daily-upgrade.service`. + +- Empty (the default) leaves the host alone. Clearing the value later does not remove a file already written; delete it by hand and reload systemd. +- The value must be empty or a one-line systemd time span starting with a digit, or `infinity`. A value systemd cannot parse is ignored by systemd with a log line, so check `systemctl show -p TimeoutStartUSec apt-daily.service`. +- `0` is accepted but means no timeout to systemd (the same as `infinity`), the opposite of a bound; set a real time span. +- Run only this step with `--tags apt-daily-timeout`. + ## 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. diff --git a/roles/system_baseline/defaults/main.yml b/roles/system_baseline/defaults/main.yml index 1da23e8..7af64ed 100644 --- a/roles/system_baseline/defaults/main.yml +++ b/roles/system_baseline/defaults/main.yml @@ -4,6 +4,13 @@ system_baseline_timezone: "Etc/UTC" system_baseline_locale: "en_US.UTF-8" system_baseline_reboot_if_required: true +# How many hosts of a play reboot at the same time (the `throttle` of the reboot task); 0 is no limit, as before. Read by the main flow +# and by the tasks_from: reboot entry point. +system_baseline_reboot_throttle: 0 +# Seconds the reboot task waits after a host answers again, before the next host reboots or the play goes on; 0 adds no wait, as before. +system_baseline_reboot_post_delay: 0 +# Systemd time span (for example 1h) written as TimeoutStartSec of apt-daily.service; empty leaves the host alone (tasks/apt_daily_timeout.yml). +system_baseline_apt_daily_timeout: "" # 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 diff --git a/roles/system_baseline/handlers/main.yml b/roles/system_baseline/handlers/main.yml index 80fafbb..5e9cd04 100644 --- a/roles/system_baseline/handlers/main.yml +++ b/roles/system_baseline/handlers/main.yml @@ -14,3 +14,8 @@ ansible.builtin.systemd: name: systemd-journald state: restarted + +- name: Reload systemd for apt-daily timeout + become: true + ansible.builtin.systemd: + daemon_reload: true diff --git a/roles/system_baseline/meta/argument_specs.yml b/roles/system_baseline/meta/argument_specs.yml index 8294816..bf282c6 100644 --- a/roles/system_baseline/meta/argument_specs.yml +++ b/roles/system_baseline/meta/argument_specs.yml @@ -6,7 +6,7 @@ argument_specs: - >- 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 (unless C(system_baseline_update) is false), packages, unattended-upgrades, locale, timezone, - journald, sysctl, tmpfs, tmux and reboot task files, in that order. + journald, apt-daily timeout (when set), 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), @@ -106,6 +106,29 @@ argument_specs: default: true description: - Reboot when C(/var/run/reboot-required) exists after the run. + system_baseline_reboot_throttle: + type: int + default: 0 + description: + - >- + How many hosts of a play reboot at the same time: the C(throttle) of the reboot task. 0 is no limit, as before; 1 reboots one host + at a time, for a group whose members must not be down together. A negative or non-integer value stops the run. + system_baseline_reboot_post_delay: + type: int + default: 0 + description: + - >- + Seconds to wait after a rebooted host answers again, before the next host reboots (with a throttle) or the play goes on, so a + service on it can settle. 0 adds no wait, as before. A negative or non-integer value stops the run. + system_baseline_apt_daily_timeout: + type: str + default: "" + description: + - >- + Systemd time span (for example C(1h)) written as C(TimeoutStartSec) of C(apt-daily.service) in + C(/etc/systemd/system/apt-daily.service.d/10-timeout.conf), so a hung apt run is stopped. Empty leaves the host alone; clearing + the value later does not remove a file already written. A value of C(0) means no timeout to systemd. + C(apt-daily-upgrade.service) is not changed. Tag C(apt-daily-timeout). system_baseline_unattended_upgrade_package_blacklist: type: list elements: str @@ -157,3 +180,13 @@ argument_specs: default: true description: - Reboot when a reboot is required. + system_baseline_reboot_throttle: + type: int + default: 0 + description: + - Hosts that reboot at the same time (the C(throttle) of the reboot task); 0 is no limit. Checked before anything runs. + system_baseline_reboot_post_delay: + type: int + default: 0 + description: + - Seconds to wait after a rebooted host answers again; 0 adds no wait. Checked before anything runs. diff --git a/roles/system_baseline/molecule/default/converge.yml b/roles/system_baseline/molecule/default/converge.yml index f0d1bd5..ec560d5 100644 --- a/roles/system_baseline/molecule/default/converge.yml +++ b/roles/system_baseline/molecule/default/converge.yml @@ -64,6 +64,43 @@ that: "'system_baseline_apt_upgrade is' in ansible_failed_result.msg | default('')" quiet: true +# The reboot inputs are whole numbers, 0 or more: a negative number and a boolean each stop the run before a host reboots, +# in the main flow and in the reboot entry point (which does not run validate.yml). +- name: A bad reboot throttle or post-reboot delay is refused + hosts: all + gather_facts: false + tasks: + - name: Refuse each bad value + ansible.builtin.include_tasks: tasks/refuse_reboot.yml + vars: + system_baseline_refused_reboot: "{{ item }}" + loop: + - {system_baseline_reboot_throttle: -1} + - {system_baseline_reboot_post_delay: -5} + - {system_baseline_reboot_post_delay: true} + +# The value is written into a unit file, so a line break (a second directive) is refused. +- name: A bad apt-daily timeout is refused + hosts: all + gather_facts: false + tasks: + - name: Run the role with a line break in system_baseline_apt_daily_timeout + block: + - name: Include system_baseline + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + vars: + system_baseline_system_type: ubuntu_lxc + system_baseline_apt_daily_timeout: "1h\nExecStart=/bin/true" + - name: Unreachable when validation works + ansible.builtin.fail: + msg: system_baseline ran with an invalid system_baseline_apt_daily_timeout + rescue: + - name: The validation stopped the play + ansible.builtin.assert: + that: "'system_baseline_apt_daily_timeout must be' in ansible_failed_result.msg | default('')" + quiet: true + # A quote, a null item and a bare string (not a list) each stop the run: the bare string would otherwise be split into one-character # patterns, and the null item would become an empty pattern that matches every package. - name: A bad package blacklist is refused @@ -85,6 +122,7 @@ vars: system_baseline_system_type: ubuntu_lxc system_baseline_tmux_autostart: true + system_baseline_apt_daily_timeout: 1h system_baseline_tmux_session: molecule system_baseline_tmux_windows: - { name: logs, command: "echo 'a b'; true" } diff --git a/roles/system_baseline/molecule/default/tasks/refuse_reboot.yml b/roles/system_baseline/molecule/default/tasks/refuse_reboot.yml new file mode 100644 index 0000000..07a8f92 --- /dev/null +++ b/roles/system_baseline/molecule/default/tasks/refuse_reboot.yml @@ -0,0 +1,11 @@ +--- +# One refusal per input: the main flow and the reboot entry point must both stop on validation, with a message that names the variable. +- name: Run the main flow and the entry point with a bad reboot input + ansible.builtin.include_tasks: refuse_reboot_one.yml + vars: + system_baseline_refused_entry: "{{ entry }}" + loop: + - main + - reboot + loop_control: + loop_var: entry diff --git a/roles/system_baseline/molecule/default/tasks/refuse_reboot_one.yml b/roles/system_baseline/molecule/default/tasks/refuse_reboot_one.yml new file mode 100644 index 0000000..0b202d5 --- /dev/null +++ b/roles/system_baseline/molecule/default/tasks/refuse_reboot_one.yml @@ -0,0 +1,19 @@ +--- +- name: Run the role with a bad reboot input + block: + - name: Include system_baseline + ansible.builtin.include_role: + name: mortis.kamino.system_baseline + tasks_from: "{{ system_baseline_refused_entry }}" + vars: + system_baseline_system_type: ubuntu_lxc + system_baseline_reboot_throttle: "{{ system_baseline_refused_reboot.system_baseline_reboot_throttle | default(0) }}" + system_baseline_reboot_post_delay: "{{ system_baseline_refused_reboot.system_baseline_reboot_post_delay | default(0) }}" + - name: Unreachable when validation works + ansible.builtin.fail: + msg: system_baseline ran with an invalid reboot input + rescue: + - name: The validation stopped the play + ansible.builtin.assert: + that: "'system_baseline_reboot_throttle (' in ansible_failed_result.msg | default('')" + quiet: true diff --git a/roles/system_baseline/molecule/default/verify.yml b/roles/system_baseline/molecule/default/verify.yml index 1b7a34c..c0d789a 100644 --- a/roles/system_baseline/molecule/default/verify.yml +++ b/roles/system_baseline/molecule/default/verify.yml @@ -77,6 +77,30 @@ that: system_baseline_verify_locales.stdout_lines | map('lower') | map('replace', '-', '') | select('match', 'en_us.utf8') | list | length > 0 quiet: true +# The converge sets system_baseline_apt_daily_timeout: 1h. systemd must have read the drop-in after the reload, not only the file exist. +- name: The apt-daily start timeout is written and loaded + hosts: all + gather_facts: false + tasks: + - name: Read the drop-in + ansible.builtin.slurp: + src: /etc/systemd/system/apt-daily.service.d/10-timeout.conf + register: system_baseline_verify_apt_daily + + - name: Ask systemd for the unit + # The systemd module cannot return TimeoutStartUSec, which is the point of this check. + ansible.builtin.command: systemctl show -p LoadState -p TimeoutStartUSec apt-daily.service # noqa: command-instead-of-module + register: system_baseline_verify_apt_daily_show + changed_when: false + + - name: The drop-in is there and systemd applies it + ansible.builtin.assert: + that: + - system_baseline_verify_apt_daily.content | b64decode is search('(?m)^TimeoutStartSec=1h$') + - system_baseline_verify_apt_daily_show.stdout_lines is contains('LoadState=loaded') + - system_baseline_verify_apt_daily_show.stdout_lines is contains('TimeoutStartUSec=1h') + quiet: true + # The reboot entry point is a supported interface: with system_baseline_reboot_if_required # false it only sets system_baseline_reboot_required. Here and not in converge, because it # creates and removes the marker file, which the idempotence run would report as a change. diff --git a/roles/system_baseline/tasks/apt_daily_timeout.yml b/roles/system_baseline/tasks/apt_daily_timeout.yml new file mode 100644 index 0000000..82b56ca --- /dev/null +++ b/roles/system_baseline/tasks/apt_daily_timeout.yml @@ -0,0 +1,24 @@ +--- +# A hung apt-daily.service (a stuck mirror, a dpkg lock that never frees) blocks the next apt run on the host for as long as systemd lets +# it start. A drop-in bounds the start. Only apt-daily.service: apt-daily-upgrade.service is left to its own timeout. +- name: Ensure the apt-daily drop-in directory exists + become: true + ansible.builtin.file: + path: /etc/systemd/system/apt-daily.service.d + state: directory + owner: root + group: root + mode: "0755" + +- name: Bound the start of apt-daily.service + become: true + ansible.builtin.copy: + dest: /etc/systemd/system/apt-daily.service.d/10-timeout.conf + content: | + # Managed by Ansible (role system_baseline). + [Service] + TimeoutStartSec={{ system_baseline_apt_daily_timeout }} + owner: root + group: root + mode: "0644" + notify: Reload systemd for apt-daily timeout diff --git a/roles/system_baseline/tasks/main.yml b/roles/system_baseline/tasks/main.yml index c5006f0..46bb026 100644 --- a/roles/system_baseline/tasks/main.yml +++ b/roles/system_baseline/tasks/main.yml @@ -32,6 +32,11 @@ ansible.builtin.import_tasks: journald.yml tags: journald +- name: Bound a hung apt-daily.service + ansible.builtin.import_tasks: apt_daily_timeout.yml + when: system_baseline_apt_daily_timeout | length > 0 + tags: apt-daily-timeout + - name: Harden sysctl ansible.builtin.import_tasks: sysctl.yml tags: sysctl diff --git a/roles/system_baseline/tasks/reboot.yml b/roles/system_baseline/tasks/reboot.yml index c37effb..900b109 100644 --- a/roles/system_baseline/tasks/reboot.yml +++ b/roles/system_baseline/tasks/reboot.yml @@ -1,4 +1,8 @@ --- +# The tasks_from: reboot entry point does not run validate.yml, so the reboot inputs are checked here as well. +- name: Validate the reboot inputs + ansible.builtin.import_tasks: validate_reboot.yml + - name: Check if reboot required (Debian) when: ansible_os_family == "Debian" block: @@ -14,6 +18,8 @@ - name: Reboot the server if required ansible.builtin.reboot: reboot_timeout: 900 + post_reboot_delay: "{{ system_baseline_reboot_post_delay | int }}" + throttle: "{{ system_baseline_reboot_throttle | int }}" become: true when: - system_baseline_reboot_if_required diff --git a/roles/system_baseline/tasks/validate.yml b/roles/system_baseline/tasks/validate.yml index 4dc442e..6c5b8e4 100644 --- a/roles/system_baseline/tasks/validate.yml +++ b/roles/system_baseline/tasks/validate.yml @@ -39,6 +39,23 @@ ansible.builtin.import_tasks: validate_apt_upgrade.yml tags: always +- name: Validate the reboot inputs + ansible.builtin.import_tasks: validate_reboot.yml + tags: always + +# The value is written into a unit file: one line, starting with a digit, so no line break can add a second directive. systemd itself +# refuses a value it cannot parse (it ignores the setting and logs it). +- name: Validate the apt-daily start timeout + ansible.builtin.assert: + that: + - system_baseline_apt_daily_timeout is string + - system_baseline_apt_daily_timeout == '' or system_baseline_apt_daily_timeout is match('^(infinity|[0-9][0-9A-Za-z. ]{0,63})\Z') + fail_msg: >- + system_baseline_apt_daily_timeout must be empty or a systemd time span on one line, for example 1h or 90min + (quote a bare number: a plain 60 is not a string). + quiet: true + tags: always + # Each package pattern is written inside a double-quoted apt.conf string, like the origins below: the same characters are refused. - name: Validate the unattended-upgrades package blacklist ansible.builtin.assert: diff --git a/roles/system_baseline/tasks/validate_reboot.yml b/roles/system_baseline/tasks/validate_reboot.yml new file mode 100644 index 0000000..d1a8267 --- /dev/null +++ b/roles/system_baseline/tasks/validate_reboot.yml @@ -0,0 +1,13 @@ +--- +# Shared by validate.yml and reboot.yml: the tasks_from: reboot entry point does not run validate.yml. A negative or non-integer value +# must stop the run before a host reboots, not be coerced into a different wait. `| string` also refuses true, 2.5 and a list. +- name: Validate the reboot throttle and post-reboot delay + ansible.builtin.assert: + that: + - system_baseline_reboot_throttle | string is match('^[0-9]+\Z') + - system_baseline_reboot_post_delay | string is match('^[0-9]+\Z') + fail_msg: >- + system_baseline_reboot_throttle ('{{ system_baseline_reboot_throttle }}') and system_baseline_reboot_post_delay + ('{{ system_baseline_reboot_post_delay }}') must each be a whole number, 0 or more. + quiet: true + tags: always -- 2.43.0