- name: Install & configure Caddy
  hosts: athelas
  become: true
  tasks:

  - name: Add Caddy GPG Key
    ansible.builtin.get_url:
      url: https://dl.cloudsmith.io/public/caddy/stable/gpg.key
      dest: /etc/apt/keyrings/caddy.asc
      mode: '0644'
      force: true

  - name: Add Caddy Repo
    ansible.builtin.apt_repository:
      repo: deb [signed-by=/etc/apt/keyrings/caddy.asc] https://dl.cloudsmith.io/public/caddy/stable/deb/debian any-version main
      state: present

  - name: Add Caddy Src Repo
    ansible.builtin.apt_repository:
      repo: deb-src [signed-by=/etc/apt/keyrings/caddy.asc] https://dl.cloudsmith.io/public/caddy/stable/deb/debian any-version main
      state: present

  - name: Install Caddy
    ansible.builtin.package:
      name: caddy

  - name: Editing Caddyfile to setup the reverse_proxy
    ansible.builtin.template:
      src: ~/ansible/templates/Caddyfile.j2
      dest: /etc/caddy/Caddyfile

  - name: Put the service binary on the remote server
    ansible.builtin.copy:
      src: ~/ansible/files/usertwist
      dest: /usr/local/bin
      owner: root
      group: root
      mode: '0755'

  - name: Put the service systemd file on the remote server
    ansible.builtin.copy:
      src: ~/ansible/files/usertwist.service
      dest: /etc/systemd/system
      owner: root
      group: root

  - name: Enable the service systemd unit
    ansible.builtin.systemd_service:
      name: usertwist
      enabled: true
      state: started

  - name: Restart Caddy service
    ansible.builtin.service:
      name: caddy
      state: restarted
