Skip to content

Commit

Permalink
feat: Add pushgateway role
Browse files Browse the repository at this point in the history
Add role to manage the Prometheus Pushgateway.

Signed-off-by: SuperQ <[email protected]>
  • Loading branch information
SuperQ committed Jul 11, 2023
1 parent 1b5a051 commit c676915
Show file tree
Hide file tree
Showing 28 changed files with 860 additions and 0 deletions.
83 changes: 83 additions & 0 deletions roles/pushgateway/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<p><img src="https://www.circonus.com/wp-content/uploads/2015/03/sol-icon-itOps.png" alt="graph logo" title="graph" align="right" height="60" /></p>

# Ansible Role: pushgateway

## Description

Deploy prometheus [pushgateway](https://github.com/prometheus/pushgateway) using ansible.

## 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/pushgateway_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.pushgateway
```
### TLS config
Before running pushgateway role, the user needs to provision their own certificate and key.
```yaml
- hosts: all
pre_tasks:
- name: Create pushgateway cert dir
file:
path: "/etc/pushgateway"
state: directory
owner: root
group: root

- name: Create cert and key
openssl_certificate:
path: /etc/pushgateway/tls.cert
csr_path: /etc/pushgateway/tls.csr
privatekey_path: /etc/pushgateway/tls.key
provider: selfsigned
roles:
- prometheus.prometheus.pushgateway
vars:
pushgateway_tls_server_config:
cert_file: /etc/pushgateway/tls.cert
key_file: /etc/pushgateway/tls.key
pushgateway_basic_auth_users:
randomuser: examplepassword
```
### Demo site
We provide an example site that demonstrates a full monitoring solution based on prometheus and grafana. The repository with code and links to running instances is [available on github](https://github.com/prometheus/demo-site) and the site is hosted on [DigitalOcean](https://digitalocean.com).
## 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`.

## Continuous Integration

Combining molecule and circle CI allows us to test how new PRs will behave when used with multiple ansible versions and multiple operating systems. This also allows use to create test scenarios for different role configurations. As a result we have quite a large test matrix which can take more time than local testing, so please be patient.

## 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.
20 changes: 20 additions & 0 deletions roles/pushgateway/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Troubleshooting

## Bad requests (HTTP 400)

This role downloads checksums from the Github project to verify the integrity of artifacts installed on your servers. When downloading the checksums, a "bad request" error might occur.

This happens in environments which (knowningly or unknowling) use the [netrc mechanism](https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html) to auto-login into servers.

Unless netrc is needed by your playbook and ansible roles, please unset the var like so:

```
$ NETRC= ansible-playbook ...
```

Or:

```
$ export NETRC=
$ ansible-playbook ...
```
20 changes: 20 additions & 0 deletions roles/pushgateway/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
pushgateway_version: 1.6.0
pushgateway_binary_local_dir: ""
pushgateway_binary_url: "https://github.com/{{ _pushgateway_repo }}/releases/download/v{{ pushgateway_version }}/\
pushgateway-{{ pushgateway_version }}.linux-{{ go_arch }}.tar.gz"
pushgateway_checksums_url: "https://github.com/{{ _pushgateway_repo }}/releases/download/v{{ pushgateway_version }}/sha256sums.txt"
pushgateway_skip_install: false

pushgateway_web_listen_address: "0.0.0.0:9091"
pushgateway_web_telemetry_path: "/metrics"

pushgateway_tls_server_config: {}

pushgateway_http_server_config: {}

pushgateway_basic_auth_users: {}

pushgateway_binary_install_dir: "/usr/local/bin"
pushgateway_system_group: "pushgateway"
pushgateway_system_user: "{{ pushgateway_system_group }}"
10 changes: 10 additions & 0 deletions roles/pushgateway/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Restart pushgateway
listen: "restart pushgateway"
become: true
ansible.builtin.systemd:
daemon_reload: true
name: pushgateway
state: restarted
when:
- not ansible_check_mode
62 changes: 62 additions & 0 deletions roles/pushgateway/meta/argument_specs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
# yamllint disable rule:line-length
argument_specs:
main:
short_description: "Prometheus Pushgateway"
description:
- "Deploy prometheus L(Pushgateway,https://github.com/prometheus/pushgateway) using ansible"
author:
- "Prometheus Community"
options:
pushgateway_version:
description: "Pushgateway package version. Also accepts latest as parameter."
default: "1.1.2"
pushgateway_skip_install:
description: "Pushgateway installation tasks gets skipped when set to true."
type: bool
default: false
pushgateway_binary_local_dir:
description:
- "Enables the use of local packages instead of those distributed on github."
- "The parameter may be set to a directory where the C(pushgateway) binary is stored on the host where ansible is run."
- "This overrides the I(pushgateway_version) parameter"
pushgateway_binary_url:
description: "URL of the Pushgateway binaries .tar.gz file"
default: "https://github.com/{{ _pushgateway_repo }}/releases/download/v{{ pushgateway_version }}/pushgateway-{{ pushgateway_version }}.linux-{{ go_arch }}.tar.gz"
pushgateway_checksums_url:
description: "URL of the Pushgateway checksums file"
default: "https://github.com/{{ _pushgateway_repo }}/releases/download/v{{ pushgateway_version }}/sha256sums.txt"
pushgateway_web_listen_address:
description: "Address on which Pushgateway will listen"
default: "0.0.0.0:9091"
pushgateway_web_telemetry_path:
description: "Path under which to expose metrics"
default: "/metrics"
pushgateway_tls_server_config:
description:
- "Configuration for TLS authentication."
- "Keys and values are the same as in L(pushgateway docs,https://github.com/prometheus/pushgateway/blob/master/https/README.md#sample-config)."
type: "dict"
pushgateway_http_server_config:
description:
- "Config for HTTP/2 support."
- "Keys and values are the same as in L(pushgateway docs,https://github.com/prometheus/pushgateway/blob/master/https/README.md#sample-config)."
type: "dict"
pushgateway_basic_auth_users:
description: "Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt."
type: "dict"
pushgateway_binary_install_dir:
description:
- "I(Advanced)"
- "Directory to install pushgateway binary"
default: "/usr/local/bin"
pushgateway_system_group:
description:
- "I(Advanced)"
- "System group for Pushgateway"
default: "pushgateway"
pushgateway_system_user:
description:
- "I(Advanced)"
- "Pushgateway user"
default: "pushgateway"
31 changes: 31 additions & 0 deletions roles/pushgateway/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
galaxy_info:
author: "Prometheus Community"
description: "Prometheus Pushgateway"
license: "Apache"
min_ansible_version: "2.9"
platforms:
- name: "Ubuntu"
versions:
- "bionic"
- "focal"
- "jammy"
- name: "Debian"
versions:
- "bullseye"
- "buster"
- name: "EL"
versions:
- "7"
- "8"
- "9"
- name: "Fedora"
versions:
- "36"
- "37"
galaxy_tags:
- "monitoring"
- "prometheus"
- "exporter"
- "metrics"
- "system"
36 changes: 36 additions & 0 deletions roles/pushgateway/molecule/alternative/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
- name: Run role
hosts: all
any_errors_fatal: true
roles:
- prometheus.prometheus.pushgateway
pre_tasks:
- name: Create pushgateway cert dir
ansible.builtin.file:
path: "{{ pushgateway_tls_server_config.cert_file | dirname }}"
state: directory
owner: root
group: root
mode: u+rwX,g+rwX,o=rX

- name: Copy cert and key
ansible.builtin.copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{ item.mode | default('0644') }}"
loop:
- src: "/tmp/tls.cert"
dest: "{{ pushgateway_tls_server_config.cert_file }}"
- src: "/tmp/tls.key"
dest: "{{ pushgateway_tls_server_config.key_file }}"
vars:
pushgateway_binary_local_dir: "/tmp/pushgateway-linux-amd64"
pushgateway_web_listen_address: "127.0.0.1:8080"

