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

- name: Metricbeat is installed
  ansible.builtin.apt:
    name: metricbeat
    state: "{% if metribeat_upgrade_package %}latest{% else %}present{% endif %}"
  notify: restart metricbeat
  tags:
    - metricbeat
    - packages

- name: Metricbeat service is enabled
  ansible.builtin.systemd:
    name: metricbeat
    enabled: yes
  notify: restart metricbeat

# When we don't use a config template (default)
- block:
  - name: Metricbeat knows where to find Elasticsearch
    ansible.builtin.lineinfile:
      dest: /etc/metricbeat/metricbeat.yml
      regexp: '^  hosts: .*'
      line: "  hosts: [\"{{ metricbeat_elasticsearch_hosts | join('\", \"') }}\"]"
      insertafter: "output.elasticsearch:"
    notify: restart metricbeat
    when: metricbeat_elasticsearch_hosts | length > 0

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

  - name: Metricbeat auth/username for Elasticsearch are configured
    ansible.builtin.lineinfile:
      dest: /etc/metricbeat/metricbeat.yml
      regexp: '{{ item.regexp }}'
      line: '{{ item.line }}'
      insertafter: "output.elasticsearch:"
    loop:
      - { regexp: '^  #?username: .*', line: '  username: "{{ metricbeat_elasticsearch_auth_username }}"' }
      - { regexp: '^  #?password: .*', line: '  password: "{{ metricbeat_elasticsearch_auth_password }}"' }
    notify: restart metricbeat
    when:
      - metricbeat_elasticsearch_auth_username | length > 0
      - metricbeat_elasticsearch_auth_password | length > 0

  - name: Metricbeat api_key for Elasticsearch are configured
    ansible.builtin.lineinfile:
      dest: /etc/metricbeat/metricbeat.yml
      regexp: '^  #?api_key: .*'
      line: '  api_key: "{{ metricbeat_elasticsearch_auth_api_key }}"'
      insertafter: "output.elasticsearch:"
    notify: restart metricbeat
    when: metricbeat_elasticsearch_auth_api_key | length > 0

  - name: disable cloud_metadata
    ansible.builtin.replace:
      dest: /etc/metricbeat/metricbeat.yml
      regexp: '^(\s+)(- add_cloud_metadata:)'
      replace: '\1# \2'
    notify: restart metricbeat
    when: not (metricbeat_processors_cloud_metadata | bool)

  - name: cloud_metadata processor is disabled
    ansible.builtin.lineinfile:
      dest: /etc/metricbeat/metricbeat.yml
      line: "  - add_cloud_metadata: ~"
      insert_after: '^processors:'
    notify: restart metricbeat
    when: metricbeat_processors_cloud_metadata | bool
  when: not (metricbeat_use_config_template | bool)

# When we use a config template
- block:
  - name: Configuration is up-to-date
    ansible.builtin.template:
      src: "{{ item }}"
      dest: /etc/metricbeat/metricbeat.yml
      force: "{{ metricbeat_force_config }}"
    loop: "{{ query('first_found', templates) }}"
    vars:
      templates:
        - "templates/metricbeat/metricbeat.{{ inventory_hostname }}.yml.j2"
        - "templates/metricbeat/metricbeat.{{ host_group | default('all') }}.yml.j2"
        - "templates/metricbeat/metricbeat.default.yml.j2"
        - "templates/metricbeat.default.yml.j2"
      notify: restart metricbeat
    when: metricbeat_update_config | bool
  when: metricbeat_use_config_template | bool
