semaphore/playbooks/update-homarr.yml

146 lines
5.4 KiB
YAML

---
# ============================================================================
# update-homarr.yml — pull latest Homarr image + recreate the running stack
# ============================================================================
# Lightweight, routine updater. Does NOT rewrite docker-compose.yml and does NOT
# fetch secrets — it operates on the compose stack already deployed by
# deploy-homarr.yml, so any manual env/compose edits on the host are preserved.
#
# For a first-time / full (re)deploy use deploy-homarr.yml instead.
#
# Connects AS the `ansible` user via the deploy SSH key (same pattern as
# deploy-homarr.yml). Semaphore injects SSH_PRIVATE_KEY from Infisical.
#
# Usage: playbook playbooks/update-homarr.yml, extra vars: target=racknerd1
# ============================================================================
- name: "PRECHECK — fetch deploy SSH key from Infisical"
hosts: localhost
gather_facts: false
vars:
infisical_url: "{{ lookup('env','INFISICAL_URL') | default('http://192.168.122.41:8080', true) }}"
infisical_env_slug: "{{ lookup('env','INFISICAL_ENV_SLUG') | default('dev', true) }}"
tasks:
- name: "[precheck] Infisical auth vars present?"
ansible.builtin.assert:
that:
- lookup('env','INFISICAL_CLIENT_ID') | length > 0
- lookup('env','INFISICAL_CLIENT_SECRET') | length > 0
- lookup('env','INFISICAL_PROJECT_ID') | length > 0
fail_msg: "Missing INFISICAL_CLIENT_ID / _SECRET / _PROJECT_ID in Semaphore Environment."
- name: "[precheck] Login to Infisical"
infisical.vault.login:
url: "{{ infisical_url }}"
auth_method: universal_auth
universal_auth_client_id: "{{ lookup('env','INFISICAL_CLIENT_ID') }}"
universal_auth_client_secret: "{{ lookup('env','INFISICAL_CLIENT_SECRET') }}"
register: infisical_login
- name: "[precheck] Read secrets"
infisical.vault.read_secrets:
login_data: "{{ infisical_login.login_data }}"
project_id: "{{ lookup('env','INFISICAL_PROJECT_ID') }}"
env_slug: "{{ infisical_env_slug }}"
path: "/"
as_dict: true
register: infisical_secrets
- name: "[precheck] stash SSH key for update play"
ansible.builtin.set_fact:
pc_ssh_privkey: "{{ infisical_secrets.secrets.SSH_PRIVATE_KEY }}"
no_log: true
- name: "[precheck] write deploy private key to runner temp (0600)"
ansible.builtin.copy:
dest: "/tmp/homarr_deploy_key"
content: "{{ pc_ssh_privkey }}\n"
mode: "0600"
no_log: true
- name: "Update Homarr (pull + recreate, as ansible user)"
hosts: "{{ target | default('racknerd1') }}"
gather_facts: false
vars:
deploy_user: ansible
homarr_dir: "/home/{{ deploy_user }}/docker/homarr"
homarr_port: 7575
ansible_user: ansible
ansible_ssh_private_key_file: "/tmp/homarr_deploy_key"
ansible_ssh_common_args: "-o StrictHostKeyChecking=accept-new -o IdentitiesOnly=yes"
tasks:
- name: Reset connection so docker group membership is active in this session
ansible.builtin.meta: reset_connection
- name: Gather facts (after connection reset)
ansible.builtin.setup:
- name: Docker available (rootless, via docker group)?
ansible.builtin.command: docker version
register: dv
changed_when: false
failed_when: dv.rc != 0
- name: Existing compose stack present?
ansible.builtin.stat:
path: "{{ homarr_dir }}/docker-compose.yml"
register: compose_file
- name: Fail early if stack not deployed
ansible.builtin.assert:
that:
- compose_file.stat.exists
fail_msg: >-
No docker-compose.yml at {{ homarr_dir }} — run deploy-homarr.yml first.
update-homarr.yml only updates an already-deployed stack.
- name: Record image digest before update
ansible.builtin.command:
cmd: docker inspect --format '{{ '{{' }}.Image{{ '}}' }}' homarr
register: img_before
changed_when: false
failed_when: false
- name: Pull latest image + recreate (compose preserved as-is)
community.docker.docker_compose_v2:
project_src: "{{ homarr_dir }}"
pull: always
state: present
recreate: auto
- name: Record image digest after update
ansible.builtin.command:
cmd: docker inspect --format '{{ '{{' }}.Image{{ '}}' }}' homarr
register: img_after
changed_when: false
- name: Wait for Homarr to be healthy
ansible.builtin.uri:
url: "http://127.0.0.1:{{ homarr_port }}/"
status_code: [200, 302, 307]
register: health
retries: 15
delay: 5
until: health.status is defined and health.status in [200, 302, 307]
- name: Prune the now-dangling old image (best effort)
ansible.builtin.command: docker image prune -f
register: prune
changed_when: "'Total reclaimed space: 0B' not in prune.stdout"
failed_when: false
- name: Result
ansible.builtin.debug:
msg: >-
Homarr on {{ inventory_hostname }}:{{ homarr_port }} — HTTP {{ health.status }}.
{{ 'Image updated.' if img_before.stdout != img_after.stdout else 'Already up to date (no image change).' }}
- name: "Cleanup runner temp key"
hosts: localhost
gather_facts: false
tasks:
- name: "remove temp deploy key from runner"
ansible.builtin.file:
path: /tmp/homarr_deploy_key
state: absent