Skip to content

Ansible Role linuxfabrik.lfops.system_update

This role configures the server to do system updates. A daily regular lane applies all available updates, and on Rocky Linux a security lane applies only security hot-fixes from the dedicated security repository four times a day. Updates that require a reboot are followed by one at a configurable maintenance window per host. The administrators are informed (via email or Rocket.Chat) about the applied updates, reboots and failures. For weekly updates, an optional notify-and-schedule script announces the pending updates the day before.

Reboots are not performed by the update scripts themselves. They are delegated to the schedule_reboot role: an update that needs a reboot drops a request into that role's spool, and schedule_reboot reboots the host once, at the window.

Available since LFOps 2.0.0.

How the Role Behaves

  • Updates run at the reboot window. The regular lane runs daily at the maintenance window defined by schedule_reboot__reboot_time__* (the schedule_reboot role). When an update needs a reboot it drops a request into that role's spool; the update unit is ordered before the reboot actor, so the reboot waits for the update to finish before it runs.
  • The Rocky security lane runs four times a day. Every 6 hours, starting at the maintenance window, for example at 04:12, 10:12, 16:12 and 22:12. A hot-fix installed outside the window that needs a reboot is followed by the reboot at the next window, not right away.
  • Pending updates are not announced by default. With daily updates there is no time left for a heads-up. For weekly updates, set system_update__update_day to a weekday and system_update__notify_and_schedule_enabled: true; the announcement then arrives the day before.
  • The regular lane only reports when it actually changed something. On an update day where nothing was pending, it still checks whether a reboot is outstanding, but sends no "System updated without Reboot" mail. The mail it does send lists the packages of that run, not of an earlier one.
  • The security lane installs the hot-fixes together with what they need. It installs exactly the versions the security repository has updates for, and resolves them with all enabled repositories. A hot-fix that requires a newer package from BaseOS or AppStream, such as a kernel that needs a newer linux-firmware on an older Rocky 8 release, pulls in that package too. Other pending updates are left to the regular lane.
  • "Security" means the security repository, not security errata. The security lane covers the hot-fixes of the security repository only. Security errata (RLSA) from BaseOS and AppStream are installed by the regular lane. With a system_update__update_day other than daily, they stay pending until the next regular run, and the rpm-updates check of the Linuxfabrik Monitoring Plugins reports them as security updates in the meantime.
  • The security lane is enabled by default, but a no-op without the security repository. That repository is provided by the repo_baseos role. On hosts where it is not present, the security lane installs nothing and requests no reboot. Turn the lane off entirely with system_update__security_enabled: false.
  • The update hooks run in both lanes, but only around a real transaction. system_update__pre_update_code and system_update__post_update_code are run by the regular lane and, by default, by the security lane too. The security lane fires four times a day and exits on most runs without installing anything, so it reaches the hooks only once the security repository actually has hot-fixes pending. Code that aborts the update or reports a failed pre-condition therefore stays quiet on the runs there is nothing to install. Where a hot-fix does not warrant what a full update does, system_update__security_pre_update_code and system_update__security_post_update_code override the security lane's half, '' included.
  • Config-file stubs are not mailed by default. The *.rpmnew, *.rpmsave, *.dpkg-dist and *.ucf-dist files an update leaves next to a locally changed configuration file are reported only with system_update__mail_new_configfiles_enabled: true, one mail per file and run.
  • The reboot notification says why. The notification sent when the host reboots lists what asked for the reboot: the needs-restarting hint on Rocky, and on Debian and Ubuntu the packages from /run/reboot-required.pkgs and the needrestart summary. This also holds for a reboot carried over from an earlier window.
  • Unused kernels are removed before the update on Debian and Ubuntu. apt-get upgrade installs every new kernel but never removes one, so the regular lane first removes the kernel packages apt lists as auto-removable, the same way unattended-upgrades does with Remove-Unused-Kernel-Packages. apt keeps the running and the newest kernel; held packages, the packages of the running kernel and anything other than versioned kernel packages are never removed. If the removal would remove or install anything beyond that, it is skipped. The update mail lists the removed packages. On Rocky, dnf's installonly_limit does the same within the transaction.
  • Packages the update leaves unused are removed on Debian and Ubuntu. After the upgrade, the regular lane removes the packages that became auto-removable in this run, typically libraries replaced by a renamed package, like unattended-upgrades does with Remove-New-Unused-Dependencies, and with the same checks as the kernel removal. Packages that were unused before the run stay installed; remove them with apt autoremove if they are not needed.
  • An interrupted dpkg is repaired on Debian and Ubuntu. If an earlier dpkg run was cut short, for example by a crash or a reboot from outside the window, the regular lane runs dpkg --configure -a before the update, keeping the existing configuration files, instead of failing on every run until someone repairs the host by hand.
  • The regular lane waits for the package manager. If another process holds the apt or dpkg lock, for example an Ansible run or an admin in apt, the regular lane and its update check (notify-and-schedule) wait up to 10 minutes for it on Debian and Ubuntu instead of failing. If the update check still cannot refresh the package lists, it sends a "System update check failed" mail instead of announcing updates from stale lists.
  • A failed update stops the run. A metadata refresh, a removal of packages or an upgrade that exits non-zero sends a "System update failed" mail with the error output and ends the run, on both families. Nothing downstream happens: no AIDE re-baseline, no reboot request, and no success mail for a host that is left half-configured.
  • AIDE is re-baselined only when a check right before the update was clean. If the host runs the AIDE check of the aide role (aidecheck.timer is active), the update runs an AIDE check before it installs anything, and if the update changed packages and that check was clean, it refreshes the AIDE database afterwards, so the next check does not flag every file the update touched. This applies to the regular and the security lane alike. If the check reports changes, the database is left untouched and the report is kept at /var/log/aide/aide.log-pre-system-update (regular lane) or /var/log/aide/aide.log-pre-security-update (security lane): drift that predates the update, an intrusion included, is never accepted as the new baseline. If the check cannot run at all, for example without an AIDE database, the database is left untouched as well, and a separate "AIDE check failed" mail quotes the error.

