Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
Always install in /usr/local/bin & role cleanup (#28)
Browse files Browse the repository at this point in the history
Port changes from other @cloudalchemy roles:

- Remove blackbox_exporter_root_dir option and always install binary in /usr/local/bin
- split tasks/main.yml into multiple files
- add tags support
- fix readme after previous release

Create a new [minor] release.
  • Loading branch information
paulfantom authored Dec 5, 2018
1 parent e886478 commit 602bc35
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 104 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ All variables which can be overridden are stored in [defaults/main.yml](defaults

| Name | Default Value | Description |
| -------------- | ------------- | -----------------------------------|
| `blackbox_exporter_version` | 0.12.0 | Blackbox exporter package version |
| `blackbox_exporter_root_dir` | /opt/blackbox_exporter | Path to directory with blackbox exporter binary file |
| `blackbox_exporter_version` | 0.13.0 | Blackbox exporter package version |
| `blackbox_exporter_web_listen_address` | 0.0.0.0:9115 | Address on which blackbox exporter will be listening |
| `blackbox_exporter_cli_flags` | {} | Additional configuration flags passed to blackbox exporter binary at startup |
| `blackbox_exporter_configuration_modules` | http_2xx: { prober: http, timeout: 5s, http: '' } | |
Expand Down
2 changes: 0 additions & 2 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
blackbox_exporter_version: 0.13.0

blackbox_exporter_root_dir: "/opt/blackbox_exporter"

blackbox_exporter_web_listen_address: "0.0.0.0:9115"

blackbox_exporter_cli_flags: {}
Expand Down
25 changes: 9 additions & 16 deletions molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import pytest
import os
import testinfra.utils.ansible_runner

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


def test_files(host):
dirs = [
"/opt/blackbox_exporter"
]
files = [
"/etc/blackbox_exporter.yml",
"/opt/blackbox_exporter/blackbox_exporter"
]
for directory in dirs:
d = host.file(directory)
assert d.is_directory
assert d.exists
for file in files:
f = host.file(file)
assert f.exists
assert f.is_file
@pytest.mark.parametrize("files", [
"/etc/blackbox_exporter.yml",
"/usr/local/bin/blackbox_exporter"
])
def test_files(host, files):
f = host.file(files)
assert f.exists
assert f.is_file


def test_service(host):
Expand Down
20 changes: 20 additions & 0 deletions tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
- name: create systemd service unit
template:
src: blackbox_exporter.service.j2
dest: /etc/systemd/system/blackbox_exporter.service
owner: root
group: root
mode: 0644
notify:
- restart blackbox exporter

- name: configure blackbox exporter
template:
src: blackbox_exporter.yml.j2
dest: /etc/blackbox_exporter.yml
owner: blackbox-exp
group: blackbox-exp
mode: 0644
notify:
- reload blackbox exporter
54 changes: 54 additions & 0 deletions tasks/install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
- name: create blackbox_exporter system group
group:
name: blackbox-exp
system: true
state: present

- name: create blackbox_exporter system user
user:
name: blackbox-exp
system: true
shell: "/sbin/nologin"
group: blackbox-exp
createhome: false

- name: download blackbox exporter binary to local folder
become: false
unarchive:
src: "https://github.com/prometheus/blackbox_exporter/releases/download/v{{ blackbox_exporter_version }}/blackbox_exporter-{{ blackbox_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}.tar.gz"
dest: "/tmp"
remote_src: true
creates: "/tmp/blackbox_exporter-{{ blackbox_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}/blackbox_exporter"
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
delegate_to: localhost
check_mode: false

- name: propagate blackbox exporter binary
copy:
src: "/tmp/blackbox_exporter-{{ blackbox_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}/blackbox_exporter"
dest: "/usr/local/bin/blackbox_exporter"
mode: 0750
owner: blackbox-exp
group: blackbox-exp
notify:
- restart blackbox exporter

- name: Install libcap on Debian systems
package:
name: "libcap2-bin"
state: present
register: _download_packages
until: _download_packages is succeeded
retries: 5
delay: 2
when: ansible_os_family | lower == "debian"

- name: Ensure blackbox exporter binary has cap_net_raw capability
capabilities:
path: '/usr/local/bin/blackbox_exporter'
capability: cap_net_raw+ep
state: present
104 changes: 21 additions & 83 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,88 +1,26 @@
---
- name: create blackbox_exporter system group
group:
name: blackbox-exp
system: true
state: present

- name: create blackbox_exporter system user
user:
name: blackbox-exp
system: true
shell: "/sbin/nologin"
group: blackbox-exp
createhome: false

- name: create blackbox_exporter directories
file:
path: "{{ blackbox_exporter_root_dir }}"
state: directory
owner: blackbox-exp
group: blackbox-exp
mode: 0755

- name: download blackbox exporter binary to local folder
become: false
unarchive:
src: "https://github.com/prometheus/blackbox_exporter/releases/download/v{{ blackbox_exporter_version }}/blackbox_exporter-{{ blackbox_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}.tar.gz"
dest: "/tmp"
remote_src: true
creates: "/tmp/blackbox_exporter-{{ blackbox_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}/blackbox_exporter"
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
delegate_to: localhost
check_mode: false

- name: propagate blackbox exporter binary
copy:
src: "/tmp/blackbox_exporter-{{ blackbox_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}/blackbox_exporter"
dest: "{{ blackbox_exporter_root_dir }}/blackbox_exporter"
mode: 0750
owner: blackbox-exp
group: blackbox-exp
notify:
- restart blackbox exporter

- name: Install libcap on Debian systems
package:
name: "libcap2-bin"
state: present
register: _download_packages
until: _download_packages is succeeded
retries: 5
delay: 2
when: ansible_os_family | lower == "debian"

- name: Ensure blackbox exporter binary has cap_net_raw capability
capabilities:
path: '{{ blackbox_exporter_root_dir }}/blackbox_exporter'
capability: cap_net_raw+ep
state: present

- name: create systemd service unit
template:
src: blackbox_exporter.service.j2
dest: /etc/systemd/system/blackbox_exporter.service
owner: root
group: root
mode: 0644
notify:
- restart blackbox exporter

- name: configure blackbox exporter
template:
src: blackbox_exporter.yml.j2
dest: /etc/blackbox_exporter.yml
owner: blackbox-exp
group: blackbox-exp
mode: 0644
notify:
- reload blackbox exporter

- name: ensure blackbox_exporter service is enabled
- include: preflight.yml
tags:
- blackbox_exporter_install
- blackbox_exporter_configure
- blackbox_exporter_run

- include: install.yml
become: true
tags:
- blackbox_exporter_install

- include: configure.yml
become: true
tags:
- blackbox_exporter_configure

- name: ensure blackbox_exporter service is started and enabled
become: true
systemd:
daemon_reload: true
name: blackbox_exporter
state: started
enabled: true
tags:
- blackbox_exporter_run
1 change: 1 addition & 0 deletions tasks/preflight.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
3 changes: 2 additions & 1 deletion templates/blackbox_exporter.service.j2
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{ ansible_managed | comment }}
[Unit]
Description=Blackbox Exporter
After=network.target
Expand All @@ -9,7 +10,7 @@ User=blackbox-exp
Group=blackbox-exp
PermissionsStartOnly=true
ExecReload=/bin/kill -HUP $MAINPID
ExecStart={{ blackbox_exporter_root_dir }}/blackbox_exporter \
ExecStart=/usr/local/bin/blackbox_exporter \
--config.file=/etc/blackbox_exporter.yml \
--web.listen-address={{ blackbox_exporter_web_listen_address }}{% for flag, flag_value in blackbox_exporter_cli_flags.items() %} \
--{{ flag }}={{ flag_value }}{% endfor %}
Expand Down

0 comments on commit 602bc35

Please sign in to comment.