An apt upgrade of a Proxmox node can restart Proxmox services while a backup holds a snapshot of a guest, and the role had no way to say "not now". system_baseline_skip_upgrade_during_vzdump (bool, default false) makes the main flow and the update entry point ask the node for its running tasks (pvesh get /nodes/localhost/tasks --source active) before the upgrade, on the pve system type only. A vzdump task without an end time skips the upgrade with a message; autoremove, autoclean and the reboot check still run. The role does not wait for the backup. A pvesh answer that is not a JSON list stops the run. With the default no pvesh command runs. Closes #400 Assisted-by: Claude Code:claude-sonnet-5-5
104 lines
4.8 KiB
YAML
104 lines
4.8 KiB
YAML
---
|
|
# In the main flow a dry run (--check) does not run the upgrade: a package that is only waiting for the next patch run is not drift, and
|
|
# the package module reports `changed` for it, so a scheduled dry run flagged every host whenever a package landed in between. The
|
|
# tasks_from: update entry point is an explicit request to update and still previews the upgrade in check mode: only main.yml sets
|
|
# __system_baseline_main_flow. Leftover packages (autoremove) and the cache (autoclean) stay checked either way.
|
|
- name: Validate the apt upgrade mode
|
|
ansible.builtin.import_tasks: validate_apt_upgrade.yml
|
|
|
|
- name: Validate the apt scratch directory
|
|
ansible.builtin.import_tasks: validate_apt_tmpdir.yml
|
|
|
|
# apt and the package module put their temporary files, including the staged .deb files, in TMPDIR. A restart of ssh.service during the upgrade
|
|
# (an openssh-server update) deletes a PrivateTmp /tmp, so TMPDIR points at a directory systemd leaves alone. Created in check mode too,
|
|
# because the autoremove below runs there with the same TMPDIR.
|
|
- name: Create the apt scratch directory
|
|
ansible.builtin.file:
|
|
path: "{{ system_baseline_apt_tmpdir }}"
|
|
state: directory
|
|
owner: root
|
|
group: root
|
|
mode: "0700"
|
|
become: true
|
|
|
|
# Opt-in guard (system_baseline_skip_upgrade_during_vzdump, pve only): an upgrade can restart Proxmox services while a backup holds a snapshot
|
|
# of a guest, so the upgrade waits for the next run when this node has a vzdump task without an end time. Both tasks read state only, so
|
|
# they run in check mode too; they are skipped where the upgrade is (a dry run of the main flow). `localhost` is the node's own alias, so no
|
|
# node name and no gathered facts are needed. --source active lists the running tasks: the default, archive, holds only finished ones. The
|
|
# fact is set on every run, so a guard that is off or skipped never inherits the answer of an earlier run in the same play.
|
|
- name: List the recent Proxmox tasks of this node
|
|
ansible.builtin.command:
|
|
argv:
|
|
- pvesh
|
|
- get
|
|
- /nodes/localhost/tasks
|
|
- --source
|
|
- active
|
|
- --limit
|
|
- "0"
|
|
- --output-format
|
|
- json
|
|
register: __system_baseline_pve_tasks
|
|
changed_when: false
|
|
check_mode: false
|
|
become: true
|
|
when:
|
|
- system_baseline_skip_upgrade_during_vzdump | bool
|
|
- system_baseline_system_type | default('') == 'pve'
|
|
- not (ansible_check_mode and __system_baseline_main_flow | default(false))
|
|
|
|
- name: Check that pvesh answered with a list of tasks
|
|
ansible.builtin.assert:
|
|
that:
|
|
- __system_baseline_pve_tasks.stdout | from_json is sequence
|
|
- __system_baseline_pve_tasks.stdout | from_json is not string
|
|
- __system_baseline_pve_tasks.stdout | from_json is not mapping
|
|
fail_msg: pvesh did not answer with a JSON list of tasks, so the role cannot tell whether a backup is running.
|
|
quiet: true
|
|
when: __system_baseline_pve_tasks is not skipped
|
|
|
|
- name: Note whether a vzdump backup is running on this node
|
|
ansible.builtin.set_fact:
|
|
__system_baseline_vzdump_running: >-
|
|
{{ __system_baseline_pve_tasks is not skipped
|
|
and (__system_baseline_pve_tasks.stdout | from_json
|
|
| selectattr('type', 'equalto', 'vzdump')
|
|
| rejectattr('endtime', 'defined')
|
|
| list | length) > 0 }}
|
|
|
|
- name: Skip the upgrade while a vzdump backup is running
|
|
ansible.builtin.debug:
|
|
msg: >-
|
|
Skipping the apt upgrade on {{ inventory_hostname }}: a vzdump task is running on this node. Run the update again after the backup
|
|
has finished.
|
|
when: __system_baseline_vzdump_running | default(false)
|
|
|
|
# safe is the apt module's name for the plain upgrade (apt-get upgrade --with-new-pkgs), what `true` meant here before; dist runs
|
|
# apt-get dist-upgrade (profile key apt_upgrade, `pve` by default).
|
|
- name: Update & upgrade apt packages
|
|
ansible.builtin.package:
|
|
update_cache: true
|
|
upgrade: "{{ system_baseline_apt_upgrade }}"
|
|
cache_valid_time: 3600
|
|
environment:
|
|
TMPDIR: "{{ system_baseline_apt_tmpdir }}"
|
|
become: true
|
|
when:
|
|
- not (ansible_check_mode and __system_baseline_main_flow | default(false))
|
|
- not (__system_baseline_vzdump_running | default(false))
|
|
|
|
# autoremove must stay in this task: the apt module accepts autoclean alone only together with one of autoremove, name, update_cache or upgrade.
|
|
- name: Remove unused packages and clean the apt cache
|
|
ansible.builtin.package:
|
|
autoremove: true
|
|
autoclean: true
|
|
environment:
|
|
TMPDIR: "{{ system_baseline_apt_tmpdir }}"
|
|
become: true
|
|
|
|
# A restart of ssh.service ends the connection that was open during the upgrade; a new one gets a new private /tmp, which later tasks
|
|
# of the play (and a caller's own tasks) may use.
|
|
- name: Open a new connection after the upgrade
|
|
ansible.builtin.meta: reset_connection
|
|
when: not (ansible_check_mode and __system_baseline_main_flow | default(false))
|