Dependent Roles

Any LFOps playbook that installs this role runs these for you. Optional ones can be disabled via the playbook's skip variables.

Tags

system_update

  • Deploys the notify-and-schedule and update-and-reboot scripts and their systemd timers/services. On Rocky Linux hosts it also deploys the security-update lane.
  • Triggers: none.

system_update:state

  • Determines whether the notify-and-schedule, update-and-reboot and (Rocky) security-update timers are enabled.
  • Triggers: none.

Optional Role Variables

system_update__cache_only

  • Whether to install updates from cache only. This implies to have the cache built beforehand.
  • Type: Bool.
  • Default: false

system_update__mail_from

  • The email sender account. This will be used as the "from"-address for all notifications.
  • Type: String.
  • Default: '{{ mailto_root__from }}'

system_update__mail_new_configfiles_enabled

  • Whether the regular lane sends a mail for every config-file stub (*.rpmnew / *.rpmsave / *.dpkg-dist / *.ucf-dist) it finds on the local filesystems after the update, to system_update__mail_recipients_new_configfiles. The lane looks for them on every run, so a stub that is left in place is mailed again each time.
  • Type: Bool.
  • Default: false

system_update__mail_recipients_new_configfiles

  • A list of email recipients to notify if there is a new version of a config file (rpmnew / rpmsave / dpkg-dist / ucf-dist), if enabled by system_update__mail_new_configfiles_enabled.
  • Type: List of strings.
  • Default: '{{ mailto_root__to }}'

system_update__mail_recipients_updates

  • A list of email recipients to notify about the expected updates (if enabled), the installed updates and failures.
  • Type: List of strings.
  • Default: '{{ mailto_root__to }}'

system_update__mail_subject_hostname

  • String which will be used as the hostname in the mail subject. You can use $() to call bash code.
  • Type: String.
  • Default: '$(hostname --short)'

system_update__mail_subject_prefix

  • This will set a prefix that will be showed in front of the hostname. Can be used to separate servers by environment or customer.
  • Type: String.
  • Default: ''

system_update__notify_and_schedule_enabled

  • Enables or disables the informational "updates pending" notification (the notify-and-schedule timer), analogous to systemctl enable/disable --now. Meant for weekly updates, see system_update__update_day. Only takes effect if system_update__update_enabled is true.
  • Type: Bool.
  • Default: false

system_update__notify_and_schedule_on_calendar

  • When the informational "updates pending" notification is sent, if enabled by system_update__notify_and_schedule_enabled. This is purely a heads-up; the update is applied later at the maintenance window. By default it is sent at 10:00 on the day before a weekday set in system_update__update_day, so it arrives before the update runs. For any other system_update__update_day, such as the daily default, it falls back to Monday 10:00, so set this explicitly. Have a look at systemd.time(7) for the format.
  • Type: String.
  • Default: 10:00 on the day before system_update__update_day (for example 'Mon 10:00' when the update day is Tue), otherwise 'Mon 10:00'

system_update__post_update_code

  • This codeblock will be executed after the updates have been installed and before a potential reboot. The security lane runs it as well, unless system_update__security_post_update_code says otherwise.
  • Type: String.
  • Default: unset

system_update__pre_update_code

  • This codeblock will be executed before the update process is started. Can be used to check pre-conditions for updating, for example for checking cluster nodes. The security lane runs it as well, unless system_update__security_pre_update_code says otherwise.
  • Type: String.
  • Default: unset

