--- - name: Deploy and Reload Caddy & frpc on Heterogeneous Systems hosts: caddy_servers become: yes # # 'pre_tasks' 会在所有常规 'tasks' 之前执行 # pre_tasks: # - name: "(Alpine Only) Ensure Python 3 is installed for Ansible" # # 使用 'raw' 模块,因为它不依赖 Python # # 这是一个标准的在 Alpine 上引导 Ansible 的方法 # raw: test -e /usr/bin/python3 || (apk update && apk add python3) # register: result # changed_when: "'OK' in result.stdout or 'fetch' in result.stdout" # # 'when' 条件确保这个任务只在 Alpine 系统上运行 # when: ansible_os_family == "Alpine" tasks: # --- Caddy 任务 (保持不变) --- - name: "Copy Caddyfile for {{ inventory_hostname }}" copy: src: "files/{{ inventory_hostname }}/Caddyfile" dest: /etc/caddy/Caddyfile owner: root group: caddy mode: '0644' validate: 'caddy validate --config %s --adapter caddyfile' notify: Reload Caddy handlers: # --- Caddy Handlers (保持不变) --- - name: Reload Caddy on systemd systems (e.g., Ubuntu) # Both handlers listen for the same notification listen: "Reload Caddy" systemd: name: caddy state: reloaded # This handler ONLY runs on systems with systemd when: ansible_service_mgr == "systemd" - name: Reload Caddy on OpenRC systems (e.g., Alpine) # Both handlers listen for the same notification listen: "Reload Caddy" # For Alpine, we use the 'command' module to call its service manager command: rc-service caddy reload # This handler ONLY runs on systems with openrc when: ansible_service_mgr == "openrc"