---
- name: Check if cron is installed
  ansible.builtin.shell:
    cmd: "set -o pipefail && dpkg -l cron 2>/dev/null | grep -q -E '^(i|h)i'"
    executable: /bin/bash
  check_mode: no
  failed_when: False
  changed_when: False
  register: is_cron_installed

- name: "log rotation script"
  ansible.builtin.template:
    src: rotate_logstash_logs.j2
    dest: /etc/cron.daily/rotate_logstash_logs
    owner: root
    group: root
    mode: "0750"
  when: is_cron_installed.rc == 0

- name: "Create a system config directory for systemd overrides"
  ansible.builtin.file:
    path: /etc/systemd/system/logstash.service.d
    state: directory

- name: "disable syslog"
  community.general.ini_file:
    path: /etc/systemd/system/logstash.service.d/override.conf
    section: Service
    option: "{{ item.option }}"
    value: "{{ item.value }}"
    owner: root
    group: root
    mode: "0644"
    create: yes
    no_extra_spaces: yes
    state: "{{ logstash_log_syslog_enabled | bool | ternary('absent','present') }}"
  loop:
    - { option: "StandardOutput", value: "null" }
    - { option: "StandardError",  value: "null" }
  notify:
    - restart logstash