diff --git a/roles/node_exporter/molecule/alternative/molecule.yml b/roles/node_exporter/molecule/alternative/molecule.yml index dda5c8e0..a9d1c7a6 100644 --- a/roles/node_exporter/molecule/alternative/molecule.yml +++ b/roles/node_exporter/molecule/alternative/molecule.yml @@ -4,7 +4,9 @@ provisioner: group_vars: all: node_exporter_binary_local_dir: "/tmp/node_exporter-linux-amd64" - node_exporter_web_listen_address: "127.0.0.1:8080" + node_exporter_web_listen_address: + - '127.0.0.1:8080' + - '127.0.1.1:8080' node_exporter_textfile_dir: "" node_exporter_enabled_collectors: - entropy @@ -19,4 +21,4 @@ provisioner: node_exporter_basic_auth_users: randomuser: examplepassword go_arch: amd64 - node_exporter_version: 1.0.0 + node_exporter_version: 1.5.0 diff --git a/roles/node_exporter/molecule/alternative/tests/test_alternative.py b/roles/node_exporter/molecule/alternative/tests/test_alternative.py index bc0591ee..0d5b517b 100644 --- a/roles/node_exporter/molecule/alternative/tests/test_alternative.py +++ b/roles/node_exporter/molecule/alternative/tests/test_alternative.py @@ -3,6 +3,7 @@ __metaclass__ = type import os import testinfra.utils.ansible_runner +import pytest testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') @@ -36,10 +37,9 @@ def test_protecthome_property(host): assert p.get("ProtectHome") == "yes" -def test_socket(host): - sockets = [ - "tcp://127.0.0.1:8080" - ] - for socket in sockets: - s = host.socket(socket) - assert s.is_listening +@pytest.mark.parametrize("sockets", [ + "tcp://127.0.0.1:8080", + "tcp://127.0.1.1:8080", +]) +def test_socket(host, sockets): + assert host.socket(sockets).is_listening diff --git a/roles/node_exporter/tasks/preflight.yml b/roles/node_exporter/tasks/preflight.yml index f1a175c1..6b4fb36a 100644 --- a/roles/node_exporter/tasks/preflight.yml +++ b/roles/node_exporter/tasks/preflight.yml @@ -20,10 +20,26 @@ ansible.builtin.package_facts: when: "not 'packages' in ansible_facts" +- name: Assert that used version supports listen address type + ansible.builtin.assert: + that: + - >- + node_exporter_web_listen_address is string + or + ( + node_exporter_version is version('1.5.0', '>=') and + node_exporter_web_listen_address | type_debug == "list" + ) + - name: Naive assertion of proper listen address ansible.builtin.assert: that: - - "':' in node_exporter_web_listen_address" + - >- + [node_exporter_web_listen_address] | + flatten | + reject('match', '.+:\\d+$') | + list | + length == 0 - name: Assert collectors are not both disabled and enabled at the same time ansible.builtin.assert: diff --git a/roles/node_exporter/templates/node_exporter.service.j2 b/roles/node_exporter/templates/node_exporter.service.j2 index 36c45034..467cece3 100644 --- a/roles/node_exporter/templates/node_exporter.service.j2 +++ b/roles/node_exporter/templates/node_exporter.service.j2 @@ -30,7 +30,16 @@ ExecStart={{ node_exporter_binary_install_dir }}/node_exporter \ '--web.config=/etc/node_exporter/config.yaml' \ {% endif %} {% endif %} +{% if node_exporter_version is version('1.5.0', '>=') and + node_exporter_web_listen_address is iterable and + node_exporter_web_listen_address is not mapping and + node_exporter_web_listen_address is not string %} +{% for address in node_exporter_web_listen_address %} + '--web.listen-address={{ address }}' \ +{% endfor %} +{% else %} '--web.listen-address={{ node_exporter_web_listen_address }}' \ +{% endif %} '--web.telemetry-path={{ node_exporter_web_telemetry_path }}' SyslogIdentifier=node_exporter