---

- name: "Setup DNS configuration for <{{ connect_id }}> to <{{ stage_server_ip }}>"
  include_role:
    name: hetzner-ansible-dns
  vars:
    record_data: "{{ stage_server_ip }}"
    record_name: "{{ connect_id }}"

- name: "Setup DNS configuration for <{{ connect_external_subdomain }}> to <{{ stage_server_ip }}>"
  include_role:
    name: hetzner-ansible-dns
  vars:
    record_data: "{{ stage_server_ip }}"
    record_name: "{{ connect_external_subdomain }}"
  when: connect_external_subdomain is defined

- name: "Setup DNS configuration for <{{ kibana_id }}> to <{{ stage_server_ip }}>"
  include_role:
    name: hetzner-ansible-dns
  vars:
    record_data: "{{ stage_server_ip }}"
    record_name: "{{ kibana_id }}"

- name: "Setup realm for {{ connect_id }}"
  include_role:
    name: keycloak
    tasks_from: _configure_realm
    apply:
        tags:
          - configure_realm
  tags:
    - configure_realm

- name: "Check if {{ connect_id }}/docker-compose.yml exists"
  stat:
    path: '{{ service_base_path }}/{{ connect_id }}/docker-compose.yml'
  register: check_docker_compose_file_connect

- name: "Deploy docker templates for {{ connect_id }}"
  include_role:
    name: hetzner-ansible-sma-deploy
    tasks_from: templates
  vars:
    current_config: "connect-compact"
    current_base_path: "{{ service_base_path }}"
    current_destination: "{{ connect_id }}"
    current_owner: "{{ docker_owner }}"
    current_group: "{{ docker_group }}"

- name: "Stop {{ connect_id }}"
  community.docker.docker_compose:
    project_src: '{{ service_base_path }}/{{ connect_id }}'
    state: absent
  when: check_docker_compose_file_connect.stat.exists

- name: "Restart {{ connect_id }}"
  community.docker.docker_compose:
    project_src: '{{ service_base_path }}/{{ connect_id }}'
    state: present
    pull: no
