diff --git a/roles/snmp_exporter/molecule/alternative/molecule.yml b/roles/snmp_exporter/molecule/alternative/molecule.yml index b3a51bf5..64ba5718 100644 --- a/roles/snmp_exporter/molecule/alternative/molecule.yml +++ b/roles/snmp_exporter/molecule/alternative/molecule.yml @@ -3,5 +3,7 @@ provisioner: inventory: group_vars: all: - snmp_exporter_web_listen_address: "127.0.0.1:9116" + snmp_exporter_web_listen_address: + - '127.0.0.1:9116' + - '127.0.1.1:9116' snmp_exporter_config_file: ${MOLECULE_SCENARIO_DIRECTORY}/templates/snmp.yml diff --git a/roles/snmp_exporter/molecule/alternative/tests/test_alternative.py b/roles/snmp_exporter/molecule/alternative/tests/test_alternative.py index 6715e5b0..7c32636c 100644 --- a/roles/snmp_exporter/molecule/alternative/tests/test_alternative.py +++ b/roles/snmp_exporter/molecule/alternative/tests/test_alternative.py @@ -33,6 +33,9 @@ def test_service(host): raise # Re-raise the original assertion error -def test_socket(host): - s = host.socket("tcp://127.0.0.1:9116") - assert s.is_listening +@pytest.mark.parametrize("sockets", [ + "tcp://127.0.0.1:9116", + "tcp://127.0.1.1:9116", +]) +def test_socket(host, sockets): + assert host.socket(sockets).is_listening diff --git a/roles/snmp_exporter/tasks/preflight.yml b/roles/snmp_exporter/tasks/preflight.yml index d72310c3..308d8007 100644 --- a/roles/snmp_exporter/tasks/preflight.yml +++ b/roles/snmp_exporter/tasks/preflight.yml @@ -1,4 +1,26 @@ --- + +- name: Assert that used version supports listen address type + ansible.builtin.assert: + that: + - >- + snmp_exporter_web_listen_address is string + or + ( + snmp_exporter_version is version('0.21.0', '>=') and + snmp_exporter_web_listen_address | type_debug == "list" + ) + +- name: Naive assertion of proper listen address + ansible.builtin.assert: + that: + - >- + [snmp_exporter_web_listen_address] | + flatten | + reject('match', '.+:\\d+$') | + list | + length == 0 + - name: Discover latest version ansible.builtin.set_fact: snmp_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/prometheus/snmp_exporter/releases/latest', headers=_github_api_headers, diff --git a/roles/snmp_exporter/templates/snmp_exporter.service.j2 b/roles/snmp_exporter/templates/snmp_exporter.service.j2 index eb47fd1c..50e0c009 100644 --- a/roles/snmp_exporter/templates/snmp_exporter.service.j2 +++ b/roles/snmp_exporter/templates/snmp_exporter.service.j2 @@ -9,7 +9,16 @@ User=nobody Group={{ 'nogroup' if ansible_os_family == 'Debian' else 'nobody' }} ExecReload=/bin/kill -HUP $MAINPID ExecStart=/usr/local/bin/snmp_exporter \ +{% if snmp_exporter_version is version('0.21.0', '>=') and + snmp_exporter_web_listen_address is iterable and + snmp_exporter_web_listen_address is not mapping and + snmp_exporter_web_listen_address is not string %} +{% for address in snmp_exporter_web_listen_address %} + --web.listen-address={{ address }} \ +{% endfor %} +{% else %} --web.listen-address={{ snmp_exporter_web_listen_address }} \ +{% endif %} --log.level={{ snmp_exporter_log_level }} \ --config.file=/etc/snmp_exporter/snmp.yml