- name: Prüfe erforderliche Variablen
  assert:
    that:
      - deploy_root is defined
      - deploy_user is defined
    fail_msg: "Erforderliche Variablen fehlen: deploy_root und/oder deploy_user nicht definiert"
    success_msg: "Alle erforderlichen Variablen sind korrekt definiert"
  tags: [always]

- name: Docker-Abhängigkeiten installieren
  apt:
    name:
      - apt-transport-https
      - ca-certificates
      - curl
      - gnupg
      - lsb-release
    state: present
    update_cache: yes
---
# Docker-Installation und -Konfiguration

- name: Docker-Abhängigkeiten installieren
  apt:
    name:
      - ca-certificates
      - curl
      - gnupg
      - apt-transport-https
      - software-properties-common
    state: present
    update_cache: yes
---
# Tasks für Docker-Installation

- name: Docker-Abhängigkeiten installieren
  apt:
    name:
      - apt-transport-https
      - ca-certificates
      - gnupg
      - lsb-release
    state: present

- name: Docker GPG-Schlüssel hinzufügen
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present

- name: Docker Repository hinzufügen
  apt_repository:
    repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable
    state: present

- name: Docker installieren
  apt:
    name:
      - docker-ce
      - docker-ce-cli
      - containerd.io
      - docker-compose-plugin
    state: present
    update_cache: yes

- name: Docker-Gruppe erstellen
  group:
    name: docker
    state: present

- name: Benutzer zur Docker-Gruppe hinzufügen
  user:
    name: "{{ deploy_user }}"
    groups: docker
    append: yes

- name: Docker-Dienst aktivieren und starten
  service:
    name: docker
    state: started
    enabled: yes

- name: Prüfen, ob Docker funktioniert
  shell: docker --version
  register: docker_version
  changed_when: false

- name: Docker-Version anzeigen
  debug:
    var: docker_version.stdout
- name: Docker GPG-Schlüssel hinzufügen
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present

- name: Docker-Repository hinzufügen
  apt_repository:
    repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable
    state: present

- name: Docker installieren
  apt:
    name:
      - docker-ce
      - docker-ce-cli
      - containerd.io
    state: present
    update_cache: yes

- name: Docker-Gruppe erstellen
  group:
    name: docker
    state: present

- name: Deploy-Benutzer zur Docker-Gruppe hinzufügen
  user:
    name: "{{ deploy_user }}"
    groups: docker
    append: yes

- name: Docker-Service aktivieren und starten
  systemd:
    name: docker
    state: started
    enabled: yes

- name: Docker Compose herunterladen
  get_url:
    url: "https://github.com/docker/compose/releases/download/v{{ docker_compose_version }}/docker-compose-Linux-x86_64"
    dest: /usr/local/bin/docker-compose
    mode: '0755'

- name: Docker-Verzeichnisse und -Dateien kopieren
  copy:
    src: "{{ playbook_dir }}/../docker/"
    dest: "{{ deploy_root }}/docker/"
    directory_mode: '0755'
    mode: '0644'

- name: Docker Compose-Datei kopieren
  copy:
    src: "{{ playbook_dir }}/../docker-compose.yml"
    dest: "{{ deploy_root }}/docker-compose.yml"
    mode: '0644'
- name: Docker GPG-Schlüssel hinzufügen
  apt_key:
    url: https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg
    state: present

- name: Docker Repository hinzufügen
  apt_repository:
    repo: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable"
    state: present

- name: Docker Engine installieren
  apt:
    name:
      - docker-ce
      - docker-ce-cli
      - containerd.io
      - docker-compose-plugin
    state: present
    update_cache: yes

- name: Docker Compose installieren (V1 als Fallback)
  get_url:
    url: "https://github.com/docker/compose/releases/download/v{{ docker_compose_version }}/docker-compose-linux-x86_64"
    dest: /usr/local/bin/docker-compose
    mode: '0755'

- name: Benutzer zur Docker-Gruppe hinzufügen
  user:
    name: "{{ ansible_user }}"
    groups: docker
    append: yes

- name: Docker-Service starten und aktivieren
  service:
    name: docker
    state: started
    enabled: yes
  notify: restart docker


- name: Starte Docker-Container via Compose
  community.docker.docker_compose_v2:
    #project_src: "{{ playbook_dir | dirname }}/../"   # ggf. anpassen auf deinen Compose-Pfad!
    project_src: "{{ deploy_root }}"
    build: always
    recreate: always
  when: false