system_update__rocketchat_msg_suffix

  • A suffix to the Rocket.Chat notifications. This can be used to mention other users.
  • Type: String.
  • Default: ''

system_update__rocketchat_url

  • The URL to a potential Rocket.Chat server to send notifications about the updates to.
  • Type: String.
  • Default: unset

system_update__security_enabled

  • Enables or disables the security lane (the security-update timer), analogous to systemctl enable/disable --now. Rocky Linux only. When enabled but the security repository is not enabled on the host, the lane is a no-op.
  • Type: Bool.
  • Default: true

system_update__security_on_calendar

  • When the security lane checks for and installs security hot-fixes. Defaults to four times a day, every 6 hours starting at the reboot window (schedule_reboot__reboot_time__*), so the first run of the day is followed by the reboot right after. Have a look at systemd.time(7) for the format.
  • Type: String.
  • Default: every 6 hours starting at the reboot window, for example '*-*-* 04,10,16,22:12' for a window at 04:12

system_update__security_post_update_code

  • The codeblock the security lane executes after the hot-fixes have been installed and before a potential reboot. Set it to '' to keep the security lane free of code the regular lane runs, or to a codeblock of its own to have the two lanes do different things.
  • Type: String.
  • Default: '{{ system_update__post_update_code | d("") }}'

system_update__security_pre_update_code

  • The codeblock the security lane executes before it installs the hot-fixes. Set it to '' to keep the security lane free of code the regular lane runs, or to a codeblock of its own to have the two lanes do different things.
  • Type: String.
  • Default: '{{ system_update__pre_update_code | d("") }}'

system_update__security_repos

  • The repositories the security lane installs from. All other repositories are disabled for the security transaction, keeping it separate from the regular update lane.
  • Type: List.
  • Default: ['security']

system_update__update_day

  • The day(s) on which the regular update lane runs. Combined with the maintenance window to build the timer schedule, for example *-*-* 04:00 (daily) or Tue 04:00 (weekly). For weekly updates with an announcement the day before, set a weekday here and enable system_update__notify_and_schedule_enabled. Have a look at systemd.time(7) for the format.
  • Type: String.
  • Default: '*-*-*'

system_update__update_enabled

  • Enables or disables the regular update lane (the notify-and-schedule and update-and-reboot timers), analogous to systemctl enable/disable --now.
  • Type: Bool.
  • Default: true

system_update__update_on_calendar

  • When the regular update lane installs the available updates. Defaults to system_update__update_day at the maintenance window (schedule_reboot__reboot_time__*), so the reboot follows right after. Set this to schedule the update lane independently of system_update__update_day, for example on the first Tuesday of the month. Have a look at systemd.time(7) for the format.
  • Type: String.
  • Default: '{{ system_update__update_day }} {{ schedule_reboot__reboot_time__combined_var | d("04:00") }}'

Example:

# optional
system_update__cache_only: true
system_update__mail_from: 'noreply@example.com'
system_update__mail_new_configfiles_enabled: true
system_update__mail_recipients_new_configfiles:
  - 'info@example.com'
  - 'support@example.com'
system_update__mail_recipients_updates:
  - 'info@example.com'
  - 'support@example.com'
system_update__mail_subject_hostname: '$(hostname --long)'
system_update__mail_subject_prefix: '001-'
system_update__notify_and_schedule_enabled: true
system_update__notify_and_schedule_on_calendar: 'mon *-*-01..07 10:00' # first monday of the month
system_update__post_update_code: |-
  VAR='hello world'
  echo $VAR
system_update__pre_update_code: |-
  check_dns() {
    local DNS_SERVER=$1
    if ! dig @$DNS_SERVER linuxfabrik.ch +short > /dev/null; then
        send_msg "$SUBJECT_PREFIX - System update failed" "DNS Server $DNS_SERVER failed to respond. Aborting update."
        exit 1
    fi
  }
  check_dns 192.0.2.10
  check_dns 192.0.2.11
system_update__rocketchat_msg_suffix: '@administrator'
system_update__rocketchat_url: 'https://chat.example.com/hooks/abcd1234'
system_update__security_enabled: true
system_update__security_on_calendar: '*-*-* 04,10,16,22:00'
system_update__security_post_update_code: '' # a hot-fix does not warrant what the regular lane does here
system_update__security_pre_update_code: |- # replaces system_update__pre_update_code, it does not add to it
  systemctl is-active --quiet pacemaker || exit 1
system_update__security_repos:
  - 'security'
system_update__update_day: 'Tue'
system_update__update_enabled: true
system_update__update_on_calendar: 'Tue *-*-01..07 04:00' # first tuesday of the month

License

The Unlicense

Author Information

Linuxfabrik GmbH, Zurich