---
- name: "Include docker-central-database"
  include_role:
    name: docker-central-database
  when: run_once_docker_mailu is not defined

- name: "Include role nginx-domain-setup for {{ application_id }}"
  include_role:
    name: nginx-domain-setup
  vars:
    domain: "{{ domains[application_id] }}"
    http_port: "{{ ports.localhost.http[application_id] }}"
    nginx_docker_reverse_proxy_extra_configuration: "client_max_body_size 31M;"
  when: run_once_docker_mailu is not defined

- name: "Include the nginx-docker-cert-deploy role"
  include_role:
    name: nginx-docker-cert-deploy
  when: run_once_docker_mailu is not defined

- name: "Copy docker-compose.yml and env file"
  include_tasks: copy-docker-compose-and-env.yml
  when: run_once_docker_mailu is not defined

- name: Flush docker service handlers
  meta: flush_handlers
  when: run_once_docker_mailu is not defined

- name: "Create Mailu accounts and API tokens"
  include_tasks: create-mailu-user-and-token.yml
  vars:
    mailu_compose_dir:        "{{ docker_compose.directories.instance }}"
    mailu_domain:             "{{ primary_domain }}"
    mailu_api_base_url:       "http://127.0.0.1:8080/api/v1"
    mailu_global_api_token:   "{{ applications.mailu.credentials.api_token }}"
    mailu_action:             "{{ item.value.is_admin | default(false) | ternary('admin','user') }}"
    mailu_user:               "{{ item.key }}"
    mailu_password:           "{{ item.value.password }}"
    mailu_token_ip:           "{{ item.value.ip | default('') }}"
  loop:                       "{{ users | dict2items }}"
  loop_control:
    loop_var: item
  when: run_once_docker_mailu is not defined

- name: Run the docker_mailu tasks once
  set_fact:
    run_once_docker_mailu: true
  when: run_once_docker_mailu is not defined