---
- name: Include loki vars
  include_vars:
    dir: vars

- name: Ensure directories
  file:
    name: "{{ item }}"
    state: directory
  loop:
    - "{{ loki_config_directory }}"
    - "{{ loki_data_directory }}"

- name: Copy config
  template:
    src: files/local-config.yaml.j2
    dest: "{{ loki_config_directory }}/local-config.yaml"
  register: loki_config

- name: Ensure loki container
  docker_container:
    name: loki
    image: grafana/loki:2.5.0
    networks:
      - name: internal
    volumes:
      - "{{ loki_config_directory }}:/etc/loki"
      - "{{ loki_data_directory }}:/loki"
    restart_policy: unless-stopped
    restart: "{{ loki_config.changed }}"
    labels:
      # Traefik will make Loki available on ansible_host:3100 with SSL and basic auth
      traefik.http.routers.loki.rule: "Host(`{{ ansible_host }}`)"
      traefik.http.routers.loki.entrypoints: "loki"
      traefik.http.routers.loki.tls.certresolver: letsEncryptResolver
      traefik.http.routers.loki.tls: "true"
      traefik.http.routers.loki.middlewares: loki-compression,loki-auth
      traefik.http.services.loki.loadbalancer.server.port: "3100"
      traefik.http.middlewares.loki-compression.compress: "true"
      traefik.http.middlewares.loki-auth.basicauth.users: "{{ loki_basic_auth_username }}:{{ loki_basic_auth_htpassword }}"
      traefik.http.middlewares.loki-auth.basicauth.removeheader: "true"
...
