---

- name: "include docker-compose role"
  include_role: 
    name: docker-compose

- name: Create {{ domains | get_domain(application_id) }}.conf if LDAP is exposed to internet
  template: 
    src:  "nginx.stream.conf.j2" 
    dest: "{{NGINX.DIRECTORIES.STREAMS}}{{ domains | get_domain(application_id) }}.conf"
  notify: restart openresty
  when: applications | get_app_conf(application_id, 'network.public', True) | bool

- name: Remove {{ domains | get_domain(application_id) }}.conf if LDAP is not exposed to internet
  file:
    path: "{{ NGINX.DIRECTORIES.STREAMS }}{{ domains | get_domain(application_id) }}.conf"
    state: absent
  when: not applications | get_app_conf(application_id, 'network.public', True) | bool

- name: create docker network for LDAP, so that other applications can access it
  community.docker.docker_network:
    name: "{{ openldap_network }}"
    state: present
    ipam_config:
      - subnet: "{{ networks.local[application_id].subnet }}"

- meta: flush_handlers

- name: "Wait for LDAP to be available"
  wait_for:
    host: "127.0.0.1"
    port: "{{ ports.localhost.ldap[application_id] }}"
    delay: 5
    timeout: 120
    state: started

- name: "Reset LDAP Credentials"
  include_tasks: 01_credentials.yml
  when:
    - applications | get_app_conf(application_id, 'network.local', True)
    - applications | get_app_conf(application_id, 'provisioning.credentials', True)

- name: "create directory {{openldap_ldif_host_path}}{{item}}"
  file:
    path: "{{openldap_ldif_host_path}}{{item}}"
    state: directory
    mode: "0755"
  loop: "{{openldap_ldif_types}}"

- name: "Import LDIF Configuration"
  include_tasks: ldifs_creation.yml
  loop:
    - configuration
  loop_control:
    loop_var: folder
  when: applications | get_app_conf(application_id, 'provisioning.configuration', True)

- name: flush LDIF handlers
  meta: flush_handlers

- name: install python-ldap
  community.general.pacman:
    name:
      - python-ldap
    state: present

- name: "Include Schemas (if enabled)"
  include_tasks: 02_schemas.yml
  when: applications | get_app_conf(application_id, 'provisioning.schemas', True)

- name: "Import LDAP Entries (if enabled)"
  include_tasks: 03_users.yml
  when: applications | get_app_conf(application_id, 'provisioning.users', True)

- name: "Import LDIF Data (if enabled)"
  include_tasks: ldifs_creation.yml
  loop:
    - groups
  loop_control:
    loop_var: folder
  when: applications | get_app_conf(application_id, 'provisioning.groups', True)

- name: "Add Objects to all users"
  include_tasks: 04_update.yml
  when: applications | get_app_conf(application_id, 'provisioning.update', True)