Files
ansible-homelab/playbooks/update-checkmk.yml
T

117 lines
4.0 KiB
YAML

- name: Gather latest checkmk version
hosts: all
tasks:
# cannot be done with git module. It has no method to query the tags of a repo
- name: Get latest checkmk version # noqa: run-once[task] command-instead-of-module
register: checkmk_version
delegate_to: localhost
changed_when: false
ansible.builtin.shell:
executable: /bin/bash
cmd: |
set -o pipefail
git -c 'versionsort.suffix=-' \
ls-remote --tags --sort='v:refname' \
https://github.com/Checkmk/checkmk.git |\
grep -v '\-rc.' | tail --lines=1 |\
cut --delimiter='/' --fields=3 | tr -d "v^{}"
- name: Output checkmk version # noqa: run-once[task]
run_once: true
ansible.builtin.debug:
var: checkmk_version.stdout
- name: Set checkmk version fact # noqa: run-once[task]
run_once: true
ansible.builtin.set_fact:
checkmk_version: "{{ checkmk_version.stdout }}"
- name: Update checkmk on monitoring hosts
hosts: sites
become: true
tasks:
- name: Get current checkmk version
become: false
register: current_version
changed_when: false
ansible.builtin.command: omd version -b
# omd appends the edition to the version (e.g. 2.4.0p12.cre), the git tag doesn't
- name: Stop when checkmk is up to date
when: current_version.stdout | regex_replace('[.][a-z]+$', '') == checkmk_version
ansible.builtin.meta: end_host
- name: Snapshot VM before update
become: false
delegate_to: localhost
community.proxmox.proxmox_snap:
api_host: "{{ proxmox_api_host }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
validate_certs: false
hostname: "{{ inventory_hostname_short }}"
snapname: "checkmk_{{ checkmk_version | replace('.', '_') | replace('-', '_') }}"
description: "Pre-update snapshot before checkmk {{ checkmk_version }}"
timeout: 120
state: present
- name: Install new checkmk version
vars:
checkmk_edition: community
filename: "check-mk-{{ checkmk_edition }}-{{ checkmk_version }}_0.{{ ansible_facts.lsb.codename }}_amd64.deb"
ansible.builtin.apt:
deb: "https://download.checkmk.com/checkmk/{{ checkmk_version }}/{{ filename }}"
when: ansible_facts.lsb.id == 'Ubuntu'
- name: Stop omd sites
register: stop_cmd
changed_when: stop_cmd.rc == 0
loop: "{{ checkmk_sites }}"
ansible.builtin.command: "omd stop {{ item }}"
- name: Create Site backups
changed_when: true
loop: "{{ checkmk_sites }}"
ansible.builtin.command: "omd backup {{ item }} /tmp/backup-{{ item }}.tar.gz"
- name: Update omd site
changed_when: true
loop: "{{ checkmk_sites }}"
ansible.builtin.command: "omd -f update --conflict=keepold {{ item }}"
- name: Start omd sites
register: start_cmd
changed_when: start_cmd.rc == 0
loop: "{{ checkmk_sites }}"
ansible.builtin.command: "omd start {{ item }}"
- 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 }}