---
- name: Ensure traefik config directory
  file:
    name: "{{ item }}"
    state: directory
  loop:
    - "{{ traefik_config_directory }}"
    - "{{ traefik_config_directory }}/dynamic"
 
- name: Check acme.json
  stat:
    path: "{{ traefik_config_directory }}/acme.json"
  register: acme_json
 
- name: Ensure acme.json
  file:
    name: "{{ traefik_config_directory }}/acme.json"
    state: touch
    mode: 0600
  when: not acme_json.stat.exists
 
- name: Copy traefik config
  template:
    src: files/traefik.yml.j2
    dest: "{{ traefik_config_directory }}/traefik.yml"
  register: copy_traefik_config
 
- name: Bring up traefik
  docker_container:
    name: traefik
    image: traefik:v2.6.1
    # Restart Traefik when the config changed
    recreate: "{{ copy_traefik_config.changed }}"
    networks:
      # Our network from before
      - name: internal
    networks_cli_compatible: yes
    restart_policy: unless-stopped
    # We will pass these from the playbook
    published_ports: "{{ published_ports }}"
    volumes:
      - "{{ traefik_config_directory }}:/etc/traefik"
      - /var/run/docker.sock:/var/run/docker.sock
    labels:
      # Disable Traefik from serving its own container
      traefik.enable: "false"
...
