Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions roles/elasticsearch_exporter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Ansible Role: elasticsearch exporter

## Description

Deploy prometheus [elasticsearch exporter](https://github.com/prometheus-community/elasticsearch_exporter) using ansible.

Grafana dashboard example: https://github.com/prometheus-community/elasticsearch_exporter/blob/master/examples/grafana/dashboard.json

## Requirements

- Ansible >= 2.9 (It might work on previous versions, but we cannot guarantee it)
- gnu-tar on Mac deployer host (`brew install gnu-tar`)
- Passlib is required when using the basic authentication feature (`pip install passlib[bcrypt]`)

## Role Variables

All variables which can be overridden are stored in [defaults/main.yml](defaults/main.yml) file as well as in [meta/argument_specs.yml](meta/argument_specs.yml).
Please refer to the [collection docs](https://prometheus-community.github.io/ansible/branch/main/elasticsearch_exporter_role.html) for description and default values of the variables.

## Example

### Playbook

Use it in a playbook as follows:
```yaml
- hosts: all
roles:
- prometheus.prometheus.elasticsearch_exporter
```

## Local Testing

The preferred way of locally testing the role is to use Docker and [molecule](https://github.com/ansible-community/molecule) (v3.x). You will have to install Docker on your system. See "Get started" for a Docker package suitable for your system. Running your tests is as simple as executing `molecule test`.

## Contributing

See [contributor guideline](CONTRIBUTING.md).

## Troubleshooting

See [troubleshooting](TROUBLESHOOTING.md).

## License

This project is licensed under MIT License. See [LICENSE](/LICENSE) for more details.
1 change: 1 addition & 0 deletions roles/elasticsearch_exporter/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Troubleshooting
37 changes: 37 additions & 0 deletions roles/elasticsearch_exporter/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
elasticsearch_exporter_version: 1.8.0
elasticsearch_exporter_binary_url: "https://github.com/{{ _elasticsearch_exporter_repo }}/releases/download/v{{ elasticsearch_exporter_version }}/\
elasticsearch_exporter-{{ elasticsearch_exporter_version }}.{{ ansible_facts['system'] | lower }}-{{ _elasticsearch_exporter_go_ansible_arch }}.tar.gz"
elasticsearch_exporter_checksums_url: "https://github.com/{{ _elasticsearch_exporter_repo }}/releases/download/v{{ elasticsearch_exporter_version }}/sha256sums.txt"

elasticsearch_exporter_web_disable_exporter_metrics: false
elasticsearch_exporter_web_listen_address: "0.0.0.0:9114"
elasticsearch_exporter_web_telemetry_path: "/metrics"

elasticsearch_exporter_textfile_dir: "/var/lib/elasticsearch_exporter"

elasticsearch_exporter_tls_server_config: {}

elasticsearch_exporter_http_server_config: {}

elasticsearch_exporter_basic_auth_users: {}

elasticsearch_exporter_enabled_collectors:
- systemd
- textfile:
directory: "{{ elasticsearch_exporter_textfile_dir }}"
# - filesystem:
# ignored-mount-points: "^/(sys|proc|dev)($|/)"
# ignored-fs-types: "^(sys|proc|auto)fs$"

elasticsearch_exporter_disabled_collectors: []

elasticsearch_exporter_binary_install_dir: "/usr/local/bin"
elasticsearch_exporter_system_group: "elastic-exp"
elasticsearch_exporter_system_user: "{{ elasticsearch_exporter_system_group }}"
# systemd cli extra arguments
elasticsearch_exporter_extra_args: []

elasticsearch_exporter_config_dir: "/etc/elasticsearch_exporter"
# Local path to stash the archive and its extraction
elasticsearch_exporter_local_cache_path: "/tmp/elasticsearch_exporter-{{ ansible_facts['system'] | lower }}-{{ _elasticsearch_exporter_go_ansible_arch }}/{{ elasticsearch_exporter_version }}"
10 changes: 10 additions & 0 deletions roles/elasticsearch_exporter/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Restart elasticsearch_exporter
listen: "restart elasticsearch_exporter"
become: true
ansible.builtin.systemd:
daemon_reload: true
name: elasticsearch_exporter
state: restarted
when:
- not ansible_check_mode
89 changes: 89 additions & 0 deletions roles/elasticsearch_exporter/meta/argument_specs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
# yamllint disable rule:line-length
argument_specs:
main:
short_description: "Prometheus Elasticsearch Exporter"
description:
- "Deploy prometheus L(elasticsearch exporter,https://github.com/prometheus-community/elasticsearch_exporter) using ansible"
author:
- "Prometheus Community"
options:
elasticsearch_exporter_version:
description: "Elasticsearch exporter package version. Also accepts latest as parameter."
default: "1.8.0"
elasticsearch_exporter_binary_url:
description: "URL of the elasticsearch exporter binaries .tar.gz file"
default: "https://github.com/{{ _elasticsearch_exporter_repo }}/releases/download/v{{ elasticsearch_exporter_version }}/elasticsearch_exporter-{{ elasticsearch_exporter_version }}.{{ ansible_facts['system'] | lower }}-{{ _elasticsearch_exporter_go_ansible_arch }}.tar.gz"
elasticsearch_exporter_checksums_url:
description: "URL of the elasticsearch exporter checksums file"
default: "https://github.com/{{ _elasticsearch_exporter_repo }}/releases/download/v{{ elasticsearch_exporter_version }}/sha256sums.txt"
elasticsearch_exporter_web_disable_exporter_metrics:
description: "Exclude metrics about the exporter itself (promhttp_*, process_*, go_*)."
type: bool
default: false
elasticsearch_exporter_web_listen_address:
description: "Address on which elasticsearch exporter will listen"
default: "0.0.0.0:9114"
elasticsearch_exporter_web_telemetry_path:
description: "Path under which to expose metrics"
default: "/metrics"
elasticsearch_exporter_enabled_collectors:
description:
- "List of dicts defining additionally enabled collectors and their configuration."
- "It adds collectors to L(those enabled by default,https://github.com/prometheus/elasticsearch_exporter#enabled-by-default)."
type: "list"
default:
- systemd
- textfile:
directory: "{{ elasticsearch_exporter_textfile_dir }}"
elasticsearch_exporter_disabled_collectors:
description:
- "List of disabled collectors."
- "By default elasticsearch_exporter disables collectors listed L(here,https://github.com/prometheus/elasticsearch_exporter#disabled-by-default)."
type: "list"
elements: "str"
elasticsearch_exporter_textfile_dir:
description:
- "Directory used by the L(Textfile Collector,https://github.com/prometheus/elasticsearch_exporter#textfile-collector)."
- "To get permissions to write metrics in this directory, users must be in C(elastic-exp) system group."
- "B(Note:) More information in TROUBLESHOOTING.md guide."
default: "/var/lib/elasticsearch_exporter"
elasticsearch_exporter_tls_server_config:
description:
- "Configuration for TLS authentication."
- "Keys and values are the same as in L(elasticsearch_exporter docs,https://prometheus.io/docs/prometheus/latest/configuration/https/)."
type: "dict"
elasticsearch_exporter_http_server_config:
description:
- "Config for HTTP/2 support."
- "Keys and values are the same as in L(elasticsearch_exporter docs,https://prometheus.io/docs/prometheus/latest/configuration/https/)."
type: "dict"
elasticsearch_exporter_basic_auth_users:
description: "Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt."
type: "dict"
elasticsearch_exporter_binary_install_dir:
description:
- "I(Advanced)"
- "Directory to install elasticsearch_exporter binary"
default: "/usr/local/bin"
elasticsearch_exporter_system_group:
description:
- "I(Advanced)"
- "System group for elasticsearch exporter"
default: "elastic-exp"
elasticsearch_exporter_system_user:
description:
- "I(Advanced)"
- "Elasticsearch exporter user"
default: "elastic-exp"
elasticsearch_exporter_extra_args:
description:
- "I(Advanced)"
- "Elasticsearch exporter cli extra arguments to be added to systemd unit"
default: []
elasticsearch_exporter_local_cache_path:
description: "Local path to stash the archive and its extraction"
default: "/tmp/elasticsearch_exporter-{{ ansible_facts['system'] | lower }}-{{ _elasticsearch_exporter_go_ansible_arch }}/{{ elasticsearch_exporter_version }}"
elasticsearch_exporter_config_dir:
description: "Path to directory with elasticsearch_exporter configuration"
default: "/etc/elasticsearch_exporter"
25 changes: 25 additions & 0 deletions roles/elasticsearch_exporter/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
galaxy_info:
author: "Prometheus Community"
description: "Prometheus Elasticsearch Exporter"
license: "Apache"
min_ansible_version: "2.9"
platforms:
- name: "Ubuntu"
versions:
- "focal"
- "jammy"
- "noble"
- name: "Debian"
versions:
- "bullseye"
- name: "EL"
versions:
- "8"
- "9"
galaxy_tags:
- "monitoring"
- "prometheus"
- "exporter"
- "metrics"
- "system"
26 changes: 26 additions & 0 deletions roles/elasticsearch_exporter/molecule/alternative/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
provisioner:
playbooks:
prepare: "${MOLECULE_PROJECT_DIRECTORY}/../../.config/molecule/alternative/prepare.yml"
inventory:
group_vars:
all:
elasticsearch_exporter_local_cache_path: "/tmp/elasticsearch_exporter-linux-amd64"
elasticsearch_exporter_web_listen_address:
- '127.0.0.1:8080'
- '127.0.1.1:8080'
elasticsearch_exporter_textfile_dir: ""
elasticsearch_exporter_enabled_collectors:
- entropy
elasticsearch_exporter_disabled_collectors:
- diskstats
elasticsearch_exporter_tls_server_config:
cert_file: /etc/elasticsearch_exporter/tls.cert
key_file: /etc/elasticsearch_exporter/tls.key
elasticsearch_exporter_http_server_config:
http2: true
elasticsearch_exporter_basic_auth_users:
randomuser: examplepassword
elasticsearch_exporter_version: 1.8.0
elasticsearch_exporter_binary_url: "https://github.com/prometheus-community/elasticsearch_exporter/releases/download/v{{\
\ elasticsearch_exporter_version }}/elasticsearch_exporter-{{ elasticsearch_exporter_version }}.linux-amd64.tar.gz"
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from testinfra_helpers import get_target_hosts
import pytest

testinfra_hosts = get_target_hosts()


def test_directories(host):
dirs = [
"/var/lib/elasticsearch_exporter"
]
for dir in dirs:
d = host.file(dir)
assert not d.exists


def test_service(host):
s = host.service("elasticsearch_exporter")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u elasticsearch_exporter --since "1 hour ago"')
print("\n==== journalctl -u elasticsearch_exporter Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error


def test_protecthome_property(host):
s = host.service("elasticsearch_exporter")
p = s.systemd_properties
assert p.get("ProtectHome") == "yes"


@pytest.mark.parametrize("sockets", [
"tcp://127.0.0.1:8080",
"tcp://127.0.1.1:8080",
])
def test_socket(host, sockets):
assert host.socket(sockets).is_listening
6 changes: 6 additions & 0 deletions roles/elasticsearch_exporter/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
provisioner:
inventory:
group_vars:
all:
elasticsearch_exporter_web_listen_address: "127.0.0.1:9114"
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from testinfra_helpers import get_target_hosts

testinfra_hosts = get_target_hosts()


def test_directories(host):
dirs = [
"/var/lib/elasticsearch_exporter"
]
for dir in dirs:
d = host.file(dir)
assert d.is_directory
assert d.exists


def test_files(host):
files = [
"/etc/systemd/system/elasticsearch_exporter.service",
"/usr/local/bin/elasticsearch_exporter"
]
for file in files:
f = host.file(file)
assert f.exists
assert f.is_file


def test_permissions_didnt_change(host):
dirs = [
"/etc",
"/root",
"/usr",
"/var"
]
for file in dirs:
f = host.file(file)
assert f.exists
assert f.is_directory
assert f.user == "root"
assert f.group == "root"


def test_user(host):
assert host.group("elastic-exp").exists
assert "elastic-exp" in host.user("elastic-exp").groups
assert host.user("elastic-exp").shell == "/usr/sbin/nologin"


def test_service(host):
s = host.service("elasticsearch_exporter")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u elasticsearch_exporter --since "1 hour ago"')
print("\n==== journalctl -u elasticsearch_exporter Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error


def test_protecthome_property(host):
s = host.service("elasticsearch_exporter")
p = s.systemd_properties
assert p.get("ProtectHome") == "yes"


def test_socket(host):
sockets = [
"tcp://127.0.0.1:9114"
]
for socket in sockets:
s = host.socket(socket)
assert s.is_listening
7 changes: 7 additions & 0 deletions roles/elasticsearch_exporter/molecule/latest/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
provisioner:
inventory:
group_vars:
all:
elasticsearch_exporter_version: latest
elasticsearch_exporter_textfile_dir: /home/elasticsearch_exporter
Loading