---
- name: Ensures traefik dir exists
  file:
    path: "{{ traefik_install_dir }}"
    state: directory
    owner: "{{ docker_www_user }}"
    group: "{{ docker_group }}"

- name: Ensures acme.json file exists
  file:
    path: "{{ traefik_install_dir }}/acme.json"
    state: touch
    mode: 0600

- name: Ensures traefik.log file exists
  file:
    path: "{{ traefik_install_dir }}/traefik.log"
    state: touch
    mode: 0600

- name: Adding traefik.toml file
  template:
    src: templates/traefik.toml.j2
    dest: "{{ traefik_install_dir }}/traefik.toml"
    mode: 0600

- name: Create web network in docker
  docker_network:
    name: "{{ traefik_docker_network }}"

- name: Clean up Traefik container
  docker_container:
    name: traefik
    state: absent
  when: traefik_recreate == "true"

- name: Run traefik container
  docker_container:
    name: traefik
    image: traefik:2.2
    restart_policy: unless-stopped
    networks_cli_compatible: yes
    networks:
      - name: "{{ traefik_docker_network }}"
      - name: bridge
    ports:
      - "80:80"
      - "443:443"
      - "1194:1194/udp"
    volumes:
      - "{{ traefik_install_dir }}/traefik.toml:/etc/traefik/traefik.toml"
      - "{{ traefik_install_dir }}/acme.json:/acme.json"
      - "{{ traefik_install_dir }}/traefik.log:/traefik.log"
      - /var/run/docker.sock:/var/run/docker.sock
    labels:
      traefik.http.routers.api.entrypoints: "websecure"
      traefik.http.routers.api.rule: "Host(`{{ ansible_fqdn }}`)"
      traefik.http.routers.api.service: "api@internal"
      traefik.http.routers.api.middlewares: "api-auth"
      traefik.http.routers.api.tls: "true"
      traefik.http.routers.api.tls.certresolver: "le"
      traefik.http.middlewares.redirect-to-https.redirectscheme.scheme: "https"