homelab/ansible/archive/playbooks/ai/validate_karakeep.yml

153 lines
5.2 KiB
YAML

---
- name: Validate Ollama service and models
hosts: ai_grid
gather_facts: true
tags: [ollama, models]
vars:
ollama_base_url: "http://{{ ansible_host }}:11434"
required_models:
- name: "llama3.1:8b"
type: "text"
- name: "llama3.2-vision:11b"
type: "vision"
tasks:
- name: Check Ollama service is responding
ansible.builtin.uri:
url: "{{ ollama_base_url }}/api/tags"
method: GET
return_content: true
status_code: 200
register: ollama_response
changed_when: false
- name: Parse available models
ansible.builtin.set_fact:
available_models: "{{ ollama_response.json.models | map(attribute='name') | list }}"
- name: Display available models
ansible.builtin.debug:
msg: "Available models: {{ available_models }}"
- name: Verify required models are installed
ansible.builtin.assert:
that:
- item.name in available_models
fail_msg: "Required model {{ item.name }} ({{ item.type }}) is not installed"
success_msg: "Model {{ item.name }} ({{ item.type }}) is available"
loop: "{{ required_models }}"
loop_control:
label: "{{ item.name }}"
- name: Test text model inference
ansible.builtin.uri:
url: "{{ ollama_base_url }}/api/generate"
method: POST
body_format: json
body:
model: "llama3.1:8b"
prompt: "Hello"
stream: false
return_content: true
status_code: 200
timeout: 30
register: text_inference_test
changed_when: false
- name: Verify text model response
ansible.builtin.assert:
that:
- text_inference_test.json.response is defined
- text_inference_test.json.response | length > 0
success_msg: "Text model inference successful"
fail_msg: "Text model inference failed"
- name: Show Ollama validation summary
ansible.builtin.debug:
msg:
- "Ollama validation passed"
- "Host: {{ inventory_hostname }} ({{ ansible_host }})"
- "Models available: {{ available_models | length }}"
- "Text inference: Working"
- name: Validate legacy Karakeep integration
hosts: localhost
gather_facts: false
tags: [karakeep, integration, legacy]
vars:
test_legacy_karakeep: "{{ test_legacy_karakeep | default(false) }}"
container_name: "hoarder-web"
ollama_host: "10.0.0.220"
ollama_port: 11434
legacy_host: "{{ legacy_host | default('10.0.0.251') }}"
tasks:
- name: Skip legacy validation when disabled
ansible.builtin.meta: end_play
when: not (test_legacy_karakeep | bool)
- name: Check whether Karakeep container is running
community.docker.docker_container_info:
name: "{{ container_name }}"
delegate_to: "{{ legacy_host }}"
vars:
ansible_user: chester
ansible_ssh_private_key_file: /home/chester/.ssh/id_ed25519
register: karakeep_container
- name: Verify Karakeep container status
ansible.builtin.assert:
that:
- karakeep_container.exists
- karakeep_container.container.State.Running
- karakeep_container.container.State.Health.Status == "healthy"
fail_msg: "Karakeep container is not running or unhealthy"
success_msg: "Karakeep container is running and healthy"
- name: Extract Ollama environment values
ansible.builtin.set_fact:
ollama_config: "{{ karakeep_container.container.Config.Env | select('match', '^(OLLAMA|INFERENCE).*') | list }}"
- name: Verify Karakeep Ollama environment variables
ansible.builtin.assert:
that:
- "'OLLAMA_BASE_URL=http://' + ollama_host + ':' + (ollama_port | string) in ollama_config"
- "'INFERENCE_TEXT_MODEL=llama3.1:8b' in ollama_config"
- "'INFERENCE_IMAGE_MODEL=llama3.2-vision:11b' in ollama_config"
fail_msg: "Ollama environment variables are incorrect"
success_msg: "Ollama environment variables are correctly configured"
- name: Test Ollama connectivity from Karakeep container
community.docker.docker_container_exec:
container: "{{ container_name }}"
command: "/bin/sh -c 'wget -qO- http://{{ ollama_host }}:{{ ollama_port }}/api/tags'"
delegate_to: "{{ legacy_host }}"
vars:
ansible_user: chester
ansible_ssh_private_key_file: /home/chester/.ssh/id_ed25519
register: container_connectivity
changed_when: false
failed_when: container_connectivity.rc != 0
- name: Verify container can reach Ollama API
ansible.builtin.assert:
that:
- "'models' in container_connectivity.stdout"
success_msg: "Karakeep container can reach Ollama API"
fail_msg: "Karakeep container cannot reach Ollama API"
- name: Display integration test summary
hosts: localhost
gather_facts: false
tags: [summary]
tasks:
- name: Show final validation report
ansible.builtin.debug:
msg:
- "Service validation complete"
- "Ollama endpoint: http://10.0.0.220:11434"
- "Models: llama3.1:8b, llama3.2-vision:11b"
- "Legacy Karakeep tested: {{ test_legacy_karakeep | default(false) }}"