forked from jakub/ansible
174 lines
6.1 KiB
YAML
174 lines
6.1 KiB
YAML
- name: Update Uptime Kuma on VM via Proxmox
|
|
hosts: proxmox
|
|
gather_facts: false
|
|
become: true
|
|
become_user: root
|
|
become_method: sudo
|
|
|
|
vars:
|
|
# --- Connection to VM (provided by Semaphore env vars) ---
|
|
vm_ip: "{{ lookup('env', 'VM_IP') }}"
|
|
vm_user: "{{ lookup('env', 'VM_USER') }}"
|
|
vm_pass: "{{ lookup('env', 'VM_PASS') }}"
|
|
use_sudo: false
|
|
|
|
# --- Uptime Kuma specifics ---
|
|
kuma_project: "uptime-kuma" # docker compose project name
|
|
kuma_compose_file: "/data/compose/kuma/uptime-kuma.yml" # adjust to your path
|
|
kuma_service: "uptime-kuma" # service name from compose
|
|
kuma_port: 3001
|
|
|
|
# Optional external URL for controller-side readiness check.
|
|
# If empty/undefined, controller check is skipped and we only probe from the VM.
|
|
kuma_url: "{{ lookup('env', 'KUMA_URL') | default('', true) }}"
|
|
|
|
# Docker command prefix (keeps behavior consistent and quiet)
|
|
docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker"
|
|
|
|
# Commands to run on the target VM
|
|
kuma_commands:
|
|
- "{{ docker_prefix }} pull -q louislam/uptime-kuma:latest >/dev/null"
|
|
- "{{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }} pull {{ kuma_service }} >/dev/null"
|
|
- "{{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }} up -d --no-deps --force-recreate {{ kuma_service }} >/dev/null"
|
|
|
|
tasks:
|
|
- name: Ensure sshpass is installed (for password-based SSH) # English comments
|
|
ansible.builtin.apt:
|
|
name: sshpass
|
|
state: present
|
|
update_cache: yes
|
|
|
|
- name: Run Uptime Kuma update commands on VM (via SSH) # Avoid leaking password in logs
|
|
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 '') + item }}"
|
|
loop: "{{ kuma_commands }}"
|
|
register: kuma_cmds
|
|
changed_when: false
|
|
no_log: true # <- hides password and raw argv from logs
|
|
|
|
- name: Show summarized outputs for each command (sanitized)
|
|
ansible.builtin.debug:
|
|
msg: |
|
|
CMD: {{ item_short }}
|
|
RC: {{ item.rc }}
|
|
STDOUT:
|
|
{{ (item.stdout | default('')).strip() }}
|
|
STDERR:
|
|
{{ (item.stderr | default('')).strip() }}
|
|
loop: "{{ kuma_cmds.results }}"
|
|
vars:
|
|
# Print a redacted/short form of the command so we don't leak the password
|
|
item_short: >-
|
|
{{
|
|
(item.cmd | map('quote') | list)[-1] # last element is the remote "bash -lc" payload
|
|
}}
|
|
when: kuma_cmds is defined
|
|
|
|
- name: Fail play if any Uptime Kuma command failed
|
|
ansible.builtin.assert:
|
|
that: "item.rc == 0"
|
|
fail_msg: "Uptime Kuma update failed on VM: {{ (item.cmd | last) }} (rc={{ item.rc }})"
|
|
success_msg: "All Uptime Kuma update commands succeeded."
|
|
loop: "{{ kuma_cmds.results }}"
|
|
|
|
# -------------------------
|
|
# Readiness checks
|
|
# -------------------------
|
|
|
|
- name: Kuma | Wait for web to respond (controller-side, if URL provided)
|
|
ansible.builtin.uri:
|
|
url: "{{ (kuma_url | regex_replace('/$','')) + '/' }}"
|
|
method: GET
|
|
return_content: true
|
|
# Validate TLS only when using https://
|
|
validate_certs: "{{ (kuma_url | default('')) is match('^https://') }}"
|
|
status_code: 200
|
|
register: kuma_controller
|
|
delegate_to: localhost
|
|
run_once: true
|
|
when: kuma_url is defined and (kuma_url | length) > 0
|
|
retries: 20
|
|
delay: 2
|
|
until: kuma_controller.status == 200 and ('Uptime Kuma' in (kuma_controller.content | default('')))
|
|
failed_when: false
|
|
changed_when: false
|
|
|
|
- name: Kuma | VM-side probe (fallback to localhost:port, no auth)
|
|
ansible.builtin.command:
|
|
argv:
|
|
- sshpass
|
|
- -p
|
|
- "{{ vm_pass }}"
|
|
- ssh
|
|
- -o
|
|
- StrictHostKeyChecking=no
|
|
- -o
|
|
- ConnectTimeout=15
|
|
- "{{ vm_user }}@{{ vm_ip }}"
|
|
- bash
|
|
- -lc
|
|
- |
|
|
python3 - <<'PY'
|
|
# Simple readiness probe: fetch "/" and look for "Uptime Kuma" in HTML
|
|
import sys, urllib.request
|
|
try:
|
|
with urllib.request.urlopen("http://127.0.0.1:{{ kuma_port }}/", timeout=15) as r:
|
|
body = r.read(8192).decode(errors='ignore')
|
|
if 'Uptime Kuma' in body:
|
|
print("OK")
|
|
except Exception:
|
|
pass
|
|
PY
|
|
register: kuma_vm
|
|
changed_when: false
|
|
failed_when: false
|
|
# Run only if controller check missing or didn't succeed
|
|
when: (kuma_url is not defined) or (kuma_url | length == 0) or (kuma_controller.status | default(0)) != 200
|
|
|
|
- name: Kuma | Decide readiness source
|
|
ansible.builtin.set_fact:
|
|
kuma_ready: >-
|
|
{{
|
|
(
|
|
(kuma_controller is defined)
|
|
and (kuma_controller.status|default(0))==200
|
|
and ('Uptime Kuma' in (kuma_controller.content|default('')))
|
|
) or
|
|
(
|
|
(kuma_vm.stdout | default('')) | trim == 'OK'
|
|
)
|
|
}}
|
|
kuma_ready_source: >-
|
|
{{
|
|
(
|
|
(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
|
|
ansible.builtin.debug:
|
|
msg: >-
|
|
Uptime Kuma is {{ 'READY' if kuma_ready else 'NOT READY' }}
|
|
(checked via {{ kuma_ready_source }}).
|
|
URL={{ (kuma_url if (kuma_url|default('')|length>0) else 'http://'+vm_ip+':'+(kuma_port|string)) }}
|
|
|
|
- name: Kuma | Not ready after retries
|
|
ansible.builtin.debug:
|
|
msg: "Kuma web není dostupná ani po pokusech."
|
|
when: not kuma_ready
|