forked from jakub/ansible
Refactor update_uptime_kuma.yml: enhance task descriptions and streamline variable definitions for clarity
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# nextcloud/update_uptime_kuma.yml
|
||||
|
||||
- name: Update Uptime Kuma on VM via Proxmox
|
||||
- name: Update Uptime Kuma on VM via Proxmox (auto-discover compose path)
|
||||
hosts: proxmox
|
||||
gather_facts: false
|
||||
become: true
|
||||
@@ -14,20 +14,13 @@
|
||||
vm_pass: "{{ lookup('env', 'VM_PASS') }}"
|
||||
use_sudo: false
|
||||
|
||||
# ---- Uptime Kuma specifics ----
|
||||
# ---- Inputs / defaults ----
|
||||
kuma_url: "https://monitor.martinfencl.eu/"
|
||||
kuma_project: "uptime-kuma" # adjust if your compose project has a different name
|
||||
kuma_compose_file: "/data/compose/uptime-kuma/docker-compose.yml" # adjust path to your compose file
|
||||
kuma_container_name: "uptime-kuma-dev" # running container name to inspect
|
||||
|
||||
# ---- Docker CLI prefix (keeps your style) ----
|
||||
# ---- Docker CLI prefix (consistent with your style) ----
|
||||
docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker"
|
||||
|
||||
# ---- Update commands: pull image + compose pull/up for the specific service ----
|
||||
kuma_commands:
|
||||
- "{{ docker_prefix }} pull -q louislam/uptime-kuma:latest >/dev/null"
|
||||
- "{{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }} pull uptime-kuma >/dev/null"
|
||||
- "{{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }} up -d --no-deps --force-recreate uptime-kuma >/dev/null"
|
||||
|
||||
tasks:
|
||||
- name: Ensure sshpass is installed (for password-based SSH) # English comments
|
||||
ansible.builtin.apt:
|
||||
@@ -35,7 +28,8 @@
|
||||
state: present
|
||||
update_cache: yes
|
||||
|
||||
- name: Run Uptime Kuma update commands on VM (via SSH)
|
||||
# --- Discover compose metadata from the running container labels ---
|
||||
- name: Discover compose labels from the container (project, service, working_dir)
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- sshpass
|
||||
@@ -49,28 +43,141 @@
|
||||
- "{{ vm_user }}@{{ vm_ip }}"
|
||||
- bash
|
||||
- -lc
|
||||
- "{{ ('sudo ' if use_sudo else '') + item }}"
|
||||
loop: "{{ kuma_commands }}"
|
||||
register: kuma_cmds
|
||||
- >
|
||||
{{ docker_prefix }} inspect {{ kuma_container_name }}
|
||||
--format '{{"{{"}}json .Config.Labels{{"}}"}}'
|
||||
register: kuma_labels_raw
|
||||
changed_when: false
|
||||
|
||||
- name: Show outputs for each Uptime Kuma command
|
||||
- name: Parse compose labels JSON
|
||||
ansible.builtin.set_fact:
|
||||
kuma_labels: "{{ kuma_labels_raw.stdout | from_json }}"
|
||||
failed_when: false
|
||||
|
||||
- name: Derive compose parameters (project, service, working_dir, compose_file)
|
||||
ansible.builtin.set_fact:
|
||||
kuma_project: "{{ kuma_labels['com.docker.compose.project'] | default('kuma') }}"
|
||||
kuma_service: "{{ kuma_labels['com.docker.compose.service'] | default('uptime-kuma') }}"
|
||||
kuma_workdir: "{{ kuma_labels['com.docker.compose.project.working_dir'] | default('') }}"
|
||||
kuma_compose_file: >-
|
||||
{{
|
||||
(kuma_labels['com.docker.compose.project.working_dir'] | default('') ~ '/docker-compose.yml')
|
||||
if (kuma_labels['com.docker.compose.project.working_dir'] | default('')) != '' else omit
|
||||
}}
|
||||
when: kuma_labels is defined
|
||||
failed_when: false
|
||||
|
||||
- name: Debug | Discovered compose info
|
||||
ansible.builtin.debug:
|
||||
msg: |
|
||||
CMD: {{ item.item }}
|
||||
RC: {{ item.rc }}
|
||||
STDOUT:
|
||||
{{ (item.stdout | default('')).strip() }}
|
||||
STDERR:
|
||||
{{ (item.stderr | default('')).strip() }}
|
||||
loop: "{{ kuma_cmds.results }}"
|
||||
Discovered:
|
||||
project={{ kuma_project | default('n/a') }}
|
||||
service={{ kuma_service | default('n/a') }}
|
||||
working_dir={{ kuma_workdir | default('n/a') }}
|
||||
compose_file={{ kuma_compose_file | default('n/a') }}
|
||||
|
||||
- name: Fail play if any Uptime Kuma command failed
|
||||
# --- Verify compose file existence on the VM ---
|
||||
- name: Check that compose file exists on VM
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- sshpass
|
||||
- -p
|
||||
- "{{ vm_pass }}"
|
||||
- ssh
|
||||
- -o
|
||||
- StrictHostKeyChecking=no
|
||||
- -o
|
||||
- ConnectTimeout=15
|
||||
- "{{ vm_user }}@{{ vm_ip }}"
|
||||
- bash
|
||||
- -lc
|
||||
- "test -f '{{ kuma_compose_file }}' && echo OK || echo MISSING"
|
||||
register: kuma_compose_check
|
||||
changed_when: false
|
||||
when: kuma_compose_file is defined
|
||||
|
||||
- name: Fail early if compose file is missing
|
||||
ansible.builtin.fail:
|
||||
msg: >-
|
||||
Compose file not found on VM: {{ kuma_compose_file | default('?') }}.
|
||||
Discovered working_dir={{ kuma_workdir | default('?') }}, project={{ kuma_project | default('?') }}, service={{ kuma_service | default('?') }}.
|
||||
The container seems to be managed by Portainer; expected path like /data/compose/<stack_id>/<version>/docker-compose.yml.
|
||||
when: kuma_compose_file is not defined or (kuma_compose_check.stdout | default('MISSING')) != "OK"
|
||||
|
||||
# --- Pull latest image first (generic pull) ---
|
||||
- name: Pull image louislam/uptime-kuma:latest
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- sshpass
|
||||
- -p
|
||||
- "{{ vm_pass }}"
|
||||
- ssh
|
||||
- -o
|
||||
- StrictHostKeyChecking=no
|
||||
- -o
|
||||
- ConnectTimeout=15
|
||||
- "{{ vm_user }}@{{ vm_ip }}"
|
||||
- bash
|
||||
- -lc
|
||||
- "{{ ('sudo ' if use_sudo else '') + docker_prefix }} pull -q louislam/uptime-kuma:latest >/dev/null"
|
||||
register: kuma_pull
|
||||
changed_when: false
|
||||
|
||||
# --- Compose pull/up for the discovered service only ---
|
||||
- name: docker compose pull {{ kuma_service }}
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- sshpass
|
||||
- -p
|
||||
- "{{ vm_pass }}"
|
||||
- ssh
|
||||
- -o
|
||||
- StrictHostKeyChecking=no
|
||||
- -o
|
||||
- ConnectTimeout=15
|
||||
- "{{ vm_user }}@{{ vm_ip }}"
|
||||
- bash
|
||||
- -lc
|
||||
- >
|
||||
{{ ('sudo ' if use_sudo else '') + docker_prefix }}
|
||||
compose -p {{ kuma_project }} -f '{{ kuma_compose_file }}' pull {{ kuma_service }} >/dev/null
|
||||
register: kuma_comp_pull
|
||||
changed_when: false
|
||||
|
||||
- name: docker compose up --no-deps --force-recreate {{ kuma_service }}
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- sshpass
|
||||
- -p
|
||||
- "{{ vm_pass }}"
|
||||
- ssh
|
||||
- -o
|
||||
- StrictHostKeyChecking=no
|
||||
- -o
|
||||
- ConnectTimeout=15
|
||||
- "{{ vm_user }}@{{ vm_ip }}"
|
||||
- bash
|
||||
- -lc
|
||||
- >
|
||||
{{ ('sudo ' if use_sudo else '') + docker_prefix }}
|
||||
compose -p {{ kuma_project }} -f '{{ kuma_compose_file }}'
|
||||
up -d --no-deps --force-recreate {{ kuma_service }} >/dev/null
|
||||
register: kuma_comp_up
|
||||
changed_when: false
|
||||
|
||||
- name: Show outputs of compose pull/up
|
||||
ansible.builtin.debug:
|
||||
msg: |
|
||||
PULL rc={{ kuma_comp_pull.rc }} stderr="{{ (kuma_comp_pull.stderr | default('')).strip() }}"
|
||||
UP rc={{ kuma_comp_up.rc }} stderr="{{ (kuma_comp_up.stderr | default('')).strip() }}"
|
||||
|
||||
- name: Assert compose pull/up succeeded
|
||||
ansible.builtin.assert:
|
||||
that: "item.rc == 0"
|
||||
fail_msg: "Uptime Kuma update failed on VM: {{ item.item }} (rc={{ item.rc }})"
|
||||
success_msg: "All Uptime Kuma update commands succeeded."
|
||||
loop: "{{ kuma_cmds.results }}"
|
||||
that:
|
||||
- kuma_comp_pull.rc == 0
|
||||
- kuma_comp_up.rc == 0
|
||||
fail_msg: "docker compose pull/up failed (see previous stderr)."
|
||||
success_msg: "Uptime Kuma updated and recreated successfully."
|
||||
|
||||
# ---- Health check from the controller: wait for 200 on the public URL ----
|
||||
- name: Uptime Kuma | Wait for web to return 200 (controller first)
|
||||
@@ -83,13 +190,13 @@
|
||||
register: kuma_controller
|
||||
delegate_to: localhost
|
||||
run_once: true
|
||||
retries: 15 # allow a bit more time after container recreate
|
||||
retries: 15
|
||||
delay: 2
|
||||
until: kuma_controller.status == 200
|
||||
failed_when: false
|
||||
changed_when: false
|
||||
|
||||
# ---- Optional: VM-side fetch (curl via SSH) to double-check reachability from the VM ----
|
||||
# ---- Optional VM-side fetch (double-check from VM) ----
|
||||
- name: Uptime Kuma | VM-side fetch HTML (via Python)
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
@@ -131,11 +238,10 @@
|
||||
}}
|
||||
failed_when: false
|
||||
|
||||
# ---- Print concise summary (tries to extract <title> if present) ----
|
||||
- name: Uptime Kuma | Print concise summary
|
||||
ansible.builtin.debug:
|
||||
msg: >-
|
||||
Uptime Kuma is reachable at {{ kuma_url }}
|
||||
Uptime Kuma reachable at {{ kuma_url }}
|
||||
(HTTP {{ kuma_controller.status | default('unknown') }}).
|
||||
Title="{{ (kuma_html | default('') | regex_search('(?is)<title[^>]*>(.*?)</title>', '\\1')) | default('n/a') }}"
|
||||
when: kuma_html is defined
|
||||
@@ -144,9 +250,3 @@
|
||||
ansible.builtin.debug:
|
||||
msg: "Uptime Kuma web není dostupný ani po pokusech."
|
||||
when: kuma_html is not defined
|
||||
|
||||
# ---- Optional: full HTML length (debug only, not the content itself) ----
|
||||
- name: Uptime Kuma | HTML length (debug)
|
||||
ansible.builtin.debug:
|
||||
msg: "Fetched HTML length: {{ (kuma_html | default('') | length) }}"
|
||||
when: kuma_html is defined
|
||||
Reference in New Issue
Block a user