diff --git a/README.md b/README.md index 4e9155e..0ed99f9 100644 --- a/README.md +++ b/README.md @@ -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: '' } | | diff --git a/defaults/main.yml b/defaults/main.yml index 257d04b..869fa73 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -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: {} diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py index 37dee1e..75ba172 100644 --- a/molecule/default/tests/test_default.py +++ b/molecule/default/tests/test_default.py @@ -1,3 +1,4 @@ +import pytest import os import testinfra.utils.ansible_runner @@ -5,22 +6,14 @@ 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): diff --git a/tasks/configure.yml b/tasks/configure.yml new file mode 100644 index 0000000..291fd0a --- /dev/null +++ b/tasks/configure.yml @@ -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 diff --git a/tasks/install.yml b/tasks/install.yml new file mode 100644 index 0000000..dd8da6f --- /dev/null +++ b/tasks/install.yml @@ -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 diff --git a/tasks/main.yml b/tasks/main.yml index c32e3db..84b18c5 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -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 diff --git a/tasks/preflight.yml b/tasks/preflight.yml new file mode 100644 index 0000000..ed97d53 --- /dev/null +++ b/tasks/preflight.yml @@ -0,0 +1 @@ +--- diff --git a/templates/blackbox_exporter.service.j2 b/templates/blackbox_exporter.service.j2 index a153319..d7953ca 100644 --- a/templates/blackbox_exporter.service.j2 +++ b/templates/blackbox_exporter.service.j2 @@ -1,3 +1,4 @@ +{{ ansible_managed | comment }} [Unit] Description=Blackbox Exporter After=network.target @@ -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 %}