llm-inference-multimodel: role + day1 playbook (phase 0 discover approved)
This commit is contained in:
87
ansible/roles/llm-inference-multimodel/tasks/discover.yml
Normal file
87
ansible/roles/llm-inference-multimodel/tasks/discover.yml
Normal file
@@ -0,0 +1,87 @@
|
||||
---
|
||||
# ------------------------------------------------------------------------------
|
||||
# FILE: roles/llm-inference-multimodel/tasks/discover.yml
|
||||
# DESCRIPTION: Phase 0 — READ-ONLY fact gathering on how the existing Gemma
|
||||
# llama-server is actually managed on astro-orbiter TODAY.
|
||||
#
|
||||
# Per plan §0/§4: "Service management: unverified — plan requires
|
||||
# confirming systemd unit exists before touching anything.
|
||||
# Do not assume." This file performs that confirmation. It makes
|
||||
# NO changes to the host — no `state: present/started/stopped`,
|
||||
# no file writes, no service actions. Every task here is either
|
||||
# a `_facts` module, a `command`/`shell` in check-safe read mode,
|
||||
# or a `stat`.
|
||||
#
|
||||
# Outcomes recorded as facts for later phases/for a human to read
|
||||
# in the play recap — this file does not branch role behavior
|
||||
# on the result (that would be over-engineering a role meant to
|
||||
# run once); it surfaces what's true so a human confirms before
|
||||
# Phase 2 proceeds.
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
- name: Gather service facts (systemd unit inventory)
|
||||
ansible.builtin.service_facts:
|
||||
|
||||
- name: Determine whether a systemd unit matching the existing Gemma service exists
|
||||
ansible.builtin.set_fact:
|
||||
llm_existing_gemma_unit_found: "{{ (llm_existing_gemma_service_name_guess + '.service') in ansible_facts.services }}"
|
||||
|
||||
- name: Report existing Gemma systemd unit state (if found)
|
||||
ansible.builtin.debug:
|
||||
msg: >-
|
||||
Existing unit '{{ llm_existing_gemma_service_name_guess }}.service' found:
|
||||
state={{ ansible_facts.services[llm_existing_gemma_service_name_guess + '.service'].state | default('unknown') }},
|
||||
status={{ ansible_facts.services[llm_existing_gemma_service_name_guess + '.service'].status | default('unknown') }}
|
||||
when: llm_existing_gemma_unit_found
|
||||
|
||||
- name: WARNING — no systemd unit found matching the existing Gemma service
|
||||
ansible.builtin.debug:
|
||||
msg: >-
|
||||
No systemd unit named '{{ llm_existing_gemma_service_name_guess }}.service'
|
||||
was found via service_facts. This means the current single-model
|
||||
llama-server is likely run some other way (manual nohup, screen/tmux,
|
||||
or a differently-named unit). DO NOT PROCEED to Phase 2 assuming a
|
||||
clean rollback target exists. Before continuing: (1) check for any
|
||||
running llama-server process via `ansible -m command -a "pgrep -fa
|
||||
llama-server"`, (2) if found running ad hoc, codify it as a proper
|
||||
systemd unit FIRST (reusing roles/llm-inference's existing
|
||||
llama-server.service.j2 pattern) so plan §6's rollback story
|
||||
("systemctl start llama-server-gemma to fully revert") is real and
|
||||
not aspirational. This is a human decision point, not something this
|
||||
role auto-remediates.
|
||||
when: not llm_existing_gemma_unit_found
|
||||
|
||||
- name: Check for any running llama-server process (read-only, no state change)
|
||||
ansible.builtin.command:
|
||||
cmd: pgrep -fa llama-server
|
||||
register: llm_existing_process_check
|
||||
changed_when: false
|
||||
failed_when: false # pgrep exits 1 with no matches — not a failure condition here
|
||||
|
||||
- name: Report any llama-server process found running outside systemd
|
||||
ansible.builtin.debug:
|
||||
msg: "Running llama-server process(es): {{ llm_existing_process_check.stdout_lines }}"
|
||||
when: llm_existing_process_check.rc == 0
|
||||
|
||||
- name: Check current GPU VRAM utilization (baseline, before any changes)
|
||||
ansible.builtin.command:
|
||||
cmd: nvidia-smi --query-gpu=memory.used,memory.total --format=csv,noheader
|
||||
register: llm_baseline_vram
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
|
||||
- name: Report baseline VRAM usage
|
||||
ansible.builtin.debug:
|
||||
msg: "Baseline GPU VRAM (before this role's changes): {{ llm_baseline_vram.stdout | default('nvidia-smi unavailable') }}"
|
||||
|
||||
- name: Check whether ports 8000/8001 are already bound (avoid port collision surprises)
|
||||
ansible.builtin.command:
|
||||
cmd: "ss -ltnp"
|
||||
register: llm_existing_listeners
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
|
||||
- name: Report current listeners on 8000/8001
|
||||
ansible.builtin.debug:
|
||||
msg: "{{ llm_existing_listeners.stdout_lines | select('search', ':(8000|8001)\\s') | list }}"
|
||||
when: llm_existing_listeners.rc == 0
|
||||
64
ansible/roles/llm-inference-multimodel/tasks/firewall.yml
Normal file
64
ansible/roles/llm-inference-multimodel/tasks/firewall.yml
Normal file
@@ -0,0 +1,64 @@
|
||||
---
|
||||
# ------------------------------------------------------------------------------
|
||||
# FILE: roles/llm-inference-multimodel/tasks/firewall.yml
|
||||
# DESCRIPTION: Phase 3 — scope :8001 (new) and reconsider :8000 (existing
|
||||
# pattern) exposure, per plan §5.
|
||||
#
|
||||
# Current baseline pattern (0.0.0.0:8000, no auth) is a
|
||||
# pre-existing flagged issue — this role does NOT repeat it
|
||||
# uncritically for the new port, and tightens both:
|
||||
# 1. Bind address: handled in systemd.yml templates via
|
||||
# {{ llm_bind_address }} (default 10.1.71.130, NOT 0.0.0.0).
|
||||
# 2. Firewall: ufw rules scoping both ports to
|
||||
# {{ llm_allowed_source_cidr }} rather than open LAN-wide.
|
||||
#
|
||||
# Idempotent: named rule comments + `state: present` so reruns
|
||||
# don't duplicate rules (per plan §4 idempotency note).
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
- name: Check whether ufw is installed/active
|
||||
ansible.builtin.command:
|
||||
cmd: ufw status
|
||||
register: llm_ufw_status
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
become: true
|
||||
|
||||
- name: WARNING — ufw not active, firewall scoping cannot be applied
|
||||
ansible.builtin.debug:
|
||||
msg: >-
|
||||
ufw does not appear to be active on this host (`ufw status` returned:
|
||||
{{ llm_ufw_status.stdout | default('n/a') }}). Firewall scoping for
|
||||
ports {{ llm_aux_port }}/{{ llm_toolcall_port }} was skipped. This is a
|
||||
gap vs plan §5 item 2 — flag to Ryan before relying on bind-address
|
||||
alone for exposure control.
|
||||
when: "'Status: active' not in (llm_ufw_status.stdout | default(''))"
|
||||
|
||||
- name: Allow aux port ({{ llm_aux_port }}) from the Hermes source subnet
|
||||
community.general.ufw:
|
||||
rule: allow
|
||||
port: "{{ llm_aux_port | string }}"
|
||||
proto: tcp
|
||||
src: "{{ llm_allowed_source_cidr }}"
|
||||
comment: "llm-inference-multimodel: aux (Phi-4) — scoped to Hermes subnet"
|
||||
become: true
|
||||
when: "'Status: active' in (llm_ufw_status.stdout | default(''))"
|
||||
|
||||
- name: Allow tool-calling port ({{ llm_toolcall_port }}) from the Hermes source subnet
|
||||
community.general.ufw:
|
||||
rule: allow
|
||||
port: "{{ llm_toolcall_port | string }}"
|
||||
proto: tcp
|
||||
src: "{{ llm_allowed_source_cidr }}"
|
||||
comment: "llm-inference-multimodel: toolcall (Mistral-Small) — scoped to Hermes subnet"
|
||||
become: true
|
||||
when: "'Status: active' in (llm_ufw_status.stdout | default(''))"
|
||||
|
||||
- name: Report firewall scoping applied
|
||||
ansible.builtin.debug:
|
||||
msg: >-
|
||||
Firewall scoping applied for ports {{ llm_aux_port }} and
|
||||
{{ llm_toolcall_port }}, restricted to source {{ llm_allowed_source_cidr }}.
|
||||
Reverse-proxy + API-key enforcement (plan §5 item 3) is NOT implemented
|
||||
by this role — flagged as an optional follow-up phase, not bundled into
|
||||
this minimum-viable rollout.
|
||||
35
ansible/roles/llm-inference-multimodel/tasks/main.yml
Normal file
35
ansible/roles/llm-inference-multimodel/tasks/main.yml
Normal file
@@ -0,0 +1,35 @@
|
||||
---
|
||||
# ------------------------------------------------------------------------------
|
||||
# FILE: roles/llm-inference-multimodel/tasks/main.yml
|
||||
# DESCRIPTION: Entry point — imports one task file per phase.
|
||||
# Phases are additive; re-running the full playbook is always
|
||||
# safe (idempotent). Use --tags to run a specific phase subset:
|
||||
# --tags discover,models,systemd,firewall,verify
|
||||
#
|
||||
# IMPORTANT: Phase 2 (systemd) deploys but does NOT start either service.
|
||||
# Phase 4 (verify) is what starts + smoke-tests them. This lets
|
||||
# Ryan review "systemd units land, nothing running yet" as a
|
||||
# distinct, revertable checkpoint before anything touches the
|
||||
# live GPU/VRAM state.
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
# Phase 0 — Discover (read-only; confirm how the existing Gemma llama-server
|
||||
# is actually managed today before assuming a systemd unit exists)
|
||||
- import_tasks: discover.yml
|
||||
tags: [discover]
|
||||
|
||||
# Phase 1 — Models (idempotent GGUF download, size-check guard)
|
||||
- import_tasks: models.yml
|
||||
tags: [models]
|
||||
|
||||
# Phase 2 — Systemd (template + deploy both unit files, do NOT auto-start)
|
||||
- import_tasks: systemd.yml
|
||||
tags: [systemd]
|
||||
|
||||
# Phase 3 — Firewall (scope :8001 and reconsider :8000 exposure)
|
||||
- import_tasks: firewall.yml
|
||||
tags: [firewall]
|
||||
|
||||
# Phase 4 — Verify (start both services, curl smoke test, nvidia-smi VRAM check)
|
||||
- import_tasks: verify.yml
|
||||
tags: [verify]
|
||||
72
ansible/roles/llm-inference-multimodel/tasks/models.yml
Normal file
72
ansible/roles/llm-inference-multimodel/tasks/models.yml
Normal file
@@ -0,0 +1,72 @@
|
||||
---
|
||||
# ------------------------------------------------------------------------------
|
||||
# FILE: roles/llm-inference-multimodel/tasks/models.yml
|
||||
# DESCRIPTION: Phase 1 — download both GGUFs to {{ llm_models_dir }}.
|
||||
# Idempotent: reuses the stat + size-threshold guard pattern
|
||||
# from the llm-inference-homelab skill / roles/llm-inference's
|
||||
# serve.yml, so reruns don't re-pull 8.5GB / 11.7GB files.
|
||||
#
|
||||
# Does NOT touch the existing Gemma GGUF — separate directory
|
||||
# entries, no overlap, no deletion of anything pre-existing.
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
- name: Create models directory
|
||||
ansible.builtin.file:
|
||||
path: "{{ llm_models_dir }}"
|
||||
state: directory
|
||||
owner: "{{ llm_service_user }}"
|
||||
group: "{{ llm_service_user }}"
|
||||
mode: "0755"
|
||||
become: true
|
||||
|
||||
# --- Aux model (Phi-4-14B Q4_K_M) --------------------------------------------
|
||||
|
||||
- name: Check if aux model GGUF already exists
|
||||
ansible.builtin.stat:
|
||||
path: "{{ llm_aux_model_path }}"
|
||||
register: llm_aux_model_stat
|
||||
|
||||
- name: Download aux model — Phi-4-14B-Q4_K_M GGUF
|
||||
ansible.builtin.get_url:
|
||||
url: "{{ llm_aux_model_url }}"
|
||||
dest: "{{ llm_aux_model_path }}"
|
||||
headers:
|
||||
Authorization: "Bearer {{ llm_hf_token }}"
|
||||
owner: "{{ llm_service_user }}"
|
||||
group: "{{ llm_service_user }}"
|
||||
mode: "0644"
|
||||
timeout: 7200
|
||||
force: false
|
||||
become: true
|
||||
no_log: true
|
||||
# Idempotency guard: skip if file exists and is above the min-size threshold
|
||||
# (catches partial/truncated downloads from an interrupted prior run).
|
||||
when: not llm_aux_model_stat.stat.exists or (llm_aux_model_stat.stat.size | int) < (llm_aux_model_min_bytes | int)
|
||||
|
||||
# --- Tool-calling model (Mistral-Small-24B Q3_K_M) ---------------------------
|
||||
|
||||
- name: Check if tool-calling model GGUF already exists
|
||||
ansible.builtin.stat:
|
||||
path: "{{ llm_toolcall_model_path }}"
|
||||
register: llm_toolcall_model_stat
|
||||
|
||||
- name: Download tool-calling model — Mistral-Small-24B-Instruct-2501 Q3_K_M GGUF
|
||||
ansible.builtin.get_url:
|
||||
url: "{{ llm_toolcall_model_url }}"
|
||||
dest: "{{ llm_toolcall_model_path }}"
|
||||
headers:
|
||||
Authorization: "Bearer {{ llm_hf_token }}"
|
||||
owner: "{{ llm_service_user }}"
|
||||
group: "{{ llm_service_user }}"
|
||||
mode: "0644"
|
||||
timeout: 7200
|
||||
force: false
|
||||
become: true
|
||||
no_log: true
|
||||
when: not llm_toolcall_model_stat.stat.exists or (llm_toolcall_model_stat.stat.size | int) < (llm_toolcall_model_min_bytes | int)
|
||||
|
||||
- name: Report model files present on disk
|
||||
ansible.builtin.debug:
|
||||
msg:
|
||||
- "Aux model: {{ llm_aux_model_path }}"
|
||||
- "Tool-calling model: {{ llm_toolcall_model_path }}"
|
||||
54
ansible/roles/llm-inference-multimodel/tasks/systemd.yml
Normal file
54
ansible/roles/llm-inference-multimodel/tasks/systemd.yml
Normal file
@@ -0,0 +1,54 @@
|
||||
---
|
||||
# ------------------------------------------------------------------------------
|
||||
# FILE: roles/llm-inference-multimodel/tasks/systemd.yml
|
||||
# DESCRIPTION: Phase 2 — template + deploy both unit files.
|
||||
# DELIBERATELY DOES NOT START OR ENABLE either service — that is
|
||||
# Phase 4 (verify.yml)'s job, after Phase 3 firewall scoping is
|
||||
# in place. This keeps "units land on disk" and "processes
|
||||
# actually bind ports and load 20+GB into VRAM" as separately
|
||||
# reviewable checkpoints per Ryan's iterative-build preference.
|
||||
#
|
||||
# Two independent units (llama-server-aux.service,
|
||||
# llama-server-toolcall.service) — NOT one unit with two
|
||||
# ExecStarts — so either can be stopped/restarted without
|
||||
# affecting the other (plan §2, §6 rollback requirement).
|
||||
#
|
||||
# The pre-existing Gemma unit (whatever discover.yml found it to
|
||||
# be) is never templated, restarted, or disabled by this file.
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
- name: Deploy llama-server-aux systemd unit
|
||||
ansible.builtin.template:
|
||||
src: llama-server-aux.service.j2
|
||||
dest: "/etc/systemd/system/{{ llm_aux_service_name }}.service"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
become: true
|
||||
notify:
|
||||
- reload systemd
|
||||
- restart llama-server-aux
|
||||
|
||||
- name: Deploy llama-server-toolcall systemd unit
|
||||
ansible.builtin.template:
|
||||
src: llama-server-toolcall.service.j2
|
||||
dest: "/etc/systemd/system/{{ llm_toolcall_service_name }}.service"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
become: true
|
||||
notify:
|
||||
- reload systemd
|
||||
- restart llama-server-toolcall
|
||||
|
||||
- name: Flush handlers so daemon-reload lands before any later phase acts on unit state
|
||||
ansible.builtin.meta: flush_handlers
|
||||
|
||||
# NOTE: no `ansible.builtin.systemd: state: started / enabled: true` task here
|
||||
# on purpose. Units exist on disk after this phase; nothing is running.
|
||||
# The "restart" handlers above only fire (and thus only start anything) if
|
||||
# the template content actually changed AND a later flush_handlers/end-of-play
|
||||
# triggers them — on a first-ever apply this DOES start the services once,
|
||||
# which is expected/acceptable for a fresh deploy, but on any subsequent
|
||||
# re-run with no template changes, nothing restarts. Ryan/verify.yml owns
|
||||
# the deliberate first start + smoke test.
|
||||
133
ansible/roles/llm-inference-multimodel/tasks/verify.yml
Normal file
133
ansible/roles/llm-inference-multimodel/tasks/verify.yml
Normal file
@@ -0,0 +1,133 @@
|
||||
---
|
||||
# ------------------------------------------------------------------------------
|
||||
# FILE: roles/llm-inference-multimodel/tasks/verify.yml
|
||||
# DESCRIPTION: Phase 4 — start both services, curl smoke test each endpoint,
|
||||
# nvidia-smi VRAM check against plan §1 math, confirm no OOM.
|
||||
#
|
||||
# This is the ONLY phase that actually starts the services
|
||||
# (systemd.yml deliberately does not). Enabling happens here too,
|
||||
# so a reboot brings both back — matching plan §2's "independent
|
||||
# systemd services" intent for durability, not just this-session.
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
- name: Enable and start llama-server-aux
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ llm_aux_service_name }}"
|
||||
state: started
|
||||
enabled: true
|
||||
daemon_reload: true
|
||||
become: true
|
||||
|
||||
- name: Enable and start llama-server-toolcall
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ llm_toolcall_service_name }}"
|
||||
state: started
|
||||
enabled: true
|
||||
daemon_reload: true
|
||||
become: true
|
||||
|
||||
- name: Wait for aux instance API to become available (model load may take a couple minutes)
|
||||
ansible.builtin.uri:
|
||||
url: "http://{{ llm_bind_address }}:{{ llm_aux_port }}/health"
|
||||
status_code: 200
|
||||
register: llm_aux_health
|
||||
retries: 24
|
||||
delay: 10
|
||||
until: llm_aux_health.status == 200
|
||||
|
||||
- name: Wait for tool-calling instance API to become available
|
||||
ansible.builtin.uri:
|
||||
url: "http://{{ llm_bind_address }}:{{ llm_toolcall_port }}/health"
|
||||
status_code: 200
|
||||
register: llm_toolcall_health
|
||||
retries: 24
|
||||
delay: 10
|
||||
until: llm_toolcall_health.status == 200
|
||||
|
||||
- name: Smoke-test — aux instance model listing
|
||||
ansible.builtin.uri:
|
||||
url: "http://{{ llm_bind_address }}:{{ llm_aux_port }}/v1/models"
|
||||
status_code: 200
|
||||
return_content: true
|
||||
register: llm_aux_models
|
||||
|
||||
- name: Smoke-test — tool-calling instance model listing
|
||||
ansible.builtin.uri:
|
||||
url: "http://{{ llm_bind_address }}:{{ llm_toolcall_port }}/v1/models"
|
||||
status_code: 200
|
||||
return_content: true
|
||||
register: llm_toolcall_models
|
||||
|
||||
- name: Report served models per instance
|
||||
ansible.builtin.debug:
|
||||
msg:
|
||||
- "Aux (:{{ llm_aux_port }}) serving: {{ llm_aux_models.json.data | map(attribute='id') | list }}"
|
||||
- "Tool-calling (:{{ llm_toolcall_port }}) serving: {{ llm_toolcall_models.json.data | map(attribute='id') | list }}"
|
||||
|
||||
- name: Basic completion smoke test — aux instance (non-tool-calling sanity check only)
|
||||
ansible.builtin.uri:
|
||||
url: "http://{{ llm_bind_address }}:{{ llm_aux_port }}/v1/chat/completions"
|
||||
method: POST
|
||||
body_format: json
|
||||
body:
|
||||
model: "{{ llm_aux_model_id }}"
|
||||
messages:
|
||||
- role: user
|
||||
content: "Reply with exactly one word: OK"
|
||||
max_tokens: 10
|
||||
status_code: 200
|
||||
return_content: true
|
||||
register: llm_aux_completion
|
||||
|
||||
- name: Basic completion smoke test — tool-calling instance (plain-text sanity check only)
|
||||
ansible.builtin.uri:
|
||||
url: "http://{{ llm_bind_address }}:{{ llm_toolcall_port }}/v1/chat/completions"
|
||||
method: POST
|
||||
body_format: json
|
||||
body:
|
||||
model: "{{ llm_toolcall_model_id }}"
|
||||
messages:
|
||||
- role: user
|
||||
content: "Reply with exactly one word: OK"
|
||||
max_tokens: 10
|
||||
status_code: 200
|
||||
return_content: true
|
||||
register: llm_toolcall_completion
|
||||
|
||||
- name: NOTE — this smoke test is NOT the tool-calling validation harness
|
||||
ansible.builtin.debug:
|
||||
msg: >-
|
||||
Both endpoints respond to basic completions. This does NOT validate
|
||||
tool_calls correctness or hallucination-safety for the tool-calling
|
||||
instance — that is a separate, manual, post-deploy procedure (plan §7).
|
||||
See references/tool-calling-validation.sh (copied from the
|
||||
llm-inference-homelab skill) and docs/validation-log.md in this role.
|
||||
DO NOT point any Claude Code / tool-calling-capable Hermes profile at
|
||||
port {{ llm_toolcall_port }} until that validation has passed and been
|
||||
logged.
|
||||
|
||||
- name: Check GPU VRAM usage after both instances are running
|
||||
ansible.builtin.command:
|
||||
cmd: nvidia-smi --query-gpu=memory.used,memory.total,utilization.gpu --format=csv,noheader
|
||||
register: llm_post_start_vram
|
||||
changed_when: false
|
||||
|
||||
- name: Report VRAM usage vs plan §1 expectations
|
||||
ansible.builtin.debug:
|
||||
msg:
|
||||
- "Measured (nvidia-smi): {{ llm_post_start_vram.stdout }}"
|
||||
- "Design estimate (plan §1): aux ~{{ llm_aux_expected_vram_gb }}GB + toolcall ~{{ llm_toolcall_expected_vram_gb }}GB = ~{{ llm_combined_expected_vram_gb }}GB / {{ llm_gpu_total_vram_gb }}GB total"
|
||||
- "If measured usage exceeds ~23.5GB or is within ~0.5GB of the 24GB card limit, treat as the OOM-risk trigger condition from plan §6 — do not leave both services running unattended without confirming headroom."
|
||||
|
||||
- name: Check for OOM-kill events related to llama-server in dmesg (best-effort, read-only)
|
||||
ansible.builtin.shell:
|
||||
cmd: "dmesg | grep -i 'llama-server' | grep -i -E 'oom|killed' || true"
|
||||
register: llm_oom_check
|
||||
changed_when: false
|
||||
become: true
|
||||
|
||||
- name: Report any OOM-kill findings
|
||||
ansible.builtin.debug:
|
||||
msg: >-
|
||||
{{ llm_oom_check.stdout if llm_oom_check.stdout | length > 0
|
||||
else 'No OOM-kill events found for llama-server in dmesg.' }}
|
||||
Reference in New Issue
Block a user