---
- name: Create traefik directory
  file:
    path: /opt/traefik
    state: directory
    mode: "0755"

- name: Create traefik data directory
  file:
    path: /opt/traefik/data
    state: directory
    mode: "0755"

- name: Create traefik dynamic config directory
  file:
    path: /opt/traefik/data/dynamic
    state: directory
    mode: "0755"

- name: Create proxy network
  docker_network:
    name: proxy

- name: Create acme.json for Let's Encrypt
  file:
    path: /opt/traefik/data/acme.json
    state: touch
    mode: "0600"

- name: Create traefik config file
  template:
    src: traefik.yml.j2
    dest: /opt/traefik/data/traefik.yml
    mode: "0644"

- name: Create remote services configuration
  template:
    src: remote-services.yml.j2
    dest: /opt/traefik/data/dynamic/remote-services.yml
    mode: "0644"

- name: Create docker-compose file
  template:
    src: docker-compose.yml.j2
    dest: /opt/traefik/docker-compose.yml
    mode: "0644"

- name: Start Traefik
  shell: cd /opt/traefik && docker compose down && docker compose up -d
  args:
    chdir: /opt/traefik
