--- # --- Install: Debian/Ubuntu --- - name: Check if Caddy is already installed ansible.builtin.command: which caddy register: caddy_installed changed_when: false failed_when: false when: ansible_facts['os_family'] == "Debian" - name: Install required packages (Debian/Ubuntu) ansible.builtin.apt: name: - debian-keyring - debian-archive-keyring - apt-transport-https - curl - gnupg2 state: present update_cache: true when: ansible_facts['os_family'] == "Debian" - name: Check for conflicting Caddy repositories ansible.builtin.stat: path: /usr/share/keyrings/caddy-stable-archive-keyring.gpg register: old_keyring when: ansible_facts['os_family'] == "Debian" - name: Remove old Caddy repository files if they exist ansible.builtin.file: path: "{{ item }}" state: absent loop: - /etc/apt/sources.list.d/caddy-stable.list - /etc/apt/sources.list.d/caddy.list - /usr/share/keyrings/caddy-stable-archive-keyring.gpg when: - ansible_facts['os_family'] == "Debian" - old_keyring.stat.exists | default(false) notify: update apt cache - name: Check if Caddy GPG key exists and is valid ansible.builtin.stat: path: /etc/apt/trusted.gpg.d/caddy-stable.asc register: caddy_gpg_key when: ansible_facts['os_family'] == "Debian" - name: Add Caddy GPG key (Debian/Ubuntu) ansible.builtin.get_url: url: "https://dl.cloudsmith.io/public/caddy/stable/gpg.key" dest: /usr/share/keyrings/caddy-stable-archive-keyring.asc mode: '0644' when: ansible_facts['os_family'] == "Debian" - name: Add Caddy repository (Debian/Ubuntu) ansible.builtin.deb822_repository: name: caddy-stable types: deb uris: https://dl.cloudsmith.io/public/caddy/stable/deb/debian suites: any-version components: main signed_by: /usr/share/keyrings/caddy-stable-archive-keyring.asc state: present when: ansible_facts['os_family'] == "Debian" - name: Install Caddy (Debian/Ubuntu) ansible.builtin.apt: name: caddy state: present update_cache: true when: - ansible_facts['os_family'] == "Debian" - caddy_installed.rc != 0 # --- Install: RedHat/AlmaLinux --- - name: Install dnf copr plugin (RedHat) ansible.builtin.dnf: name: dnf-plugins-core state: present when: ansible_facts['os_family'] == "RedHat" - name: Enable Caddy COPR repository (RedHat) ansible.builtin.command: cmd: dnf -y copr enable @caddy/caddy creates: /etc/yum.repos.d/_copr:copr.fedorainfracloud.org:group_caddy:caddy.repo when: ansible_facts['os_family'] == "RedHat" - name: Install Caddy (RedHat) ansible.builtin.dnf: name: caddy state: present when: ansible_facts['os_family'] == "RedHat" # --- Common configuration --- - name: Ensure Caddy log directory exists ansible.builtin.file: path: /var/log/caddy state: directory owner: "{{ caddy_user }}" group: "{{ caddy_group }}" mode: '0755' - name: Ensure Caddy service is enabled and started ansible.builtin.service: name: caddy state: started enabled: true - name: Configure Caddy (shared template via caddy_template) ansible.builtin.template: src: "{{ caddy_template }}" dest: /etc/caddy/Caddyfile owner: root group: root mode: '0644' notify: restart caddy when: caddy_template is defined - name: Check if host-specific Caddyfile template exists ansible.builtin.stat: path: "{{ playbook_dir }}/roles/caddy/templates/Caddyfile-{{ inventory_hostname }}.j2" register: host_specific_template delegate_to: localhost become: false when: caddy_template is not defined - name: Configure Caddy (host-specific template) ansible.builtin.template: src: "Caddyfile-{{ inventory_hostname }}.j2" dest: /etc/caddy/Caddyfile owner: root group: root mode: '0644' notify: restart caddy when: - caddy_template is not defined - host_specific_template.stat.exists | default(false) - name: Configure Caddy (default template) ansible.builtin.template: src: Caddyfile.j2 dest: /etc/caddy/Caddyfile owner: root group: root mode: '0644' notify: restart caddy when: - caddy_template is not defined - not (host_specific_template.stat.exists | default(false))