This guide provides a full Ansible playbook to install Metricbeat with distro-aware package handling and service management for Debian 10+, Ubuntu LTS, and RHEL 9+ compatible systems.
- name: Install Metricbeat
hosts: metricbeat
become: true
vars:
elastic_version: "8.11.0"
elasticsearch_hosts:
- "https://elasticsearch:9200"
elasticsearch_username: "elastic"
elasticsearch_password: "changeme"
tasks:
- name: Install required packages (Debian/Ubuntu)
apt:
name:
- apt-transport-https
- gnupg
- curl
state: present
update_cache: true
when: ansible_os_family == "Debian"
- name: Add Elastic GPG key (Debian/Ubuntu)
apt_key:
url: https://artifacts.elastic.co/GPG-KEY-elasticsearch
state: present
when: ansible_os_family == "Debian"
- name: Add Elastic repository (Debian/Ubuntu)
apt_repository:
repo: "deb https://artifacts.elastic.co/packages/8.x/apt stable main"
state: present
filename: elastic-8.x
when: ansible_os_family == "Debian"
- name: Install required packages (RHEL)
dnf:
name:
- yum-utils
- gnupg
state: present
when: ansible_os_family == "RedHat"
- name: Add Elastic repository (RHEL)
yum_repository:
name: elastic-8.x
description: Elastic repository for 8.x packages
baseurl: https://artifacts.elastic.co/packages/8.x/yum
gpgcheck: 1
gpgkey: https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled: 1
when: ansible_os_family == "RedHat"
- name: Install Metricbeat
package:
name: metricbeat
state: present
notify: restart metricbeat
- name: Configure Metricbeat output
lineinfile:
path: /etc/metricbeat/metricbeat.yml
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
loop:
- { regexp: '^#?output.elasticsearch:', line: 'output.elasticsearch:' }
- { regexp: '^#? hosts:', line: ' hosts: {{ elasticsearch_hosts }}' }
- { regexp: '^#? username:', line: ' username: {{ elasticsearch_username }}' }
- { regexp: '^#? password:', line: ' password: {{ elasticsearch_password }}' }
notify: restart metricbeat
- name: Enable system module
command: metricbeat modules enable system
notify: restart metricbeat
- name: Enable and start Metricbeat service
service:
name: metricbeat
state: started
enabled: true
handlers:
- name: restart metricbeat
service:
name: metricbeat
state: restarted
metricbeat with filebeat, heartbeat, auditbeat, etc.Any questions?
Feel free to contact us. Find all contact information on our contact page.