---
- name: Update Filebeat
  hosts: all
  become: yes
  become_user: root
  gather_facts: no

  tasks:
    - name: Update Apt cache
      apt:
        update_cache: yes
      ignore_errors: yes

    - name: Add Elasticsearch Repository Key
      apt_key:
        url: https://artifacts.elastic.co/GPG-KEY-elasticsearch
        state: present

    - name: Add Elasticsearch Repository
      apt_repository:
        repo: deb https://artifacts.elastic.co/packages/7.x/apt stable main
        state: present
        update_cache: true

    - name: Update Filebeat
      apt:
        name: filebeat
        state: latest
      register: result
      until: result.failed == false
      retries: 5
      delay: 120

    - name: Restart Filebeat Daemon
      systemd:
        name: filebeat
        daemon_reload: true
        enabled: yes
        state: restarted
