---
# file: roles/filebeat/tasks/modules.yml

- name: "make sure the system module is enabled"
  ansible.builtin.command:
    cmd: "filebeat modules enable system"
    creates: "/etc/filebeat/modules.d/system.yml"
  notify: restart filebeat
  tags:
    - filebeat
    - filebeat_modules

- name: "make sure the additional modules are enabled, if defined"
  ansible.builtin.command:
    cmd: "filebeat modules enable {{ item }}"
    creates: "/etc/filebeat/modules.d/{{ item }}.yml"
  loop: "{{ filebeat_modules_list }}"
  when: filebeat_modules_list is defined
  notify: restart filebeat
  tags:
    - filebeat
    - filebeat_modules

- name: "change module name for elastic_major_version == '6.x'"
  ansible.builtin.set_fact:
    filebeat_modules_detection:
      - module: apache2
        path: /etc/apache2/apache2.conf
      - module: elasticsearch
        path: /etc/elasticsearch/elasticsearch.yml
      - module: haproxy
        path: /etc/haproxy/haproxy.cfg
      - module: kibana
        path: /etc/kibana/kibana.yml
      - module: logstash
        path: /etc/logstash/logstash.yml
  when: elastic_major_version == "6.x"
  tags:
    - filebeat
    - filebeat_modules

- name: "detect installed software to enable correct modules"
  ansible.builtin.stat:
    path: "{{ item['path'] }}"
  loop: "{{ filebeat_modules_detection }}"
  register: detection
  tags:
    - filebeat
    - filebeat_modules

- name: "enable module for installed softwares"
  ansible.builtin.command:
    cmd: "filebeat modules enable {{ item.item.module }}"
    creates: "/etc/filebeat/modules.d/{{ item.item.module }}.yml"
  loop: "{{ detection.results }}"
  when: item.stat.exists
  notify: restart filebeat
  tags:
    - filebeat
    - filebeat_modules

- name: "disable module for haproxy (this is temporary step to get rid of haproxy module, BB-673 related)"
  ansible.builtin.command:
    cmd: "filebeat modules disable haproxy"
    creates: "/etc/filebeat/modules.d/haproxy.yml.disabled"
  notify: restart filebeat
  tags:
    - filebeat
    - filebeat_modules

- name: "module configuration: /etc/filebeat/modules.d/[module].yml"
  ansible.builtin.copy:
    src: "{{ item.item.module }}.yml"
    dest: "/etc/filebeat/modules.d/{{ item.item.module }}.yml"
  loop: "{{ detection.results }}"
  when:
    - item.stat.exists
    - item.item.module == "apache" or item.item.module == "kibana" or item.item.module == "haproxy"
  notify: restart filebeat
  tags:
    - filebeat
    - filebeat_modules
