---
# AWOOOI Ansible — 192.168.0.188 AI+Web 中心完整狀態描述
# 執行: ansible-playbook -i inventory/hosts.yml playbooks/188-ai-web.yml
#
# 預期狀態:
#   openclaw:       running (port 8088)
#   tsenyang:       running (port 3000)
#   momo:           running (port 5003)
#   signoz:         running (port 3301)
#   minio:          running (port 9000)
#   litellm:        running (port 4000)
#   n8n:            running (port 5678)
#   open-webui:     running (port 3010)
#   docker-registry: running (port 5001)
#   nginx:          all-sites.conf 無 gitlab block
#   keepalived:     MASTER priority 100 VIP:200

- name: "192.168.0.188 AI+Web 中心 — 預期狀態收斂"
  hosts: host_188
  become: true
  vars:
    ansible_become_pass: "{{ vault_sudo_password | default(omit) }}"

  tasks:
    # ========================================================================
    # Docker 服務健康檢查
    # ========================================================================
    - name: "Docker | 收集運作中的容器清單"
      ansible.builtin.command:
        cmd: "docker ps --format '{{ '{{' }}.Names{{ '}}' }}'"
      register: running_containers
      changed_when: false
      tags: docker

    - name: "Docker | 確認關鍵服務運作中"
      ansible.builtin.debug:
        msg: "⚠️  服務 {{ item }} 未在容器列表中，請確認"
      when: item not in running_containers.stdout
      loop:
        - openclaw
        - tsenyang
        - momo
        - signoz
        - minio
        - litellm
      tags: docker

    # ========================================================================
    # n8n / open-webui (Sprint A 新啟動)
    # ========================================================================
    - name: "n8n | 確認容器運作中"
      ansible.builtin.command:
        cmd: "docker ps --filter name=n8n --filter status=running --format '{{ '{{' }}.Names{{ '}}' }}'"
      register: n8n_status
      changed_when: false
      tags: n8n

    - name: "n8n | 若停止則啟動"
      ansible.builtin.command:
        cmd: "docker compose up -d"
        chdir: /opt/n8n
      when: n8n_status.stdout == ""
      tags: n8n

    - name: "open-webui | 確認容器運作中"
      ansible.builtin.command:
        cmd: "docker ps --filter name=open-webui --filter status=running --format '{{ '{{' }}.Names{{ '}}' }}'"
      register: openwebui_status
      changed_when: false
      tags: open_webui

    - name: "open-webui | 若停止則啟動"
      ansible.builtin.command:
        cmd: "docker compose up -d"
        chdir: /opt/open-webui
      when: openwebui_status.stdout == ""
      tags: open_webui

    # ========================================================================
    # Nginx 狀態確認
    # ========================================================================
    - name: "nginx | 確認服務運作中"
      ansible.builtin.systemd:
        name: nginx
      register: nginx_status
      tags: nginx

    - name: "nginx | 警告：nginx 未運作"
      ansible.builtin.debug:
        msg: "🚨 nginx 未運作！"
      when: nginx_status.status.ActiveState != "active"
      tags: nginx

    - name: "nginx | 確認 all-sites.conf 無 gitlab block"
      ansible.builtin.command:
        cmd: "grep -c 'gitlab' /etc/nginx/sites-enabled/all-sites.conf"
      register: gitlab_check
      changed_when: false
      failed_when: false
      tags: nginx

    - name: "nginx | 警告：all-sites.conf 仍含 gitlab block"
      ansible.builtin.debug:
        msg: "⚠️  all-sites.conf 仍含 gitlab 設定，請確認 Sprint A 清除是否完整"
      when: gitlab_check.stdout != "0"
      tags: nginx

    # ========================================================================
    # keepalived MASTER
    # ========================================================================
    - name: "keepalived | 確認服務運作中"
      ansible.builtin.systemd:
        name: keepalived
      register: keepalived_status
      tags: keepalived

    - name: "keepalived | 警告：keepalived 未運作"
      ansible.builtin.debug:
        msg: "⚠️  keepalived MASTER 未運作，VIP:200 降級為 110 BACKUP"
      when: keepalived_status.status.ActiveState != "active"
      tags: keepalived

    - name: "keepalived | 確認 VIP:200 由本機持有"
      ansible.builtin.command:
        cmd: "ip addr show | grep 192.168.0.200"
      register: vip_check
      changed_when: false
      failed_when: false
      tags: keepalived

    - name: "keepalived | 警告：VIP:200 不在本機"
      ansible.builtin.debug:
        msg: "⚠️  VIP 192.168.0.200 不在 188 (MASTER 可能已 failover 到 110)"
      when: vip_check.rc != 0
      tags: keepalived
