# update_semaphore.yml - name: Update Semaphore on VM via Proxmox hosts: linux_servers 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') }}" # IP vm-portainer vm_user: "{{ lookup('env', 'VM_USER') }}" vm_pass: "{{ lookup('env', 'VM_PASS') }}" use_sudo: false # --- Debug mode (controlled via Semaphore variable) --- DEBUG: "{{ lookup('env', 'DEBUG') | default(0) | int }}" # --- Semaphore specifics --- semaphore_project: "semaphore" semaphore_compose_file: "/data/compose/semaphore/docker-compose.yml" semaphore_service: "semaphore" tasks: - name: Ensure sshpass is installed (for password-based SSH) # English comments ansible.builtin.apt: name: sshpass state: present update_cache: yes - name: Run Semaphore self-update on VM in background (nohup + log) ansible.builtin.command: argv: - sshpass - -e - ssh - -o - StrictHostKeyChecking=no - -o - ConnectTimeout=15 - "{{ vm_user }}@{{ vm_ip }}" - bash - -lc - | set -euo pipefail # English comments: create unique run id and log paths RID="$(date +%s)" LOG="/tmp/semaphore-self-update-${RID}.log" PIDF="/tmp/semaphore-self-update-${RID}.pid" RCF="/tmp/semaphore-self-update-${RID}.rc" # English comments: build docker command (optionally via sudo -n) DOCKER="docker" if [ "${USE_SUDO:-0}" = "1" ]; then DOCKER="sudo -n docker" fi # English comments: start background worker, delay to let Semaphore job finish nohup bash -lc " set -euo pipefail unalias docker 2>/dev/null || true export DOCKER_CLI_HINTS=0 echo \"=== started: \$(date -Is) ===\" echo \"host: \$(hostname)\" echo \"compose: {{ semaphore_compose_file }}\" echo \"project: {{ semaphore_project }} service: {{ semaphore_service }}\" echo \"USE_SUDO=\${USE_SUDO:-0}\" echo sleep 15 # English comments: pull explicitly (more compatible than --pull always) $DOCKER compose -p {{ semaphore_project }} -f {{ semaphore_compose_file }} pull {{ semaphore_service }} # English comments: recreate service $DOCKER compose -p {{ semaphore_project }} -f {{ semaphore_compose_file }} up -d --no-deps --force-recreate {{ semaphore_service }} echo echo \"=== finished: \$(date -Is) ===\" " >\"$LOG\" 2>&1 \"$PIDF\" echo \"scheduled: pid=\$(cat \"$PIDF\"), log=$LOG\" environment: SSHPASS: "{{ vm_pass }}" # Set USE_SUDO=1 in Semaphore env vars if vm_user needs sudo for docker USE_SUDO: "{{ lookup('env', 'USE_SUDO') | default('0') }}" register: semaphore_update changed_when: false no_log: "{{ DEBUG == 0 }}" - name: Show result of Semaphore self-update (debug) ansible.builtin.debug: msg: | RC: {{ semaphore_update.rc }} STDOUT: {{ (semaphore_update.stdout | default('')).strip() }} STDERR: {{ (semaphore_update.stderr | default('')).strip() }} when: DEBUG == 1