From 2d664453e6b84fd2d9347d613f3101aec448e5d6 Mon Sep 17 00:00:00 2001 From: rsuplina Date: Mon, 4 Mar 2024 11:44:19 +0000 Subject: [PATCH] Add Blackbox Role Signed-off-by: rsuplina --- roles/blackbox/defaults/main.yml | 26 ++++++ roles/blackbox/tasks/main.yml | 88 ++++++++++++++++++++ roles/blackbox/templates/blackbox.service.j2 | 16 ++++ 3 files changed, 130 insertions(+) create mode 100644 roles/blackbox/defaults/main.yml create mode 100644 roles/blackbox/tasks/main.yml create mode 100644 roles/blackbox/templates/blackbox.service.j2 diff --git a/roles/blackbox/defaults/main.yml b/roles/blackbox/defaults/main.yml new file mode 100644 index 00000000..4b36b8ca --- /dev/null +++ b/roles/blackbox/defaults/main.yml @@ -0,0 +1,26 @@ +--- + +# Copyright 2024 Cloudera, Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +blackbox_tarball_url: https://github.com/prometheus/blackbox_exporter/releases/download/v0.24.0/blackbox_exporter-0.24.0.linux-amd64.tar.gz +blackbox_directory: /etc/blackbox +blackbox_bin_directory: /usr/local/bin +blackbox_tarball_file: blackbox.tar.gz + +blackbox_user: blackbox +blackbox_group: blackbox + +blackbox_service_directory: /etc/systemd/system/blackbox.service diff --git a/roles/blackbox/tasks/main.yml b/roles/blackbox/tasks/main.yml new file mode 100644 index 00000000..0666cbdf --- /dev/null +++ b/roles/blackbox/tasks/main.yml @@ -0,0 +1,88 @@ +--- + +# Copyright 2024 Cloudera, Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +- name: Create Blackbox directory + ansible.builtin.file: + path: "{{ blackbox_directory }}" + state: directory + +- name: Create a temporary directory + ansible.builtin.tempfile: + state: directory + register: __blackbox_tmp + +- name: Download Blackbox tarball + ansible.builtin.get_url: + url: "{{ blackbox_tarball_url }}" + dest: "{{ __blackbox_tmp.path }}/{{ blackbox_tarball_file }}" + +- name: Extract tarball + ansible.builtin.unarchive: + src: "{{ __blackbox_tmp.path }}/{{ blackbox_tarball_file }}" + dest: "{{ blackbox_directory }}" + extra_opts: --strip-components=1 + remote_src: yes + +- name: Remove the temporary directory + when: __blackbox_tmp is defined + ansible.builtin.file: + path: "{{ __blackbox_tmp.path }}" + state: absent + +- name: Create Blackbox group + ansible.builtin.group: + name: "{{ blackbox_group }}" + +- name: Create Blackbox user + ansible.builtin.user: + name: "{{ blackbox_user }}" + system: True + +- name: Set ownership of all files inside /etc/blackbox + ansible.builtin.file: + path: "{{ blackbox_directory }}" + owner: "{{ blackbox_user }}" + group: "{{ blackbox_group }}" + recurse: yes + +- name: Copy blackbox binary to /usr/local/bin + ansible.builtin.copy: + remote_src: yes + src: "{{ blackbox_directory }}/blackbox_exporter" + dest: "{{ blackbox_bin_directory }}/blackbox_exporter" + owner: "{{ blackbox_user }}" + group: "{{ blackbox_group }}" + mode: '0755' + +- name: Create Blackbox service template + ansible.builtin.template: + src: blackbox.service.j2 + dest: "{{ blackbox_service_directory }}" + register: __blackbox_service + +- name: Start and enable Blackbox service + when: __blackbox_service.changed + block: + + - name: Reload systemd daemon + ansible.builtin.systemd: + daemon_reload: yes + + - name: Start Blackbox service + ansible.builtin.systemd: + name: blackbox + state: started + enabled: yes diff --git a/roles/blackbox/templates/blackbox.service.j2 b/roles/blackbox/templates/blackbox.service.j2 new file mode 100644 index 00000000..2d926e20 --- /dev/null +++ b/roles/blackbox/templates/blackbox.service.j2 @@ -0,0 +1,16 @@ +[Unit] +Description=Blackbox Exporter Service +Wants=network-online.target +After=network-online.target + +[Service] +Type=simple +User={{ blackbox_user }} +Group={{ blackbox_group }} +ExecStart={{ blackbox_bin_directory }}/blackbox_exporter \ + --config.file={{ blackbox_directory }}/blackbox.yml + +Restart=always + +[Install] +WantedBy=multi-user.target \ No newline at end of file