---

# TODO: voir comment faire des backups initiaux des fichiers

# RSyslog
- name: Copy rsyslog evolinux defaut conf
  ansible.builtin.copy:
    src: logs/rsyslog.d/10-evolinux-default.conf
    dest: /etc/rsyslog.d/10-evolinux-default.conf
    mode: "0644"
  notify: restart rsyslog
  when: evolinux_logs_rsyslog_conf | bool

# Logrotate
- name: Disable logrotate default conf
  ansible.builtin.command:
    cmd: mv /etc/logrotate.d/rsyslog /etc/logrotate.d/rsyslog.disabled
  args:
    removes: /etc/logrotate.d/rsyslog
    creates: /etc/logrotate.d/rsyslog.disabled
  notify: restart rsyslog
  when: evolinux_logs_disable_logrotate_rsyslog | bool

- name: Copy many logrotate files
  ansible.builtin.copy:
    src: logs/logrotate.d/
    dest: /etc/logrotate.d/
  when: evolinux_logs_logrotate_confs | bool

- name: Copy rsyslog logrotate file
  ansible.builtin.template:
    src: logs/zsyslog.j2
    dest: /etc/logrotate.d/zsyslog
  when: evolinux_logs_logrotate_confs | bool

- name: Configure logrotate.conf default rotate value
  ansible.builtin.replace:
    dest: /etc/logrotate.conf
    regexp: "rotate [0-9]+"
    replace: "rotate 12"
  when: evolinux_logs_default_rotate | bool

- name: Enable logrotate.conf dateext option
  ansible.builtin.lineinfile:
    dest: /etc/logrotate.conf
    line: "dateext"
    regexp: "^#?\\s*dateext"
  when: evolinux_logs_default_dateext | bool

- name: Enable logrotate.conf dateformat option
  ansible.builtin.lineinfile:
    dest: /etc/logrotate.conf
    line: "dateformat {{ evolinux_logrotate_dateformat | mandatory }}"
    regexp: "^#?\\s*dateformat.*"
    insertafter: 'dateext'
  when: evolinux_logs_default_dateext | bool

- name: Disable logrotate.conf dateyesterday option
  ansible.builtin.lineinfile:
    dest: /etc/logrotate.conf
    line: "# dateyesterday"
    regexp: "^\\s*dateyesterday"
    insertafter: 'dateext'
  when: evolinux_logs_default_dateext | bool

# Logcheck
- name: Check if journald.logfiles exists
  stat:
    path: /etc/logcheck/logcheck.logfiles.d/journal.logfiles
  register: _logcheck_journald_logfiles

- name: Disable logcheck monitoring of journald
  ansible.builtin.lineinfile:
    dest: /etc/logcheck/logcheck.logfiles.d/journal.logfiles
    line: "#journal"
    regexp: "^journal"
  when:
    - _logcheck_journald_logfiles.stat.exists
    - evolinux_logs_disable_logcheck_journald | bool

# Journald
- name: /etc/systemd/journald.conf.d/ is present
  ansible.builtin.file:
    path: /etc/systemd/journald.conf.d/
    state: directory
    mode: "0755"
  when: evolinux_logs_journald_conf | bool

- name: Copy journald.conf
  ansible.builtin.copy:
    src: logs/journald.conf
    dest: /etc/systemd/journald.conf.d/00-evolinux-default.conf
    mode: "0644"
  notify: restart systemd-journald
  when: evolinux_logs_journald_conf | bool

- ansible.builtin.meta: flush_handlers
