# update_homarr.yml - name: Update Homarr 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 }}" # Homarr specifics homarr_project: "homarr" homarr_compose_file: "/data/compose/homarr/docker-compose-homarr.yml" homarr_service: "homarr" homarr_image: "ghcr.io/homarr-labs/homarr:latest" homarr_port: 7575 # Optional external URL for controller-side readiness check (e.g., https://homarr.example.com) # If empty/undefined, controller check is skipped and we only probe from the VM. homarr_url: "{{ lookup('env', 'HOMARR_URL') | default('', true) }}" # Fixed container name used in your compose (avoid conflicts with any leftover container) homarr_container_name: "homarr" # Retry policy (same pattern as Kuma): 25x with 2s delay homarr_retries: "{{ RETRIES }}" homarr_delay: 2 # Docker command prefix (consistent behavior) docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker" # Commands to run on the target VM (quiet outputs) homarr_commands: - "{{ docker_prefix }} pull -q {{ homarr_image }} >/dev/null" - "{{ docker_prefix }} compose -p {{ homarr_project }} -f {{ homarr_compose_file }} pull {{ homarr_service }} >/dev/null" # remove conflicting container name before compose up (silently) - "{{ docker_prefix }} rm -f {{ homarr_container_name }} >/dev/null 2>&1 || true" - "{{ docker_prefix }} compose -p {{ homarr_project }} -f {{ homarr_compose_file }} up -d --no-deps --force-recreate {{ homarr_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 Homarr update commands on VM (via SSH) # use SSHPASS env, hide item label ansible.builtin.command: argv: - sshpass - -e # read password from SSHPASS environment - ssh - -o - StrictHostKeyChecking=no - -o - ConnectTimeout=15 - "{{ vm_user }}@{{ vm_ip }}" - bash - -lc - "{{ ('sudo ' if use_sudo else '') + item }}" environment: SSHPASS: "{{ vm_pass }}" # supply password via environment loop: "{{ homarr_commands }}" loop_control: index_var: idx # capture loop index label: "cmd-{{ idx }}" # avoid printing full command in (item=...) line register: homarr_cmds changed_when: false no_log: "{{ DEBUG == 0 }}" # hide outputs and env when not debugging - name: Show outputs for each Homarr command ansible.builtin.debug: msg: | CMD: {{ item.item }} RC: {{ item.rc }} STDOUT: {{ (item.stdout | default('')).strip() }} STDERR: {{ (item.stderr | default('')).strip() }} loop: "{{ homarr_cmds.results }}" when: DEBUG == 1 - name: Fail play if any Homarr command failed # also hide item label ansible.builtin.assert: that: "item.rc == 0" fail_msg: "Homarr update failed on VM: {{ item.item }} (rc={{ item.rc }})" success_msg: "All Homarr update commands succeeded." loop: "{{ homarr_cmds.results }}" loop_control: index_var: idx label: "cmd-{{ idx }}" # ------------------------- # Readiness checks (controller first, then VM fallback) # ------------------------- - name: Homarr | Wait for homepage (controller first, with retries) ansible.builtin.uri: url: "{{ (homarr_url | regex_replace('/$','')) + '/' }}" method: GET return_content: true # Validate TLS only when using https:// validate_certs: "{{ (homarr_url | default('')) is match('^https://') }}" status_code: 200 register: homarr_controller delegate_to: localhost run_once: true when: homarr_url is defined and (homarr_url | length) > 0 retries: "{{ homarr_retries }}" delay: "{{ homarr_delay }}" until: homarr_controller.status == 200 failed_when: false changed_when: false - name: Homarr | VM-side fetch (HTML via Python, with retries) # use SSHPASS env here too ansible.builtin.command: argv: - sshpass - -e - ssh - -o - StrictHostKeyChecking=no - -o - ConnectTimeout=15 - "{{ vm_user }}@{{ vm_ip }}" - bash - -lc - | python3 - <<'PY' # Fetch Homarr homepage from localhost and print HTML to stdout import urllib.request, sys try: with urllib.request.urlopen("http://127.0.0.1:{{ homarr_port }}/", timeout=15) as r: sys.stdout.write(r.read().decode(errors='ignore')) except Exception: pass PY environment: SSHPASS: "{{ vm_pass }}" register: homarr_vm changed_when: false failed_when: false when: homarr_controller.status | default(0) != 200 or homarr_controller.content is not defined retries: "{{ homarr_retries }}" delay: "{{ homarr_delay }}" until: (homarr_vm.stdout | default('') | trim | length) > 0 and ('Homarr' in (homarr_vm.stdout | default(''))) no_log: "{{ DEBUG == 0 }}" - name: Homarr | Choose homepage HTML (controller wins, else VM) # safe guard against empty result ansible.builtin.set_fact: homarr_home_html: >- {{ ( homarr_controller.content if (homarr_controller is defined) and ((homarr_controller.status|default(0))==200) and (homarr_controller.content is defined) else (homarr_vm.stdout | default('') | trim) ) }} when: - (homarr_controller is defined and (homarr_controller.status|default(0))==200 and (homarr_controller.content is defined)) or ((homarr_vm.stdout | default('') | trim | length) > 0) - name: Homarr | Print concise summary ansible.builtin.debug: msg: >- Homarr homepage {{ 'reachable' if (homarr_home_html is defined) else 'NOT reachable' }}. Source={{ 'controller' if ((homarr_controller is defined) and (homarr_controller.status|default(0))==200 and (homarr_controller.content is defined)) else 'vm' if (homarr_vm.stdout|default('')|trim|length>0) else 'n/a' }}; length={{ (homarr_home_html | default('')) | length }}; contains('Homarr')={{ (homarr_home_html is defined) and ('Homarr' in homarr_home_html) }} when: DEBUG == 1 - name: Homarr | Homepage unavailable (after retries) ansible.builtin.debug: msg: "Homarr web nenĂ­ dostupnĂ˝ ani po pokusech." when: homarr_home_html is not defined and DEBUG == 1 # Optional detailed dump (short excerpt only) - name: Homarr | HTML excerpt (debug) ansible.builtin.debug: msg: "{{ (homarr_home_html | default(''))[:500] }}" when: homarr_home_html is defined and DEBUG == 1