pushgateway_tls_server_config:
cert_file: /etc/pushgateway/tls.cert
key_file: /etc/pushgateway/tls.key
pushgateway_http_server_config:
http2: true
pushgateway_basic_auth_users:
randomuser: examplepassword
1 change: 1 addition & 0 deletions roles/pushgateway/molecule/alternative/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
56 changes: 56 additions & 0 deletions roles/pushgateway/molecule/alternative/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
- name: Prepare
hosts: localhost
gather_facts: false
vars:
go_arch: amd64
pushgateway_version: 1.5.1
tasks:
- name: Download pushgateway binary to local folder
become: false
ansible.builtin.get_url:
url: "https://github.com/prometheus/pushgateway/releases/download/v{{ pushgateway_version }}/\
pushgateway-{{ pushgateway_version }}.linux-{{ go_arch }}.tar.gz"
dest: "/tmp/pushgateway-{{ pushgateway_version }}.linux-{{ go_arch }}.tar.gz"
mode: 0644
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
check_mode: false

- name: Unpack pushgateway binary
become: false
ansible.builtin.unarchive:
src: "/tmp/pushgateway-{{ pushgateway_version }}.linux-{{ go_arch }}.tar.gz"
dest: "/tmp"
creates: "/tmp/pushgateway-{{ pushgateway_version }}.linux-{{ go_arch }}/pushgateway"
check_mode: false

- name: Link to pushgateway binaries directory
become: false
ansible.builtin.file:
src: "/tmp/pushgateway-{{ pushgateway_version }}.linux-amd64"
dest: "/tmp/pushgateway-linux-amd64"
state: link
check_mode: false

- name: Install pyOpenSSL for certificate generation
ansible.builtin.pip:
name: "pyOpenSSL"

- name: Create private key
community.crypto.openssl_privatekey:
path: "/tmp/tls.key"

- name: Create CSR
community.crypto.openssl_csr:
path: "/tmp/tls.csr"
privatekey_path: "/tmp/tls.key"

- name: Create certificate
community.crypto.x509_certificate:
path: "/tmp/tls.cert"
csr_path: "/tmp/tls.csr"
privatekey_path: "/tmp/tls.key"
provider: selfsigned
39 changes: 39 additions & 0 deletions roles/pushgateway/molecule/alternative/tests/test_alternative.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


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


def test_service(host):
s = host.service("pushgateway")
# assert s.is_enabled
assert s.is_running


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


def test_socket(host):
sockets = [
"tcp://127.0.0.1:8080"
]
for socket in sockets:
s = host.socket(socket)
assert s.is_listening
8 changes: 8 additions & 0 deletions roles/pushgateway/molecule/default/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Converge
hosts: all
any_errors_fatal: true
roles:
- prometheus.prometheus.pushgateway
vars:
pushgateway_web_listen_address: "127.0.0.1:9091"
1 change: 1 addition & 0 deletions roles/pushgateway/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
Loading

0 comments on commit c676915

Please sign in to comment.