Files
Kamino/roles/_binary_update
Defame1297 75d82becbf
Build / build (pull_request) Successful in 35s
Lint / lint (pull_request) Successful in 1m27s
Molecule / molecule (pull_request) Canceled after 10m22s
chore!: raise the ansible-core floor to 2.22, the pinned core
requires_ansible and every role's min_ansible_version said 2.19, which CI
never tested; CI tests only the pinned ansible-core (2.22). The floor now
equals that minor. ansible-lint 26.10.1's built-in supported list ends at
2.19, so .ansible-lint adds 2.22 for meta-runtime[unsupported-version].

BREAKING CHANGE: ansible-core 2.19, 2.20 and 2.21 (and controller Python
3.12) are no longer supported.

Closes #164

Assisted-by: Claude Code
2026-11-24 12:46:38 +00:00
..

_binary_update

Internal role. It is called only by other roles of this Collection and is not part of the Collection's public API: its interface can change in any release, without a major version. Do not call it from a play; it refuses to run without a parent role.

It swaps a new binary into place for a systemd service and rolls back when the update fails. The calling role does the download and the verification (a checksum, a signature, a checksum file: each upstream differs) and stages the verified file next to the live binary before it includes this role, so a slow or failed download never stops the service.

Variables and their types are defined in meta/argument_specs.yml, which is the source of truth.

What it does

  1. Refuses to continue, with the service untouched, when nothing executable is staged.
  2. Reloads systemd, stops the service and waits until it has stopped.
  3. Copies the live binary to <binary>.bak, when there is one.
  4. Moves the staged file over the live binary: one rename on one filesystem, so the live path never holds a partial file.
  5. Starts the service, waits until it is active and compares the version the new binary reports with the expected version.
  6. Removes the backup.

Any failure after the stop restores the backup of this run, starts the service again and fails with a message that says whether the restore and the restart worked. The staged file is removed in every case. A .bak left by an earlier run is never restored, only replaced.

Under --check the waits and the version check are skipped, so a preview against a stopped service does not report a rollback; the stop, the backup and the start report what they would change (for a running service with a live binary: all three).

Limits:

  • The backup keeps the owner, group and mode of the live binary, but nothing else (no extended attributes or file capabilities).
  • Only one backup is kept. When a restore fails, restore <binary>.bak by hand before the next run: that run replaces it.
  • "Active" is checked once the unit reports it. A Type=simple service that crashes a moment after it started passes the wait and is caught only by the version check, if at all.

Calling it

- name: Swap in the new binary
  ansible.builtin.include_role:
    name: mortis.kamino._binary_update
    apply:
      tags: [myservice, update]
  vars:
    _binary_update_service: myservice
    _binary_update_binary_path: /usr/local/bin/myservice
    _binary_update_staged_path: /usr/local/bin/myservice.new
    _binary_update_version_command: [/usr/local/bin/myservice, --version]
    _binary_update_expected_version: "{{ myservice_version }}"
  tags: [myservice, update]

An include does not pass its tags on to the included tasks, so pass them with apply as well. Compare the installed version first and stage and include only when it differs; the role itself always stops and starts the service.

Testing

Two molecule scenarios run against stub binaries (shell scripts) under a stub unit, with no network: default covers a successful bump, its idempotence, a binary that cannot start, a binary that reports another version (both rolled back), a run with nothing staged (the service is not stopped), a direct call from a play (refused) and a first install with nothing to back up. check_mode runs the bump under --check against a stopped service and checks that nothing changed. The calling roles' own scenarios cover a real upstream binary.