---

- name: APT https transport is enabled
  apt:
    name: apt-transport-https
    state: present
  tags:
  - filebeat
  - 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:
  - filebeat
  - 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:
  - filebeat
  - packages

- name: Filebeat is installed
  apt:
    name: filebeat
    state: present
  tags:
  - filebeat
  - packages

- name: Filebeat service is enabled
  systemd:
    name: filebeat
    enabled: yes

- name: is logstash-plugin available?
  stat:
    path: /usr/share/logstash/bin/logstash-plugin
  check_mode: no
  register: logstash_plugin

- name: is logstash-input-beats installed?
  command: grep logstash-input-beats /usr/share/logstash/Gemfile
  check_mode: no
  register: logstash_plugin_installed
  failed_when: false
  changed_when: false
  when: filebeat_logstash_plugin and logstash_plugin.stat.exists

- name: Logstash plugin is installed
  block:
    - include_role:
        name: evolix/remount-usr

    - name: logstash-plugin install logstash-input-beats
      command: /usr/share/logstash/bin/logstash-plugin install logstash-input-beats
  when:
    - filebeat_logstash_plugin
    - logstash_plugin.stat.exists
    - not logstash_plugin_installed | success

# When we don't use a config template (default)
- block:
  - name: cloud_metadata processor is disabled
    replace:
      dest: /etc/filebeat/filebeat.yml
      regexp: '^(\s+)(- add_cloud_metadata:)'
      replace: '\1# \2'
    notify: restart filebeat
    when: not filebeat_processors_cloud_metadata

  - name: cloud_metadata processor is disabled
    lineinfile:
      dest: /etc/filebeat/filebeat.yml
      line: "  - add_cloud_metadata: ~"
      insert_after: '^processors:'
    notify: restart filebeat
    when: filebeat_processors_cloud_metadata

  - name: Filebeat knows where to find Elasticsearch
    lineinfile:
      dest: /etc/filebeat/filebeat.yml
      regexp: '^  hosts: .*'
      line: "  hosts: [\"{{ filebeat_elasticsearch_hosts | join('\", \"') }}\"]"
      insertafter: "output.elasticsearch:"
    notify: restart filebeat
    when:
      - filebeat_elasticsearch_hosts

  - name: Filebeat protocol for Elasticsearch
    lineinfile:
      dest: /etc/filebeat/filebeat.yml
      regexp: '^  #?protocol: .*'
      line: "  protocol: \"{{ filebeat_elasticsearch_protocol }}\""
      insertafter: "output.elasticsearch:"
    notify: restart filebeat
    when: filebeat_elasticsearch_protocol == "http" or filebeat_elasticsearch_protocol == "https"

  - name: Filebeat auth/username for Elasticsearch are configured
    lineinfile:
      dest: /etc/filebeat/filebeat.yml
      regexp: '{{ item.regexp }}'
      line: '{{ item.line }}'
      insertafter: "output.elasticsearch:"
    with_items:
      - { regexp: '^  #?username: .*', line: '  username: "{{ filebeat_elasticsearch_auth_username }}"' }
      - { regexp: '^  #?password: .*', line: '  password: "{{ filebeat_elasticsearch_auth_password }}"' }
    notify: restart filebeat
    when:
      - filebeat_elasticsearch_auth_username
      - filebeat_elasticsearch_auth_password
  when: not filebeat_use_config_template

- name: Filebeat api_key for Elasticsearch are configured
  lineinfile:
    dest: /etc/filebeat/filebeat.yml
    regexp: '^  #?api_key: .*'
    line: '  api_key: "{{ filebeat_elasticsearch_auth_api_key }}"'
    insertafter: "output.elasticsearch:"
  notify: restart filebeat
  when: filebeat_elasticsearch_auth_api_key

# When we use a config template
- block:
  - name: Configuration is up-to-date
    template:
      src: "{{ item }}"
      dest: /etc/filebeat/filebeat.yml
      force: "{{ filebeat_force_config }}"
    with_first_found:
      - "templates/filebeat/filebeat.{{ inventory_hostname }}.yml.j2"
      - "templates/filebeat/filebeat.{{ host_group }}.yml.j2"
      - "templates/filebeat/filebeat.default.yml.j2"
      - "filebeat.default.yml.j2"
    notify: restart filebeat
    when: filebeat_update_config
  when: filebeat_use_config_template
