
- include: ../functions/get_secret.yml
  with_items:
    - { path: "{{ basedir }}/letsencrypt_notification_email",  length: -1 }
  when: selfSignedCN is not defined 

- name: "create folder struct for {{ servicename }}"
  file:
    path: "{{ item }}"
    state: "directory"
  with_items:
    - "{{ basedir }}"
    - "{{ basedir }}/dynamic"

- name: "Check if CertStore exists"
  stat:
    path: "{{ basedir }}/acme.json"
  register: acme_stat_result

- name: "Create CertStore if needed and set permissions"
  file:
    path: "{{ basedir }}/acme.json"
    owner: root
    group: root
    mode: '600'
    state: touch
  when: not acme_stat_result.stat.exists

- name: "Create SelfSigned CA and Cert"
  ansible.builtin.include_tasks: certificate.yml
  when: selfSignedCN is defined 
  

- name: Docker Compose Konfig-Datei erstellen
  template:
    src: "{{ item }}"
    dest: "{{ basedir }}/{{ item }}"
  with_items:
    - docker-compose.yml
    - traefik.yml 
    - dynamic/redirect-default.yml
    - dynamic/tls.yml
  register: config

- name: "stop {{ servicename}} docker"
  docker_compose:
    project_src: "{{ basedir }}"
    state: absent
  when: config.changed

- name: "start {{ servicename}} docker"
  docker_compose:
    project_src: "{{ basedir }}"
    state: present
    
