---

- name: Install packages
  package: name={{ ldap2pg_packages }}
  tags: pg

- when: ansible_os_family == 'RedHat' and ansible_distribution_major_version is version('8', '<')
  block:
    - name: Check if ldap2Pg is built with python 2 or 3
      stat: path=/usr/lib/python3.6/site-packages/ldap2pg/ldap.py
      register: ldap2pg_py

    - name: Set ldap.py path
      set_fact: ldap_py_ver={{ ldap2pg_py.stat.exists | ternary('3.6', '2.7') }}

    - name: Patch ldap2pg to specify global cacert
      patch: src=ldap2pg_cacert.diff dest=/usr/lib/python{{ ldap_py_ver }}/site-packages/ldap2pg/ldap.py
  tags: pg

- name: Install systemd unit
  template: src=ldap2pg.{{ item }}.j2 dest=/etc/systemd/system/ldap2pg.{{ item }}
  loop:
    - service
    - timer
  notify:
    - restart ldap2pg.service
    - restart ldap2pg.timer
  register: ldap2pg_units
  tags: pg

- name: Reload systemd
  systemd: daemon_reload=True
  when: ldap2pg_units.results | selectattr('changed','equalto',True) | list | length > 0
  tags: pg
