- name: Load memberof module from file in OpenLDAP container
  shell: >
    docker exec -i {{ openldap_name }} ldapmodify -Y EXTERNAL -H ldapi:/// -f {{openldap_ldif_docker_path}}configuration/01_member_of_configuration.ldif
  listen: 
    - "Import configuration LDIF files"
    - "Import all LDIF files"
  # @todo Remove the following ignore errors when setting up a new server
  # Just here because debugging would take to much time
  ignore_errors: true

- name: Refint Module Activation for OpenLDAP
  shell: >
    docker exec -i {{ openldap_name }} ldapadd -Y EXTERNAL -H ldapi:/// -f {{openldap_ldif_docker_path}}configuration/02_member_of_configuration.ldif
  listen: 
    - "Import configuration LDIF files"
    - "Import all LDIF files"
  register: ldapadd_result
  failed_when: ldapadd_result.rc not in [0, 68]
  # @todo Remove the following ignore errors when setting up a new server
  # Just here because debugging would take to much time
  ignore_errors: true

- name: "Import schemas"
  shell: >
    docker exec -i {{ openldap_name }} ldapadd -Y EXTERNAL -H ldapi:/// -f "{{openldap_ldif_docker_path}}schema/{{ item | basename | regex_replace('\.j2$', '') }}"
  register: ldapadd_result
  changed_when: "'adding new entry' in ldapadd_result.stdout"
  failed_when: ldapadd_result.rc not in [0, 80]
  listen:
    - "Import schema LDIF files"
    - "Import all LDIF files"
  loop: "{{ lookup('fileglob', role_path ~ '/templates/ldif/schema/*.j2', wantlist=True) }}"

- name: Refint Overlay Configuration for OpenLDAP
  shell: >
    docker exec -i {{ openldap_name }} ldapmodify -Y EXTERNAL -H ldapi:/// -f {{openldap_ldif_docker_path}}configuration/03_member_of_configuration.ldif
  listen:
    - "Import configuration LDIF files"
    - "Import all LDIF files"
  register: ldapadd_result
  failed_when: ldapadd_result.rc not in [0, 68]
  # @todo Remove the following ignore errors when setting up a new server
  # Just here because debugging would take to much time
  ignore_errors: true

- name: "Import users, groups, etc. to LDAP"
  shell: >
    docker exec -i {{ openldap_name }} ldapadd -x -D "{{ldap.dn.administrator.data}}" -w "{{ldap.bind_credential}}" -c -f "{{openldap_ldif_docker_path}}groups/{{ item | basename | regex_replace('\.j2$', '') }}"
  register: ldapadd_result
  changed_when: "'adding new entry' in ldapadd_result.stdout"
  failed_when: ldapadd_result.rc not in [0, 20, 68, 65]
  listen:
    - "Import groups LDIF files"
    - "Import all LDIF files"
  loop: "{{ query('fileglob', role_path ~ '/templates/ldif/groups/*.j2') | sort }}"