blumeops/ansible/roles/kiwix/tasks/main.yml
Erich Blume d3d3041b27 Decouple ZIM/torrent ansible tasks for faster provisioning (#18)
## Summary
- Simplify kiwix role from 213 lines to 151 lines (-30%)
- Replace per-archive torrent status loops with single shell command
- Decouple kiwix startup from declared inventory - now serves whatever completed ZIM files exist
- Fix tailscale_serve role to handle empty JSON in check mode

## Performance improvement
- **Before**: ~132 operations (44 archives × 3 loops for status check, recheck, symlink)
- **After**: ~5 operations (1 shell script + 1 find + conditional symlinks)
- Expected reduction: ~3 minutes per ansible run

## Test plan
- [x] Ran `mise run provision-indri -- --check --diff` to preview changes
- [x] Ran `mise run provision-indri` to apply changes
- [x] Ran `mise run indri-services-check` - all services healthy

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Reviewed-on: https://forge.tail8d86e.ts.net/eblume/blumeops/pulls/18
2026-01-16 15:14:00 -08:00

150 lines
5.2 KiB
YAML

---
- name: Ensure kiwix ZIM directory exists
ansible.builtin.file:
path: "{{ kiwix_zim_dir }}"
state: directory
mode: '0755'
# --- Transmission-based torrent management ---
# This section ensures declared ZIM archives have torrents added to transmission.
# It does NOT wait for downloads to complete - kiwix startup handles that separately.
- name: Check transmission daemon is responding
ansible.builtin.command: transmission-remote -l
register: transmission_check
changed_when: false
failed_when: false
when: kiwix_use_transmission
- name: Fail if transmission is not running
ansible.builtin.fail:
msg: "Transmission daemon is not responding. Ensure transmission role ran successfully."
when: kiwix_use_transmission and transmission_check.rc != 0
# Find which declared archives don't have torrents yet (single shell command)
- name: Find declared archives missing from transmission
ansible.builtin.shell: |
set -euo pipefail
# Get current torrent list (skip header and footer)
torrents=$(transmission-remote -l 2>/dev/null | tail -n +2 | head -n -1 || true)
# Check each declared archive
{% for archive in kiwix_zim_archives %}
base="{{ archive.filename | regex_replace('\\.zim$', '') }}"
if ! echo "$torrents" | grep -qF "$base"; then
echo "{{ archive.category }}/{{ archive.filename }}"
fi
{% endfor %}
args:
executable: /bin/bash
register: missing_torrents
changed_when: false
when: kiwix_use_transmission
# Add only the missing torrents
- name: Add missing torrents to transmission
ansible.builtin.command: >
transmission-remote -a "{{ kiwix_torrent_base_url }}/{{ item }}.torrent"
loop: "{{ missing_torrents.stdout_lines | default([]) }}"
loop_control:
label: "{{ item | basename }}"
when:
- kiwix_use_transmission
- missing_torrents.stdout_lines | default([]) | length > 0
register: torrent_add
changed_when: torrent_add.rc == 0
# --- Kiwix startup: serve whatever completed ZIM files exist ---
# This is decoupled from the declared inventory - it just serves what's available.
# Find all completed ZIM files in transmission download directory
- name: Find completed ZIM files in transmission download directory
ansible.builtin.find:
paths: "{{ transmission_download_dir }}"
patterns: "*.zim"
file_type: file
register: completed_zim_files
when: kiwix_use_transmission
# Check which ZIM files already have symlinks in kiwix directory
- name: Check existing symlinks in kiwix directory
ansible.builtin.stat:
path: "{{ kiwix_zim_dir }}/{{ item.path | basename }}"
get_checksum: false
loop: "{{ completed_zim_files.files | default([]) }}"
loop_control:
label: "{{ item.path | basename }}"
register: existing_symlinks
when: kiwix_use_transmission
# Create symlinks for any completed ZIM files not yet linked
- name: Symlink completed ZIM files to kiwix directory
ansible.builtin.file:
src: "{{ item.item.path }}"
dest: "{{ kiwix_zim_dir }}/{{ item.item.path | basename }}"
state: link
loop: "{{ existing_symlinks.results | default([]) }}"
loop_control:
label: "{{ item.item.path | basename }}"
when:
- kiwix_use_transmission
- item.stat is defined
- not item.stat.exists
notify: restart kiwix-serve
# --- Fallback: Direct HTTP download (original behavior) ---
- name: Check which ZIM archives exist (direct download mode)
ansible.builtin.stat:
path: "{{ kiwix_zim_dir }}/{{ item.filename }}"
get_checksum: false
loop: "{{ kiwix_zim_archives }}"
loop_control:
label: "{{ item.filename }}"
register: zim_stat
when: not kiwix_use_transmission
- name: Download missing ZIM archives (direct download mode)
ansible.builtin.get_url:
url: "https://download.kiwix.org/zim/{{ item.item.category }}/{{ item.item.filename }}"
dest: "{{ kiwix_zim_dir }}/{{ item.item.filename }}"
mode: '0644'
timeout: 3600
loop: "{{ zim_stat.results | default([]) }}"
loop_control:
label: "{{ item.item.filename | default('unknown') }}"
when:
- not kiwix_use_transmission
- item.stat is defined
- not item.stat.exists
notify: restart kiwix-serve
# --- Determine which archives are available ---
- name: Find available ZIM archives in kiwix directory
ansible.builtin.find:
paths: "{{ kiwix_zim_dir }}"
patterns: "*.zim"
file_type: any # includes symlinks
register: available_zim_files
- name: Build list of available archive filenames
ansible.builtin.set_fact:
kiwix_available_archives: "{{ available_zim_files.files | map(attribute='path') | map('basename') | list }}"
# --- LaunchAgent deployment ---
- name: Deploy kiwix-serve LaunchAgent plist
ansible.builtin.template:
src: kiwix-serve.plist.j2
dest: ~/Library/LaunchAgents/mcquack.eblume.kiwix-serve.plist
mode: '0644'
notify: restart kiwix-serve
- name: Check if kiwix-serve LaunchAgent is loaded
ansible.builtin.command: launchctl list mcquack.eblume.kiwix-serve
register: launchctl_check
changed_when: false
failed_when: false
- name: Load kiwix-serve LaunchAgent if not loaded
ansible.builtin.command: launchctl load ~/Library/LaunchAgents/mcquack.eblume.kiwix-serve.plist
when: launchctl_check.rc != 0
failed_when: false