--- - 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: kiwix_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 kiwix_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: kiwix_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: "{{ kiwix_missing_torrents.stdout_lines | default([]) }}" loop_control: label: "{{ item | basename }}" when: - kiwix_use_transmission - kiwix_missing_torrents.stdout_lines | default([]) | length > 0 register: kiwix_torrent_add changed_when: kiwix_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: kiwix_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: "{{ kiwix_completed_zim_files.files | default([]) }}" loop_control: label: "{{ item.path | basename }}" register: kiwix_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: "{{ kiwix_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: kiwix_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: "{{ kiwix_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: kiwix_available_zim_files - name: Build list of available archive filenames ansible.builtin.set_fact: kiwix_available_archives: "{{ kiwix_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: kiwix_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: kiwix_launchctl_check.rc != 0 changed_when: true failed_when: false