---
- name: Create Traefik log directory
  ansible.builtin.file:
    path: /var/log/traefik
    state: directory
    owner: root
    group: root
    mode: "0755"
  tags: traefik

- name: Create Traefik data directory
  ansible.builtin.file:
    path: "{{ traefik_data_dir }}"
    state: directory
    owner: root
    group: root
    mode: "0755"
  tags: traefik

# acme.json must be 0600 or Traefik refuses to start
- name: Create acme.json for certificate storage
  ansible.builtin.file:
    path: "{{ traefik_data_dir }}/acme.json"
    state: touch
    owner: root
    group: root
    mode: "0600"
    modification_time: preserve
    access_time: preserve
  tags: traefik

- name: Deploy Traefik static config
  ansible.builtin.template:
    src: traefik.yml.j2
    dest: "{{ traefik_data_dir }}/traefik.yml"
    owner: root
    group: root
    mode: "0644"
  notify: Restart traefik
  tags: traefik

- name: Deploy Docker Compose file
  ansible.builtin.template:
    src: docker-compose.yml.j2
    dest: "{{ traefik_data_dir }}/docker-compose.yml"
    owner: root
    group: root
    mode: "0644"
  notify: Restart traefik
  tags: traefik

- name: Start Traefik
  community.docker.docker_compose_v2:
    project_src: "{{ traefik_data_dir }}"
    state: present
    pull: missing
  tags: traefik
