#####################################
### someone's ansible provisioner ###
#####################################
# Part of: https://git.somenet.org/root/pub/somesible.git
# 2017-2026 by someone <someone@somenet.org>
#
# Sympa is broken in debian, in that it cannot be installed with dbconfig-no-thanks.
# To work around this, we have to first debconf-preseed some config values and deploy the config file before AND after installing the sympa package.
# todo: move to sertvices?
---
- name: create sympa user
  user:
    name: "sympa"
    home: "/var/lib/sympa"
    system: yes
    state: present


- name: create /etc/sympa
  file:
    path: "/etc/sympa"
    state: directory
    mode: 0755
    owner: "sympa"
    group: "sympa"

- name: create /etc/sympa/sympa
  file:
    path: "/etc/sympa/sympa"
    state: directory
    mode: 0755
    owner: "sympa"
    group: "sympa"


- name: copy sympa.debconf
  copy:
    src: "{{item}}"
    dest: "/etc/sympa/sympa/sympa.debconf"
    mode: 0644
    owner: "sympa"
    group: "sympa"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/sympa.debconf"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/sympa.debconf"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/sympa.debconf"
    - "default/sympa.debconf"
  notify: restart sympa.service


- name: preseed sympa.debconf
  shell: 'debconf-set-selections /etc/sympa/sympa/sympa.debconf'


- name: copy sympa config
  copy:
    src: "{{item}}"
    dest: "/etc/sympa/sympa/sympa.conf"
    mode: 0644
    owner: "sympa"
    group: "sympa"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/sympa.conf"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/sympa.conf"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/sympa.conf"
    - "default/sympa.conf"
  notify: restart sympa.service


- name: install sympa
  apt:
    pkg:
    - sympa
    state: present
    policy_rc_d: 101
  tags: "online"
  ignore_errors: "{{ignore_online_errors | bool}}"
  register: result


- name: copy sympa config again
  copy:
    src: "{{item}}"
    dest: "/etc/sympa/sympa/sympa.conf"
    mode: 0644
    owner: "sympa"
    group: "sympa"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/sympa.conf"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/sympa.conf"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/sympa.conf"
    - "default/sympa.conf"
  notify: restart sympa.service
  when: result is defined and result.changed


- name: copy key_dkim.private
  copy:
    src: "{{item}}"
    dest: "/etc/sympa/sympa/key_dkim.private"
    mode: 0640
    owner: "sympa"
    group: "sympa"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/key_dkim.private"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/key_dkim.private"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/key_dkim.private"
    - "default/key_dkim.private"
  notify: restart sympa.service


- name: copy list_aliases.tt2
  copy:
    src: "{{item}}"
    dest: "/etc/sympa/list_aliases.tt2"
    mode: 0644
    owner: "sympa"
    group: "sympa"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/list_aliases.tt2"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/list_aliases.tt2"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/list_aliases.tt2"
    - "default/list_aliases.tt2"
  notify: restart sympa.service


- name: include vars_nginx_vhost_custom
  include_vars:
    file: "{{item}}"
    name: vars_nginx_vhost_custom
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/{{sympa_main_host_name}}-vars_nginx_vhost_custom.yml"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/{{sympa_main_host_name}}-vars_nginx_vhost_custom.yml"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/{{sympa_main_host_name}}-vars_nginx_vhost_custom.yml"
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/vars_nginx_vhost_custom.yml"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/vars_nginx_vhost_custom.yml"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/vars_nginx_vhost_custom.yml"
    - "default/vars_nginx_vhost_custom.yml"


- name: configure vhost for "{{sympa_main_host_name}}"
  include_role:
    name: server/nginx/vhost-unified
  vars:
    vhost_type: "custom"
    vhost_name: "{{sympa_main_host_name}}"
    vhost_custom:
      vhost_custom_pre_server: "{{vars_nginx_vhost_custom.vhost_custom_pre_server}}"
      vhost_custom: "{{vars_nginx_vhost_custom.vhost_custom}}"


- name: enable and start sympa.service
  include_role: name="base/systemd/enable-and-start"
  vars:
    service_name: sympa.service

- name: enable and start wwsympa.socket
  include_role: name="base/systemd/enable-and-start"
  vars:
    service_name: wwsympa.socket
