From 232bdca2add4253981b0873188663069f74a0610 Mon Sep 17 00:00:00 2001 From: Colin Wilk Date: Sun, 11 Jun 2023 18:53:04 +0200 Subject: Add ability to specify target path prefix This will decrease the amount of redundant configuration for users by allowing them to specify a general prefix on global / exporter / play level Signed-off-by: Colin Wilk --- README.md | 49 +++++++++++++++++++++- defaults/main.yml | 15 ++++++- meta/argument_specs.yml | 7 ++++ molecule/default/converge.yml | 45 +++++++++++++++++++- .../default/tests/test_check_prometheus_targets.py | 21 ++++++++++ tasks/lineinfile.yml | 5 ++- 6 files changed, 137 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index dfc3939..f3d285c 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,9 @@ prometheus_target_exporter_defaults: {} # path: /opt/prometheus/targets.yml # host: '{{ inventory_hostname }}:9100' # blackbox_exporter: - # path: /opt/prometheus/blackbox.yml + # path: /opt/targets/blackbox.yml # host: 'https://{{ hostvars[inventory_hostname].ansible_host }}' + # path_prefix: '' prometheus_target_exporter: [] ``` @@ -71,6 +72,26 @@ prometheus_target_default_exporters: [] prometheus_target_skip_default_exporters: false ``` +As you usually have most target files in one directory you can specify a target +prefix for your target files: + +```yaml +prometheus_target_exporter_target_prefix: '' +``` + +This way you only need to pass `target.yml` instead of `/path/to/target.yml` as +your exporter path. You can additionally define this at the +`prometheus_target_exporter_defaults` and the `prometheus_target_exporter` level +using the `path_prefix` variable: + +```yaml +prometheus_target_exporter_defaults: + blackbox_exporter: + path: /opt/targets/blackbox.yml + host: 'https://{{ hostvars[inventory_hostname].ansible_host }}' + path_prefix: '' # Disables configured prefix +``` + This role offers a few strategies that you can use to deploy your targets. The strategy decides how exactly targets are added to the targets file and more importantly how to handle existing configuration. @@ -164,6 +185,32 @@ Simple example - id: node_exporter ``` +Using Target prefix + +```yaml +- name: Deploy node exporter with target prefix + hosts: myhost + + vars: # General configuration. Can be set in group_vars + prometheus_target_host: prometheus + prometheus_target_exporter_target_prefix: /opt/prometheus/targets/ + prometheus_target_exporter_defaults: + node_exporter: + path: node.yml + host: '{{ inventory_hostname }}:9100' + blackbox_exporter: # Another exporter with different prefix + path: target.yml + host: '{{ inventory_hostname }}' + path_prefix: /opt/prefix/ + + roles: + - role: kliwniloc.prometheus_target + prometheus_target_exporter: + - id: node_exporter # -> /opt/prometheus/targets/node.yml + - { id: node_exporter, path: /target.yml, path_prefix: '' } # -> /target.yml + - { id: blackbox_exporter, path: blackbox.yml } # -> /opt/prefix/blackbox.yml +``` + Using Handlers ```yaml diff --git a/defaults/main.yml b/defaults/main.yml index 52afffd..0a8e9ba 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -91,6 +91,16 @@ prometheus_target_strategy_lineinfile_suffix: '' # Exporter configuration ################################################################################ +# You can configure a target prefix to keep your targets in your exporters +# configuration. DRY For example if all your target files reside in +# /opt/prometheus it's a good idea to set the prefix to /opt/prometheus/ so that +# you can define your remaining target files as just target.yml instead of +# /opt/prometheus/target.yml +# In case you have a few exporters that reside in different directories you may +# ignore the prefix by specifying `path_prefix: ''` in the exporter default +# or the enabled exporter config. +prometheus_target_exporter_target_prefix: '' + # This will configure the default settings for specified exporters. # The key in this dict should map to the possible id in the specified exporter # in prometheus_target_exporter or prometheus_target_default_exporters. @@ -105,8 +115,9 @@ prometheus_target_exporter_defaults: {} # path: /opt/prometheus/targets.yml # host: '{{ inventory_hostname }}:9100' # blackbox_exporter: - # path: /opt/prometheus/blackbox.yml + # path: /opt/targets/blackbox.yml # host: 'https://{{ hostvars[inventory_hostname].ansible_host }}' + # path_prefix: '' # This is where you specify the exporters that should be deployed to prometheus. # You should configure this on a per play basis. If you wish to configure @@ -125,7 +136,7 @@ prometheus_target_exporter: [] # ignored. prometheus_target_default_exporters: [] # - { id: node_exporter } - # - { id: blackbox_exporter, path: /path/to/target } + # - { id: blackbox_exporter, path: /path/to/target, path_prefix: '' } # You can enable this variable to not add exporters defined in # prometheus_target_default_exporters diff --git a/meta/argument_specs.yml b/meta/argument_specs.yml index ec16ae0..270bf68 100644 --- a/meta/argument_specs.yml +++ b/meta/argument_specs.yml @@ -13,6 +13,10 @@ argument_specs: ################################################################################ # Optional ################################################################################ + prometheus_target_exporter_target_prefix: + type: str + required: false + default: '' prometheus_target_strategy: type: str @@ -71,6 +75,9 @@ argument_specs: path: type: str required: false + path_prefix: + type: str + required: false host: type: str required: false diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 8f2b399..e96fb81 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -1,4 +1,6 @@ --- +################################################################################ +################################################################################ - name: Simple deploy hosts: application pre_tasks: @@ -37,7 +39,47 @@ - id: blackbox_exporter path: /opt/simple_target3.yml +################################################################################ +################################################################################ +- name: Prefix deploy + hosts: application + pre_tasks: + - name: Create targets + ansible.builtin.file: + path: '{{ item.path }}' + state: '{{ item.state }}' + modification_time: preserve + access_time: preserve + mode: '0644' + become: true + delegate_to: '{{ prometheus_target_host }}' + loop: + - { state: touch, path: /opt/prefix_target1.yml } + - { state: touch, path: /opt/prefix_target2.yml } + - { state: directory, path: /opt/prefix} + - { state: touch, path: /opt/prefix/prefix_target3.yml } + + vars: + prometheus_target_host: prometheus + prometheus_target_exporter_target_prefix: /opt/ + prometheus_target_exporter_defaults: + node_exporter: + path: prefix_target1.yml + host: '{{ inventory_hostname }}' + blackbox_exporter: + path: prefix_target_not_exist.yml + host: '{{ inventory_hostname }}' + path_prefix: /opt/prefix/ + roles: + - role: kliwniloc.prometheus_target + prometheus_target_exporter: + - id: node_exporter + - { id: node_exporter, path: /opt/prefix_target2.yml, path_prefix: '' } + - { id: blackbox_exporter, path: prefix_target3.yml } + +################################################################################ +################################################################################ - name: Deploy with hooks hosts: application pre_tasks: @@ -71,7 +113,8 @@ host: application path: /opt/hook_target.yml - +################################################################################ +################################################################################ - name: Deploy with lineinfile hosts: application pre_tasks: diff --git a/molecule/default/tests/test_check_prometheus_targets.py b/molecule/default/tests/test_check_prometheus_targets.py index 2a45661..9ce1056 100644 --- a/molecule/default/tests/test_check_prometheus_targets.py +++ b/molecule/default/tests/test_check_prometheus_targets.py @@ -23,6 +23,27 @@ def test_check_hosts_added_simple(host): assert t3.content_string == \ ' - application_AA\n' +""" +Test prefix functionality +""" +def test_check_hosts_added_prefix(host): + t1 = host.file('/opt/prefix_target1.yml') + t2 = host.file('/opt/prefix_target2.yml') + t3 = host.file('/opt/prefix/prefix_target3.yml') + + assert t1.exists + assert t2.exists + assert t3.exists + + assert t1.content_string == \ + ' - application\n' + + + assert t2.content_string == \ + ' - application\n' + + assert t3.content_string == \ + ' - application\n' """ Test hook functionality diff --git a/tasks/lineinfile.yml b/tasks/lineinfile.yml index 5f93b92..d7f0793 100644 --- a/tasks/lineinfile.yml +++ b/tasks/lineinfile.yml @@ -1,7 +1,10 @@ --- - name: Make sure targets are deployed ansible.builtin.lineinfile: - path: '{{ item.path | default(prometheus_target_exporter_defaults[item.id].path) }}' + path: '{{ (item.path_prefix + | default(prometheus_target_exporter_defaults[item.id].path_prefix) + | default(prometheus_target_exporter_target_prefix)) ~ + item.path | default(prometheus_target_exporter_defaults[item.id].path) }}' line: '{{ prometheus_target_strategy_lineinfile_prefix ~ (item.host | default(prometheus_target_exporter_defaults[item.id].host)) ~ prometheus_target_strategy_lineinfile_suffix }}' -- cgit v1.2.3