---
- name: Generate keys and certificates
  hosts:
    - localhost
  gather_facts: false
  handlers:
    - name: Restart Nginx on host01
      delegate_to: host01
      command: docker restart web-proxy_proxy_1

    - name: Restart Nginx on ns4
      delegate_to: ns4
      command: docker restart web-proxy_proxy_1

    - name: Restart Nginx on kodi
      delegate_to: kodi
      command: docker restart web-proxy_proxy_1

    - name: Reload Exim
      delegate_to: host01
      command: docker kill --signal SIGHUP mail_smtp_1

    - name: Reload Dovecot
      delegate_to: host01
      command: docker kill --signal SIGHUP mail_imap_1

  tasks:
    - name: Issue certificate for ns4
      include_tasks:
        apply:
          tags:
            - ns4
        file: '{{ playbook_dir }}/tasks/renew-cert.yaml'
      vars:
        domains:
          - autoconfig.shore.co.il
          - myip.shore.co.il
          - ns4.shore.co.il
          - registry.shore.co.il
          - shore.co.il
          - www.shore.co.il
        handlers:
          - Restart Nginx on ns4
        host: ns4
      tags:
        - ns4

    - name: Issue certificate for host01
      include_tasks:
        apply:
          tags:
            - host01
        file: '{{ playbook_dir }}/tasks/renew-cert.yaml'
      vars:
        domains:
          - code.shore.co.il
          - git.shore.co.il
          - lam.shore.co.il
          - nextcloud.shore.co.il
          - ns1.shore.co.il
          - vouch.shore.co.il
        handlers:
          - Restart Nginx on host01
        host: host01
      tags:
        - host01

    - name: Issue certificate for kodi
      include_tasks:
        apply:
          tags:
            - kodi
        file: '{{ playbook_dir }}/tasks/renew-cert.yaml'
      vars:
        domains:
          - jellyfin.shore.co.il
          - kodi.shore.co.il
          - library.shore.co.il
          - transmission.shore.co.il
        handlers:
          - Restart Nginx on kodi
        host: kodi
      tags:
        - kodi

    - name: Issue certificate for smtp
      include_tasks:
        apply:
          tags:
            - smtp
        file: '{{ playbook_dir }}/tasks/renew-cert.yaml'
      vars:
        delegate_host: host01
        domains:
          - imap.shore.co.il
          - mta-sts.shore.co.il
          - smtp.shore.co.il
        filename: mail
        handlers:
          - Reload Dovecot
          - Reload Exim
          - Restart Nginx on host01
        host: mail
      tags:
        - smtp
