--- # Ensure Gitea is Discovered by Traefik # This playbook ensures that Traefik properly discovers Gitea after restarts - name: Ensure Gitea is Discovered by Traefik hosts: production gather_facts: no become: no vars: traefik_stack_path: "{{ stacks_base_path }}/traefik" gitea_stack_path: "{{ stacks_base_path }}/gitea" max_wait_seconds: 60 check_interval: 5 tasks: - name: Check if Gitea container is running ansible.builtin.shell: | cd {{ gitea_stack_path }} docker compose ps gitea | grep -q "Up" && echo "RUNNING" || echo "NOT_RUNNING" register: gitea_status changed_when: false - name: Start Gitea if not running ansible.builtin.shell: | cd {{ gitea_stack_path }} docker compose up -d gitea when: gitea_status.stdout == "NOT_RUNNING" register: gitea_start - name: Wait for Gitea to be ready ansible.builtin.wait_for: timeout: 30 delay: 2 when: gitea_start.changed | default(false) | bool - name: Check if Traefik can see Gitea container ansible.builtin.shell: | cd {{ traefik_stack_path }} docker compose exec -T traefik sh -c 'wget -qO- http://localhost:8080/api/http/routers 2>&1 | python3 -m json.tool 2>&1 | grep -qi gitea && echo "FOUND" || echo "NOT_FOUND"' register: traefik_gitea_check changed_when: false failed_when: false retries: "{{ (max_wait_seconds | int) // (check_interval | int) }}" delay: "{{ check_interval }}" until: traefik_gitea_check.stdout == "FOUND" - name: Restart Traefik if Gitea not found ansible.builtin.shell: | cd {{ traefik_stack_path }} docker compose restart traefik when: traefik_gitea_check.stdout == "NOT_FOUND" register: traefik_restart - name: Wait for Traefik to be ready after restart ansible.builtin.wait_for: timeout: 30 delay: 2 when: traefik_restart.changed | default(false) | bool - name: Verify Gitea is reachable via Traefik ansible.builtin.uri: url: "https://{{ gitea_domain }}/api/healthz" method: GET status_code: [200] validate_certs: false timeout: 10 register: gitea_health_check retries: 5 delay: 2 until: gitea_health_check.status == 200 failed_when: false - name: Display result ansible.builtin.debug: msg: | ================================================================================ GITEA TRAEFIK DISCOVERY - RESULT ================================================================================ Gitea Status: {{ gitea_status.stdout }} Traefik Discovery: {{ traefik_gitea_check.stdout }} Gitea Health Check: {{ 'OK' if (gitea_health_check.status | default(0) == 200) else 'FAILED' }} {% if gitea_health_check.status | default(0) == 200 %} ✅ Gitea is reachable via Traefik {% else %} ❌ Gitea is not reachable via Traefik {% endif %} ================================================================================