#####################################
### someone's ansible provisioner ###
#####################################
# Part of: https://git.somenet.org/root/pub/somesible.git
# 2017-2025 by someone <someone@somenet.org>
#
---
- name: install cyrus
  apt:
    pkg:
    - cyrus-admin
    - cyrus-clients
    - cyrus-imapd
    state: present
    policy_rc_d: 101
  tags: "online"
  ignore_errors: "{{ignore_online_errors | bool}}"


- name: add cyrus user to group ssl-cert
  user:
    name: "cyrus"
    groups: "ssl-cert"
    append: yes
    createhome: no
    state: present


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


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


- name: copy default.sieve
  copy:
    src: "{{item}}"
    dest: "/var/lib/cyrus/default.sieve"
    mode: 0600
    owner: "cyrus"
    group: "mail"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/default.sieve"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/default.sieve"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/default.sieve"
    - "default/default.sieve"
  notify: restart cyrus-imapd.service


- name: copy fail2ban.jail.d.cyrus.conf to /etc/fail2ban/jail.d/
  copy:
    src: "{{item}}"
    dest: "/etc/fail2ban/jail.d/cyrus.conf"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/fail2ban.jail.d.cyrus.conf"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/fail2ban.jail.d.cyrus.conf"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/fail2ban.jail.d.cyrus.conf"
    - "default/fail2ban.jail.d.cyrus.conf"
  notify: reload fail2ban.service


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