From 579b168b2c93daee67ede17ee3289da1678b3833 Mon Sep 17 00:00:00 2001 From: Colin Wilk Date: Thu, 15 Jun 2023 22:40:27 +0200 Subject: Add ability to use exporter without id property The id property of the exporter is used for checking the default values specified in `prometheus_target_exporter_defaults` variable. However when both the `host` and the `path` fields are specified manually in the `prometheus_target_exporter` list this id is never looked up. The way the templating was handled however, still resulted in an error when leaving out the id field. The templating code was now rewritten to allow for the following exporter configuraion to run: prometheus_target_exporter: - { host: exporter_without_id, path: /opt/simple_target4.yml } Signed-off-by: Colin Wilk --- README.md | 2 ++ meta/argument_specs.yml | 2 +- molecule/default/converge.yml | 2 ++ molecule/default/tests/test_check_prometheus_targets.py | 5 +++++ tasks/lineinfile.yml | 14 +++++++++----- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f3d285c..0cfe278 100644 --- a/README.md +++ b/README.md @@ -254,6 +254,8 @@ Multiple exporters - role: kliwniloc.prometheus_target # deploy targets prometheus_target_exporter: - id: node_exporter # deploy node_exporter with default host + # deploy an exporter that is not specified in prometheus_target_exporter_defaults + - { host: exporter_without_id, path: /opt/simple_target4.yml } # deploy blackbox_exporter with multiple hosts - { id: blackbox_exporter, host: node1.example.org } - { id: blackbox_exporter, host: node2.example.org } diff --git a/meta/argument_specs.yml b/meta/argument_specs.yml index 270bf68..502c640 100644 --- a/meta/argument_specs.yml +++ b/meta/argument_specs.yml @@ -71,7 +71,7 @@ argument_specs: options: id: type: str - required: true + required: false path: type: str required: false diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index e96fb81..9346988 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -17,6 +17,7 @@ - /opt/simple_target1.yml - /opt/simple_target2.yml - /opt/simple_target3.yml + - /opt/simple_target4.yml vars: prometheus_target_host: prometheus @@ -34,6 +35,7 @@ - id: node_exporter - { id: blackbox_exporter, host: test1 } - { id: blackbox_exporter, host: test2 } + - { host: exporter_without_id, path: /opt/simple_target4.yml } - role: kliwniloc.prometheus_target prometheus_target_exporter: - id: blackbox_exporter diff --git a/molecule/default/tests/test_check_prometheus_targets.py b/molecule/default/tests/test_check_prometheus_targets.py index 9ce1056..ddf3a22 100644 --- a/molecule/default/tests/test_check_prometheus_targets.py +++ b/molecule/default/tests/test_check_prometheus_targets.py @@ -7,10 +7,12 @@ def test_check_hosts_added_simple(host): t1 = host.file('/opt/simple_target1.yml') t2 = host.file('/opt/simple_target2.yml') t3 = host.file('/opt/simple_target3.yml') + t4 = host.file('/opt/simple_target4.yml') assert t1.exists assert t2.exists assert t3.exists + assert t4.exists assert t1.content_string == \ ' - application\n' @@ -23,6 +25,9 @@ def test_check_hosts_added_simple(host): assert t3.content_string == \ ' - application_AA\n' + assert t4.content_string == \ + ' - exporter_without_id\n' + """ Test prefix functionality """ diff --git a/tasks/lineinfile.yml b/tasks/lineinfile.yml index d7f0793..0367be9 100644 --- a/tasks/lineinfile.yml +++ b/tasks/lineinfile.yml @@ -1,12 +1,16 @@ --- - name: Make sure targets are deployed ansible.builtin.lineinfile: - 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) }}' + path: '{{ ( + item.path_prefix if item.path_prefix is defined else + (prometheus_target_exporter_defaults[item.id].path_prefix + | default(prometheus_target_exporter_target_prefix)) + if item.id is defined) + ~ + (item.path if item.path is defined + else prometheus_target_exporter_defaults[item.id].path if item.id is defined) | mandatory }}' line: '{{ prometheus_target_strategy_lineinfile_prefix ~ - (item.host | default(prometheus_target_exporter_defaults[item.id].host)) ~ + (item.host if item.host is defined else prometheus_target_exporter_defaults[item.id].host) | mandatory ~ prometheus_target_strategy_lineinfile_suffix }}' state: present become: true -- cgit v1.2.3