---
- name: Ensure snapd is installed.
  ansible.builtin.package:
    name: snapd
    state: present
  register: snapd_install

- name: Ensure snapd is enabled.
  ansible.builtin.systemd:
    name: snapd.socket
    enabled: true
    state: started

- name: Enable classic snap support.
  ansible.builtin.file:
    src: /var/lib/snapd/snap
    dest: /snap
    state: link
  when: ansible_facts.os_family != "Debian"

- name: Update snap after install.
  ansible.builtin.shell: snap install core; snap refresh core
  changed_when: true
  failed_when: false
  when: snapd_install is changed

- name: Install certbot via snap.
  community.general.snap:
    name: certbot
    classic: true

- name: Symlink certbot into place.
  ansible.builtin.file:
    src: /snap/bin/certbot
    dest: /usr/bin/certbot
    state: link
  ignore_errors: "{{ ansible_check_mode }}"

- name: Set Certbot script variable.
  ansible.builtin.set_fact:
    certbot_script: /usr/bin/certbot
