# update_immich.yml - name: Update Immich on VM via Proxmox hosts: linux_servers gather_facts: false become: true become_user: root become_method: sudo vars: # VM connection (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 # --- Debug mode (controlled via Semaphore variable) --- DEBUG: "{{ lookup('env', 'DEBUG') | default(0) | int }}" RETRIES: "{{ lookup('env', 'RETRIES') | default(25) | int }}" # Immich specifics immich_dir: "/opt/immich" immich_project: "immich" immich_compose_url: "https://github.com/immich-app/immich/releases/latest/download/docker-compose.yml" immich_compose_file: "/opt/immich/docker-compose.yml" immich_override_file: "/opt/immich/docker-compose.override.yml" immich_port: 2283 # Optional external URL for controller-side readiness check (e.g., https://photos.example.com) immich_url: "{{ lookup('env', 'IMMICH_URL') | default('', true) }}" # Retry policy immich_retries: "{{ RETRIES }}" immich_delay: 2 # Docker command prefix (consistent behavior) docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker" # Compose command (always include override to keep local mounts separate from upstream compose) immich_compose_cmd: >- {{ docker_prefix }} compose -p {{ immich_project }} -f {{ immich_compose_file }} -f {{ immich_override_file }} # Commands to run on the target VM (quiet outputs) immich_commands: - "cd {{ immich_dir }} && test -f .env" - | cd {{ immich_dir }} mkdir -p backups if [ -f docker-compose.yml ]; then cp -a docker-compose.yml "backups/docker-compose.yml.$(date +%F_%H%M%S).bak" fi - | cd {{ immich_dir }} # Download latest compose from Immich releases (requires curl or wget) if command -v curl >/dev/null 2>&1; then curl -fsSL -o docker-compose.yml "{{ immich_compose_url }}" elif command -v wget >/dev/null 2>&1; then wget -qO docker-compose.yml "{{ immich_compose_url }}" else echo "Neither curl nor wget is available on the VM." exit 1 fi - | cd {{ immich_dir }} # Create override file if missing (keep local mounts here so upstream compose can be replaced safely) python3 - <<'PY' # Write docker-compose.override.yml only if it doesn't exist from pathlib import Path p = Path("docker-compose.override.yml") if p.exists(): raise SystemExit(0) content = """\ # Local overrides for Immich (kept separate from upstream docker-compose.yml). # If you want read-only mounts, append ':ro' to the right side. services: immich-server: volumes: - /mnt/nextcloud-howard-photos:/mnt/nextcloud-howard-photos - /mnt/nextcloud-kamilkaprdelka-photos:/mnt/nextcloud-kamilkaprdelka-photos """ p.write_text(content, encoding="utf-8") PY - | cd {{ immich_dir }} # Docker Engine <25 may not support healthcheck.start_interval; comment it out if present server_major="$({{ docker_prefix }} version --format '{{ "{{.Server.Version}}" }}' 2>/dev/null | cut -d. -f1 || echo 0)" if [ "${server_major:-0}" -lt 25 ]; then sed -i -E 's/^([[:space:]]*)start_interval:/\1# start_interval:/' docker-compose.yml || true fi - "cd {{ immich_dir }} && {{ immich_compose_cmd }} config >/dev/null" - "cd {{ immich_dir }} && {{ immich_compose_cmd }} pull >/dev/null" - "cd {{ immich_dir }} && {{ immich_compose_cmd }} up -d --remove-orphans >/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 Immich update commands on VM (via SSH) # use SSHPASS env, hide item label ansible.builtin.command: argv: - sshpass - -e - ssh - -o - StrictHostKeyChecking=no - -o - ConnectTimeout=15 - "{{ vm_user }}@{{ vm_ip }}" - bash - -lc - "{{ ('sudo ' if use_sudo else '') + item }}" environment: SSHPASS: "{{ vm_pass }}" loop: "{{ immich_commands }}" loop_control: index_var: idx label: "cmd-{{ idx }}" register: immich_cmds changed_when: false no_log: "{{ DEBUG == 0 }}" - name: Show outputs for each Immich command ansible.builtin.debug: msg: | CMD: {{ item.item }} RC: {{ item.rc }} STDOUT: {{ (item.stdout | default('')).strip() }} STDERR: {{ (item.stderr | default('')).strip() }} loop: "{{ immich_cmds.results }}" when: DEBUG == 1 - name: Fail play if any Immich command failed ansible.builtin.assert: that: "item.rc == 0" fail_msg: "Immich update failed on VM: {{ item.item }} (rc={{ item.rc }})" success_msg: "All Immich update commands succeeded." loop: "{{ immich_cmds.results }}" loop_control: index_var: idx label: "cmd-{{ idx }}" # ------------------------- # Readiness checks (controller first, then VM fallback) # ------------------------- - name: Immich | Wait for API ping (controller first, with retries) ansible.builtin.uri: url: "{{ (immich_url | regex_replace('/$','')) + '/api/server/ping' }}" method: GET return_content: true validate_certs: "{{ (immich_url | default('')) is match('^https://') }}" status_code: 200 register: immich_controller delegate_to: localhost run_once: true when: immich_url is defined and (immich_url | length) > 0 retries: "{{ immich_retries }}" delay: "{{ immich_delay }}" until: immich_controller.status == 200 and ('pong' in (immich_controller.content | default(''))) failed_when: false changed_when: false - name: Immich | VM-side ping (JSON via Python, with retries) ansible.builtin.command: argv: - sshpass - -e - ssh - -o - StrictHostKeyChecking=no - -o - ConnectTimeout=15 - "{{ vm_user }}@{{ vm_ip }}" - bash - -lc - | python3 - <<'PY' # Ping Immich API from localhost and print response to stdout import urllib.request, sys try: with urllib.request.urlopen("http://127.0.0.1:{{ immich_port }}/api/server/ping", timeout=15) as r: sys.stdout.write(r.read().decode(errors='ignore')) except Exception: pass PY environment: SSHPASS: "{{ vm_pass }}" register: immich_vm changed_when: false failed_when: false when: immich_controller.status | default(0) != 200 retries: "{{ immich_retries }}" delay: "{{ immich_delay }}" until: (immich_vm.stdout | default('') | trim | length) > 0 and ('pong' in (immich_vm.stdout | default(''))) no_log: "{{ DEBUG == 0 }}" - name: Immich | Print concise summary ansible.builtin.debug: msg: >- Immich API ping {{ 'OK' if (('pong' in (immich_controller.content|default(''))) or ('pong' in (immich_vm.stdout|default('')))) else 'NOT OK' }}. Source={{ 'controller' if (immich_controller.status|default(0))==200 else 'vm' if (immich_vm.stdout|default('')|trim|length>0) else 'n/a' }}. when: DEBUG == 1