From a3d4e9b4fad21a26d75c5875283a9ff21052fcdd Mon Sep 17 00:00:00 2001 From: Vincent Stuyck Date: Fri, 30 Oct 2026 19:42:55 +0100 Subject: [PATCH] Renew Checkmk agent certificates during updates --- CLAUDE.md | 4 ++-- playbooks/update-checkmk.yml | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 53d224c..ab1b6da 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -37,8 +37,8 @@ The `# noqa:` annotations in the playbooks are for ansible-lint (`ansible-lint p - `update-checkmk.yml` has three plays that share state through a fact: 1. On `all` hosts, delegated to localhost, it gets the latest stable Checkmk tag from GitHub with `git ls-remote` and sets `checkmk_version`. It targets `all` on purpose: a `set_fact` only applies to the hosts in its own play, so `sites` and `agents` would lose the fact if the play ran only on `localhost` (or they'd have to read `hostvars['localhost'].checkmk_version`). - 2. On `sites`, it snapshots the VM through the Proxmox API (delegated to localhost, using `inventory_hostname_short` as the Proxmox VM name), installs the community-edition `.deb`, then runs `omd stop`, `backup`, `update` and `start` for each entry in `checkmk_sites`. - 3. On `agents`, it installs the agent `.deb` from the local Checkmk server (`checkmk.stuyckv.com/local`). + 2. On `sites`, it reads the installed version with `omd version -b` (as the login user, no `become`) and ends the play for that host (`meta: end_host`) when it already matches `checkmk_version`. Otherwise it snapshots the VM through the Proxmox API (delegated to localhost, using `inventory_hostname_short` as the Proxmox VM name), installs the community-edition `.deb`, then runs `omd stop`, `backup`, `update` and `start` for each entry in `checkmk_sites`. + 3. On `agents`, it installs the agent `.deb` from the local Checkmk server (`checkmk.stuyckv.com/local`), then registers the agent for TLS with `cmk-agent-ctl register`, using the `checkmk_api_*` vars. It registers on every run on purpose, because registering again renews the agent certificate. `checkmk.general` has an `agent` role but no `agent` module, so this is done with a plain command. Package installs only run when `ansible_facts.lsb.id == 'Ubuntu'`. - `update-debian.yml` runs against `ubuntu`. It sets a Checkmk downtime through `checkmk.general`, then runs dist-upgrade, reboots if needed and autoremoves. Like the Proxmox snapshot, the downtime task is delegated to localhost with `become: false`. diff --git a/playbooks/update-checkmk.yml b/playbooks/update-checkmk.yml index 5df41b0..21ad73f 100644 --- a/playbooks/update-checkmk.yml +++ b/playbooks/update-checkmk.yml @@ -91,9 +91,26 @@ - name: Update checkmk agents on monitored hosts hosts: agents become: true + vars: + checkmk_server: "{{ checkmk_api_url | urlsplit('hostname') }}" tasks: - name: Install new checkmk agent ansible.builtin.apt: deb: "https://checkmk.stuyckv.com/local/check_mk/agents/check-mk-agent_{{ checkmk_version }}-1_all.deb" when: ansible_facts.lsb.id == 'Ubuntu' + + # registering again renews the agent certificate + - name: Register agent with the site + when: ansible_facts.lsb.id == 'Ubuntu' + changed_when: true + ansible.builtin.command: + argv: + - cmk-agent-ctl + - register + - --trust-cert + - --hostname={{ inventory_hostname }} + - --server={{ checkmk_server }} + - --site={{ checkmk_api_site }} + - --user={{ checkmk_api_user }} + - --password={{ checkmk_api_secret }}