---

- name: APT https transport is enabled
  apt:
    name: apt-transport-https
    state: present
  tags:
  - logstash
  - packages

- name: Elastic GPG key is installed
  apt_key:
    # url: https://artifacts.elastic.co/GPG-KEY-elasticsearch
    data: "{{ lookup('file', 'elasticsearch.key') }}"
    state: present
  tags:
  - logstash
  - packages

- name: Elastic sources list is available
  apt_repository:
    repo: "deb https://artifacts.elastic.co/packages/{{ elastic_stack_version | mandatory }}/apt stable main"
    filename: elastic
    state: present
    update_cache: yes
  tags:
  - logstash
  - packages

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

- name: Logstash service is enabled
  systemd:
    name: logstash
    enabled: yes

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

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

- name: Add a configuration
  template:
    src: "{{ item }}"
    dest: /etc/logstash/conf.d/logstash.conf
    owner: logstash
    group: logstash
    mode: "0640"
    force: yes
  with_first_found:
  - "templates/logstash/logstash.{{ inventory_hostname }}.conf.j2"
  - "templates/logstash/logstash.{{ host_group }}.conf.j2"
  - "templates/logstash/logstash.default.conf.j2"
  - "logstash.default.conf.j2"
  register: logstash_template

- debug:
    var: logstash_template
    verbosity: 1

- include: logs.yml

- include: tmpdir.yml
