forked from jakub/ansible
.
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
# nextcloud/update_uptime_kuma.yml
|
# nextcloud/update_uptime_kuma.yml
|
||||||
|
|
||||||
- name: Update Uptime Kuma on VM via Proxmox
|
- name: Update Uptime Kuma on VM via Proxmox
|
||||||
hosts: proxmox
|
hosts: proxmox
|
||||||
gather_facts: false
|
gather_facts: false
|
||||||
@@ -7,48 +8,34 @@
|
|||||||
become_method: sudo
|
become_method: sudo
|
||||||
|
|
||||||
vars:
|
vars:
|
||||||
# --- Connection to VM (provided by Semaphore env vars) ---
|
# VM connection (provided by Semaphore env vars)
|
||||||
vm_ip: "{{ lookup('env', 'VM_IP') }}"
|
vm_ip: "{{ lookup('env', 'VM_IP') }}"
|
||||||
vm_user: "{{ lookup('env', 'VM_USER') }}"
|
vm_user: "{{ lookup('env', 'VM_USER') }}"
|
||||||
vm_pass: "{{ lookup('env', 'VM_PASS') }}"
|
vm_pass: "{{ lookup('env', 'VM_PASS') }}"
|
||||||
use_sudo: false
|
use_sudo: false
|
||||||
|
|
||||||
# --- Debug toggle (set DEBUG=1 in Semaphore to see raw stdout/stderr) ---
|
# Uptime Kuma specifics
|
||||||
kuma_debug: "{{ (lookup('env','DEBUG') | default('0')) | bool }}"
|
kuma_project: "uptime-kuma"
|
||||||
|
|
||||||
# --- Uptime Kuma specifics ---
|
|
||||||
kuma_project: "uptime-kuma" # docker compose project name
|
|
||||||
kuma_compose_file: "/data/compose/uptime-kuma.yml"
|
kuma_compose_file: "/data/compose/uptime-kuma.yml"
|
||||||
kuma_service: "uptime-kuma" # service name in the compose file
|
kuma_service: "uptime-kuma"
|
||||||
kuma_image: "louislam/uptime-kuma:latest"
|
kuma_image: "louislam/uptime-kuma:latest"
|
||||||
kuma_port: 3001
|
kuma_port: 3001
|
||||||
kuma_url: "{{ lookup('env', 'KUMA_URL') | default('', true) }}" # optional public URL
|
|
||||||
|
|
||||||
# Fixed container name used in your compose (may conflict with pre-existing container)
|
# Optional external URL for controller-side readiness check (e.g., https://kuma.example.com).
|
||||||
kuma_container_name: "uptime-kuma-dev"
|
# If empty/undefined, controller check is skipped and we only probe from the VM.
|
||||||
kuma_force_replace_conflict: true # remove conflicting container automatically
|
kuma_url: "{{ lookup('env', 'KUMA_URL') | default('', true) }}"
|
||||||
kuma_remove_orphans: true # remove containers not present in the compose file
|
|
||||||
|
|
||||||
# Host path used by your volume mapping in compose (ensure it exists)
|
# Debug toggle for the final HTML excerpt
|
||||||
kuma_data_dir: "/data/compose/kuma/data"
|
kuma_debug_home: true
|
||||||
|
|
||||||
# Docker command prefix (consistent behavior and quiet hints)
|
# Docker command prefix (consistent behavior)
|
||||||
docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker"
|
docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker"
|
||||||
|
|
||||||
# Commands to run on the target VM (outputs are visible when kuma_debug=true)
|
# Commands to run on the target VM (quiet outputs like in Collabora play)
|
||||||
kuma_commands:
|
kuma_commands:
|
||||||
# 0) pull image (helpful cache warm-up)
|
- "{{ docker_prefix }} pull -q {{ kuma_image }} >/dev/null"
|
||||||
- "{{ docker_prefix }} pull -q {{ kuma_image }}"
|
- "{{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }} pull {{ kuma_service }} >/dev/null"
|
||||||
# 1) show services resolved from the compose file (helps debugging service names)
|
- "{{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }} up -d --no-deps --force-recreate {{ kuma_service }} >/dev/null"
|
||||||
- "{{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }} config --services"
|
|
||||||
# 2) pull service
|
|
||||||
- "{{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }} pull {{ kuma_service }}"
|
|
||||||
# 3) up the service (optionally remove orphans)
|
|
||||||
- >-
|
|
||||||
{{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }}
|
|
||||||
up -d --no-deps --force-recreate
|
|
||||||
{{ '--remove-orphans' if kuma_remove_orphans else '' }}
|
|
||||||
{{ kuma_service }}
|
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
- name: Ensure sshpass is installed (for password-based SSH) # English comments
|
- name: Ensure sshpass is installed (for password-based SSH) # English comments
|
||||||
@@ -57,44 +44,7 @@
|
|||||||
state: present
|
state: present
|
||||||
update_cache: yes
|
update_cache: yes
|
||||||
|
|
||||||
# -------------------------
|
- name: Run Uptime Kuma update commands on VM (via SSH)
|
||||||
# Preflight: connectivity and inputs
|
|
||||||
# -------------------------
|
|
||||||
|
|
||||||
- name: Preflight | Validate required inputs are present
|
|
||||||
ansible.builtin.assert:
|
|
||||||
that:
|
|
||||||
- vm_ip | length > 0
|
|
||||||
- vm_user | length > 0
|
|
||||||
- vm_pass | length > 0
|
|
||||||
fail_msg: "Missing VM connection variables (VM_IP/VM_USER/VM_PASS). Check Semaphore template variables."
|
|
||||||
success_msg: "Inputs look good."
|
|
||||||
|
|
||||||
- name: Preflight | Check SSH connectivity to VM
|
|
||||||
ansible.builtin.command:
|
|
||||||
argv:
|
|
||||||
- sshpass
|
|
||||||
- -p
|
|
||||||
- "{{ vm_pass }}"
|
|
||||||
- ssh
|
|
||||||
- -o
|
|
||||||
- StrictHostKeyChecking=no
|
|
||||||
- -o
|
|
||||||
- ConnectTimeout=8
|
|
||||||
- "{{ vm_user }}@{{ vm_ip }}"
|
|
||||||
- bash
|
|
||||||
- -lc
|
|
||||||
- "echo OK"
|
|
||||||
register: preflight_ssh_ok
|
|
||||||
changed_when: false
|
|
||||||
failed_when: preflight_ssh_ok.rc != 0 or (preflight_ssh_ok.stdout | trim) != 'OK'
|
|
||||||
no_log: "{{ not kuma_debug }}"
|
|
||||||
|
|
||||||
# -------------------------
|
|
||||||
# Preflight: compose sanity
|
|
||||||
# -------------------------
|
|
||||||
|
|
||||||
- name: Preflight | Compose file exists on VM (do not fail here)
|
|
||||||
ansible.builtin.command:
|
ansible.builtin.command:
|
||||||
argv:
|
argv:
|
||||||
- sshpass
|
- sshpass
|
||||||
@@ -108,199 +58,52 @@
|
|||||||
- "{{ vm_user }}@{{ vm_ip }}"
|
- "{{ vm_user }}@{{ vm_ip }}"
|
||||||
- bash
|
- bash
|
||||||
- -lc
|
- -lc
|
||||||
- "test -r {{ kuma_compose_file }}"
|
|
||||||
register: preflight_compose_exists
|
|
||||||
changed_when: false
|
|
||||||
failed_when: false
|
|
||||||
no_log: "{{ not kuma_debug }}"
|
|
||||||
|
|
||||||
- name: Preflight | Validate compose file syntax (do not fail here)
|
|
||||||
ansible.builtin.command:
|
|
||||||
argv:
|
|
||||||
- sshpass
|
|
||||||
- -p
|
|
||||||
- "{{ vm_pass }}"
|
|
||||||
- ssh
|
|
||||||
- -o
|
|
||||||
- StrictHostKeyChecking=no
|
|
||||||
- -o
|
|
||||||
- ConnectTimeout=15
|
|
||||||
- "{{ vm_user }}@{{ vm_ip }}"
|
|
||||||
- bash
|
|
||||||
- -lc
|
|
||||||
- "{{ docker_prefix }} compose -f {{ kuma_compose_file }} config -q"
|
|
||||||
register: preflight_compose_valid
|
|
||||||
changed_when: false
|
|
||||||
failed_when: false
|
|
||||||
no_log: "{{ not kuma_debug }}"
|
|
||||||
|
|
||||||
- name: Preflight | Ensure service exists in compose (do not fail here)
|
|
||||||
ansible.builtin.command:
|
|
||||||
argv:
|
|
||||||
- sshpass
|
|
||||||
- -p
|
|
||||||
- "{{ vm_pass }}"
|
|
||||||
- ssh
|
|
||||||
- -o
|
|
||||||
- StrictHostKeyChecking=no
|
|
||||||
- -o
|
|
||||||
- ConnectTimeout=15
|
|
||||||
- "{{ vm_user }}@{{ vm_ip }}"
|
|
||||||
- bash
|
|
||||||
- -lc
|
|
||||||
- >-
|
|
||||||
{{ docker_prefix }} compose -f {{ kuma_compose_file }} config --services | grep -x {{ kuma_service }}
|
|
||||||
register: preflight_service_exists
|
|
||||||
changed_when: false
|
|
||||||
failed_when: false
|
|
||||||
no_log: "{{ not kuma_debug }}"
|
|
||||||
|
|
||||||
- name: Preflight | Assert compose prerequisites
|
|
||||||
ansible.builtin.assert:
|
|
||||||
that:
|
|
||||||
- preflight_compose_exists.rc == 0
|
|
||||||
- preflight_compose_valid.rc == 0
|
|
||||||
- preflight_service_exists.rc == 0
|
|
||||||
fail_msg: >-
|
|
||||||
Compose preflight failed on {{ vm_ip }} as {{ vm_user }}.
|
|
||||||
- exists: rc={{ preflight_compose_exists.rc }}
|
|
||||||
- syntax: rc={{ preflight_compose_valid.rc }}
|
|
||||||
- service "{{ kuma_service }}": rc={{ preflight_service_exists.rc }}
|
|
||||||
Hints:
|
|
||||||
* Verify {{ kuma_compose_file }} path on VM and its permissions.
|
|
||||||
* Check that service name in the compose matches "kuma_service".
|
|
||||||
* If SSH connectivity is flaky, re-check VM_IP/USER/PASS.
|
|
||||||
success_msg: "Compose file present, valid, and service '{{ kuma_service }}' found."
|
|
||||||
|
|
||||||
- name: Preflight | Ensure Kuma data dir exists (host path from compose)
|
|
||||||
ansible.builtin.command:
|
|
||||||
argv:
|
|
||||||
- sshpass
|
|
||||||
- -p
|
|
||||||
- "{{ vm_pass }}"
|
|
||||||
- ssh
|
|
||||||
- -o
|
|
||||||
- StrictHostKeyChecking=no
|
|
||||||
- -o
|
|
||||||
- ConnectTimeout=15
|
|
||||||
- "{{ vm_user }}@{{ vm_ip }}"
|
|
||||||
- bash
|
|
||||||
- -lc
|
|
||||||
- "mkdir -p {{ kuma_data_dir }}"
|
|
||||||
changed_when: false
|
|
||||||
no_log: "{{ not kuma_debug }}"
|
|
||||||
|
|
||||||
- name: Preflight | Detect conflicting container by fixed name
|
|
||||||
ansible.builtin.command:
|
|
||||||
argv:
|
|
||||||
- sshpass
|
|
||||||
- -p
|
|
||||||
- "{{ vm_pass }}"
|
|
||||||
- ssh
|
|
||||||
- -o
|
|
||||||
- StrictHostKeyChecking=no
|
|
||||||
- -o
|
|
||||||
- ConnectTimeout=15
|
|
||||||
- "{{ vm_user }}@{{ vm_ip }}"
|
|
||||||
- bash
|
|
||||||
- -lc
|
|
||||||
- "docker ps -a --filter name=^/{{ kuma_container_name }}$ --format '{{'{{'}}.ID{{'}}'}}'"
|
|
||||||
register: kuma_conflict
|
|
||||||
changed_when: false
|
|
||||||
failed_when: false
|
|
||||||
no_log: "{{ not kuma_debug }}"
|
|
||||||
|
|
||||||
- name: Preflight | Remove conflicting container if present (and allowed)
|
|
||||||
ansible.builtin.command:
|
|
||||||
argv:
|
|
||||||
- sshpass
|
|
||||||
- -p
|
|
||||||
- "{{ vm_pass }}"
|
|
||||||
- ssh
|
|
||||||
- -o
|
|
||||||
- StrictHostKeyChecking=no
|
|
||||||
- -o
|
|
||||||
- ConnectTimeout=15
|
|
||||||
- "{{ vm_user }}@{{ vm_ip }}"
|
|
||||||
- bash
|
|
||||||
- -lc
|
|
||||||
- "docker rm -f {{ kuma_container_name }}"
|
|
||||||
when:
|
|
||||||
- kuma_force_replace_conflict | bool
|
|
||||||
- (kuma_conflict.stdout | default('') | trim) | length > 0
|
|
||||||
changed_when: true
|
|
||||||
no_log: "{{ not kuma_debug }}"
|
|
||||||
|
|
||||||
# -------------------------
|
|
||||||
# Update commands
|
|
||||||
# -------------------------
|
|
||||||
|
|
||||||
- name: Run Uptime Kuma update commands on VM (via SSH) # Shows details when DEBUG=1
|
|
||||||
ansible.builtin.command:
|
|
||||||
argv:
|
|
||||||
- sshpass
|
|
||||||
- -p
|
|
||||||
- "{{ vm_pass }}"
|
|
||||||
- ssh
|
|
||||||
- -o
|
|
||||||
- StrictHostKeyChecking=no
|
|
||||||
- -o
|
|
||||||
- ConnectTimeout=20
|
|
||||||
- "{{ vm_user }}@{{ vm_ip }}"
|
|
||||||
- bash
|
|
||||||
- -lc
|
|
||||||
- "{{ ('sudo ' if use_sudo else '') + item }}"
|
- "{{ ('sudo ' if use_sudo else '') + item }}"
|
||||||
loop: "{{ kuma_commands }}"
|
loop: "{{ kuma_commands }}"
|
||||||
register: kuma_cmds
|
register: kuma_cmds
|
||||||
changed_when: false
|
changed_when: false
|
||||||
ignore_errors: true
|
|
||||||
no_log: "{{ not kuma_debug }}"
|
|
||||||
|
|
||||||
- name: Show summarized outputs for each command (sanitized)
|
- name: Show outputs for each Uptime Kuma command
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
msg: |
|
msg: |
|
||||||
CMD: {{ item_short }}
|
CMD: {{ item.item }}
|
||||||
RC: {{ item.rc }}
|
RC: {{ item.rc }}
|
||||||
STDOUT:
|
STDOUT:
|
||||||
{{ (item.stdout | default('')).strip() }}
|
{{ (item.stdout | default('')).strip() }}
|
||||||
STDERR:
|
STDERR:
|
||||||
{{ (item.stderr | default('')).strip() }}
|
{{ (item.stderr | default('')).strip() }}
|
||||||
loop: "{{ kuma_cmds.results }}"
|
loop: "{{ kuma_cmds.results }}"
|
||||||
vars:
|
|
||||||
# Print only the remote "bash -lc" payload (no password leaks)
|
|
||||||
item_short: "{{ (item.cmd | default([]))[-1] | default('N/A') }}"
|
|
||||||
when: kuma_cmds is defined
|
|
||||||
changed_when: false
|
|
||||||
|
|
||||||
- name: Fail play if any Uptime Kuma command failed
|
- name: Fail play if any Uptime Kuma command failed
|
||||||
ansible.builtin.assert:
|
ansible.builtin.assert:
|
||||||
that: "item.rc == 0"
|
that: "item.rc == 0"
|
||||||
fail_msg: "Uptime Kuma update failed on VM: {{ (item.cmd | default([]))[-1] | default('N/A') }} (rc={{ item.rc }})"
|
fail_msg: "Uptime Kuma update failed on VM: {{ item.item }} (rc={{ item.rc }})"
|
||||||
success_msg: "All Uptime Kuma update commands succeeded."
|
success_msg: "All Uptime Kuma update commands succeeded."
|
||||||
loop: "{{ kuma_cmds.results }}"
|
loop: "{{ kuma_cmds.results }}"
|
||||||
|
|
||||||
# -------------------------
|
# -------------------------
|
||||||
# Readiness checks
|
# Readiness checks (controller first, then VM fallback)
|
||||||
# -------------------------
|
# -------------------------
|
||||||
|
|
||||||
- name: Kuma | Wait for web to respond (controller-side, if URL provided)
|
- name: Kuma | Wait for homepage (controller first)
|
||||||
ansible.builtin.uri:
|
ansible.builtin.uri:
|
||||||
url: "{{ (kuma_url | regex_replace('/$','')) + '/' }}"
|
url: "{{ (kuma_url | regex_replace('/$','')) + '/' }}"
|
||||||
method: GET
|
method: GET
|
||||||
return_content: true
|
return_content: true
|
||||||
|
# Validate TLS only when using https://
|
||||||
validate_certs: "{{ (kuma_url | default('')) is match('^https://') }}"
|
validate_certs: "{{ (kuma_url | default('')) is match('^https://') }}"
|
||||||
status_code: 200
|
status_code: 200
|
||||||
register: kuma_controller
|
register: kuma_controller
|
||||||
delegate_to: localhost
|
delegate_to: localhost
|
||||||
run_once: true
|
run_once: true
|
||||||
when: kuma_url is defined and (kuma_url | length) > 0
|
when: kuma_url is defined and (kuma_url | length) > 0
|
||||||
retries: 20
|
retries: 10
|
||||||
delay: 2
|
delay: 2
|
||||||
until: kuma_controller.status == 200 and ('Uptime Kuma' in (kuma_controller.content | default('')))
|
until: kuma_controller.status == 200
|
||||||
failed_when: false
|
failed_when: false
|
||||||
changed_when: false
|
changed_when: false
|
||||||
|
|
||||||
- name: Kuma | VM-side probe (fallback to localhost:port, no auth)
|
- name: Kuma | VM-side fetch (HTML via Python)
|
||||||
ansible.builtin.command:
|
ansible.builtin.command:
|
||||||
argv:
|
argv:
|
||||||
- sshpass
|
- sshpass
|
||||||
@@ -316,52 +119,48 @@
|
|||||||
- -lc
|
- -lc
|
||||||
- |
|
- |
|
||||||
python3 - <<'PY'
|
python3 - <<'PY'
|
||||||
# Simple readiness probe: fetch "/" and look for "Uptime Kuma" in HTML
|
# Fetch Kuma homepage from localhost and print HTML to stdout
|
||||||
import sys, urllib.request
|
import urllib.request, sys
|
||||||
try:
|
try:
|
||||||
with urllib.request.urlopen("http://127.0.0.1:{{ kuma_port }}/", timeout=15) as r:
|
with urllib.request.urlopen("http://127.0.0.1:{{ kuma_port }}/", timeout=15) as r:
|
||||||
body = r.read(8192).decode(errors='ignore')
|
sys.stdout.write(r.read().decode(errors='ignore'))
|
||||||
if 'Uptime Kuma' in body:
|
|
||||||
print("OK")
|
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
PY
|
PY
|
||||||
register: kuma_vm
|
register: kuma_vm
|
||||||
changed_when: false
|
changed_when: false
|
||||||
failed_when: false
|
failed_when: false
|
||||||
when: (kuma_url is not defined) or (kuma_url | length == 0) or (kuma_controller.status | default(0)) != 200
|
when: kuma_controller.status | default(0) != 200 or kuma_controller.content is not defined
|
||||||
|
|
||||||
- name: Kuma | Decide readiness source
|
- name: Kuma | Choose homepage HTML (controller wins, else VM)
|
||||||
ansible.builtin.set_fact:
|
ansible.builtin.set_fact:
|
||||||
kuma_ready: >-
|
kuma_home_html: >-
|
||||||
{{
|
{{
|
||||||
(
|
(kuma_controller.content
|
||||||
(kuma_controller is defined)
|
if (kuma_controller.status|default(0))==200 and (kuma_controller.content is defined)
|
||||||
and (kuma_controller.status|default(0))==200
|
else (
|
||||||
and ('Uptime Kuma' in (kuma_controller.content|default('')))
|
(kuma_vm.stdout | default('') | trim | length > 0)
|
||||||
) or
|
| ternary((kuma_vm.stdout | trim), omit)
|
||||||
(
|
)
|
||||||
(kuma_vm.stdout | default('')) | trim == 'OK'
|
|
||||||
)
|
)
|
||||||
}}
|
}}
|
||||||
kuma_ready_source: >-
|
failed_when: false
|
||||||
{{
|
|
||||||
(
|
|
||||||
(kuma_controller is defined)
|
|
||||||
and (kuma_controller.status|default(0))==200
|
|
||||||
and ('Uptime Kuma' in (kuma_controller.content|default('')))
|
|
||||||
)
|
|
||||||
| ternary('controller', 'vm')
|
|
||||||
}}
|
|
||||||
|
|
||||||
- name: Kuma | Print concise summary
|
- name: Kuma | Print concise summary
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
msg: >-
|
msg: >-
|
||||||
Uptime Kuma is {{ 'READY' if kuma_ready else 'NOT READY' }}
|
Uptime Kuma homepage {{ 'reachable' if (kuma_home_html is defined) else 'NOT reachable' }}.
|
||||||
(checked via {{ kuma_ready_source }}).
|
Source={{ 'controller' if ((kuma_controller.status|default(0))==200 and (kuma_controller.content is defined)) else 'vm' if (kuma_vm.stdout|default('')|trim|length>0) else 'n/a' }};
|
||||||
URL={{ (kuma_url if (kuma_url|default('')|length>0) else 'http://'+vm_ip+':'+(kuma_port|string)) }}
|
length={{ (kuma_home_html | default('')) | length }};
|
||||||
|
contains('Uptime Kuma')={{ (kuma_home_html is defined) and ('Uptime Kuma' in kuma_home_html) }}
|
||||||
|
|
||||||
- name: Kuma | Not ready after retries
|
- name: Kuma | Homepage unavailable (after retries)
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
msg: "Kuma web není dostupná ani po pokusech."
|
msg: "Kuma web není dostupná ani po pokusech."
|
||||||
when: not kuma_ready
|
when: kuma_home_html is not defined
|
||||||
|
|
||||||
|
# Optional detailed dump (short excerpt only)
|
||||||
|
- name: Kuma | HTML excerpt (debug)
|
||||||
|
ansible.builtin.debug:
|
||||||
|
msg: "{{ (kuma_home_html | default(''))[:500] }}"
|
||||||
|
when: kuma_debug_home and (kuma_home_html is defined)
|
||||||
Reference in New Issue
Block a user