---
- name: APT sources
  ansible.builtin.include_tasks: apt_sources.yml
  args:
    apply:
      tags:
        - logstash
        - packages

- name: Logstash is installed
  ansible.builtin.apt:
    name: logstash
    state: present
  tags:
    - logstash
    - packages

- name: Logstash service is enabled
  ansible.builtin.systemd:
    name: logstash
    enabled: yes
  tags:
    - logstash

- name: JVM Heap size (min) is set
  ansible.builtin.lineinfile:
    dest: /etc/logstash/jvm.options
    regexp: "^-Xms"
    line: "-Xms{{ logstash_jvm_xms }}"
  tags:
    - logstash
    - config

- name: JVM Heap size (max) is set
  ansible.builtin.lineinfile:
    dest: /etc/logstash/jvm.options
    regexp: "^-Xmx"
    line: "-Xmx{{ logstash_jvm_xmx }}"
  tags:
    - logstash
    - config

- name: Add a configuration
  ansible.builtin.template:
    src: "{{ item }}"
    dest: /etc/logstash/conf.d/logstash.conf
    owner: logstash
    group: logstash
    mode: "0640"
    force: "{{ logstash_config_force | bool }}"
  loop: "{{ query('first_found', templates) }}"
  vars:
    templates:
      - "templates/logstash/logstash.{{ inventory_hostname }}.conf.j2"
      - "templates/logstash/logstash.{{ host_group | default('all') }}.conf.j2"
      - "templates/logstash/logstash.default.conf.j2"
      - "templates/logstash.default.conf.j2"
  register: logstash_template
  tags:
    - logstash
    - config

- ansible.builtin.debug:
    var: logstash_template
    verbosity: 1

- ansible.builtin.include: logs.yml

- ansible.builtin.include: tmpdir.yml
