diff --git a/roles/nginx_exporter/defaults/main.yml b/roles/nginx_exporter/defaults/main.yml index cf389ee2..42ba9de1 100644 --- a/roles/nginx_exporter/defaults/main.yml +++ b/roles/nginx_exporter/defaults/main.yml @@ -1,11 +1,9 @@ --- nginx_exporter_version: 1.3.0 -nginx_exporter_binary_local_dir: "" nginx_exporter_binary_url: "https://github.com/{{ _nginx_exporter_repo }}/releases/download/v{{ nginx_exporter_version }}/\ - nginx-prometheus-exporter_{{ nginx_exporter_version }}_linux_{{ go_arch }}.tar.gz" + nginx-prometheus-exporter_{{ nginx_exporter_version }}_{{ ansible_system | lower }}_{{ _nginx_exporter_go_ansible_arch }}.tar.gz" nginx_exporter_checksums_url: "https://github.com/{{ _nginx_exporter_repo }}/releases/download/v{{ nginx_exporter_version }}/\ nginx-prometheus-exporter_{{ nginx_exporter_version }}_checksums.txt" -nginx_exporter_skip_install: false nginx_exporter_plus: false nginx_exporter_scrape_uri: "http://127.0.0.1/stub_status" nginx_exporter_web_listen_address: "0.0.0.0:9113" @@ -24,5 +22,7 @@ nginx_exporter_binary_install_dir: "/usr/local/bin" nginx_exporter_system_group: "nginx-exp" nginx_exporter_system_user: "{{ nginx_exporter_system_group }}" +nginx_exporter_config_dir: "/etc/nginx_exporter" + # Local path to stash the archive and its extraction -nginx_exporter_archive_path: /tmp +nginx_exporter_local_cache_path: "/tmp/nginx_exporter-{{ ansible_system | lower }}-{{ _nginx_exporter_go_ansible_arch }}/{{ nginx_exporter_version }}" diff --git a/roles/nginx_exporter/meta/argument_specs.yml b/roles/nginx_exporter/meta/argument_specs.yml index 330cd9a9..5004be07 100644 --- a/roles/nginx_exporter/meta/argument_specs.yml +++ b/roles/nginx_exporter/meta/argument_specs.yml @@ -11,22 +11,13 @@ argument_specs: nginx_exporter_version: description: "nginx_exporter package version. Also accepts latest as parameter." default: "1.3.0" - nginx_exporter_skip_install: - description: "nginx_exporter installation tasks gets skipped when set to true." - type: bool - default: false nginx_exporter_plus: description: "Start the exporter for NGINX Plus." type: bool default: false - nginx_exporter_binary_local_dir: - description: - - "Enables the use of local packages instead of those distributed on github." - - "The parameter may be set to a directory where the C(nginx_exporter) binary is stored on the host where ansible is run." - - "This overrides the I(nginx_exporter_version) parameter" nginx_exporter_binary_url: description: "URL of the nginx_exporter binaries .tar.gz file" - default: "https://github.com/{{ _nginx_exporter_repo }}/releases/download/v{{ nginx_exporter_version }}/nginx_exporter-{{ nginx_exporter_version }}.linux-{{ go_arch }}.tar.gz" + default: "https://github.com/{{ _nginx_exporter_repo }}/releases/download/v{{ nginx_exporter_version }}/nginx-prometheus-exporter_{{ nginx_exporter_version }}_{{ ansible_system | lower }}_{{ _nginx_exporter_go_ansible_arch }}.tar.gz" nginx_exporter_checksums_url: description: "URL of the nginx_exporter checksums file" default: "https://github.com/{{ _nginx_exporter_repo }}/releases/download/v{{ nginx_exporter_version }}/nginx-prometheus-exporter_{{ nginx_exporter_version }}_checksums.txt" @@ -73,6 +64,9 @@ argument_specs: - "I(Advanced)" - "nginx_exporter user" default: "nginx-exp" - nginx_exporter_archive_path: - description: 'Local path to stash the archive and its extraction' - default: "/tmp" + nginx_exporter_local_cache_path: + description: "Local path to stash the archive and its extraction" + default: "/tmp/nginx_exporter-{{ ansible_system | lower }}-{{ _nginx_exporter_go_ansible_arch }}/{{ nginx_exporter_version }}" + nginx_exporter_config_dir: + description: "Path to directory with nginx_exporter configuration" + default: "/etc/nginx_exporter" diff --git a/roles/nginx_exporter/molecule/default/tests/test_default.py b/roles/nginx_exporter/molecule/default/tests/test_default.py index d7c4a9fd..1d10cc2a 100644 --- a/roles/nginx_exporter/molecule/default/tests/test_default.py +++ b/roles/nginx_exporter/molecule/default/tests/test_default.py @@ -31,7 +31,6 @@ def test_user(host): assert host.group("nginx-exp").exists assert "nginx-exp" in host.user("nginx-exp").groups assert host.user("nginx-exp").shell == "/usr/sbin/nologin" - assert host.user("nginx-exp").home == "/" def test_service(host): diff --git a/roles/nginx_exporter/tasks/configure.yml b/roles/nginx_exporter/tasks/configure.yml deleted file mode 100644 index 55e810ce..00000000 --- a/roles/nginx_exporter/tasks/configure.yml +++ /dev/null @@ -1,36 +0,0 @@ ---- -- name: Copy the nginx_exporter systemd service file - ansible.builtin.template: - src: nginx_exporter.service.j2 - dest: /etc/systemd/system/nginx_exporter.service - owner: root - group: root - mode: 0644 - notify: restart nginx_exporter - -- name: Create nginx_exporter config directory - ansible.builtin.file: - path: "/etc/nginx_exporter" - state: directory - owner: root - group: root - mode: u+rwX,g+rwX,o=rX - -- name: Copy the nginx_exporter web config file - ansible.builtin.template: - src: web_config.yaml.j2 - dest: /etc/nginx_exporter/web_config.yaml - owner: root - group: root - mode: 0644 - notify: restart nginx_exporter - -- name: Allow nginx_exporter port in SELinux on RedHat OS family - community.general.seport: - ports: "{{ nginx_exporter_web_listen_address.split(':')[-1] }}" - proto: tcp - setype: http_port_t - state: present - when: - - ansible_version.full is version_compare('2.4', '>=') - - ansible_selinux.status == "enabled" diff --git a/roles/nginx_exporter/tasks/install.yml b/roles/nginx_exporter/tasks/install.yml deleted file mode 100644 index efc79678..00000000 --- a/roles/nginx_exporter/tasks/install.yml +++ /dev/null @@ -1,69 +0,0 @@ ---- -- name: Create the nginx_exporter group - ansible.builtin.group: - name: "{{ nginx_exporter_system_group }}" - state: present - system: true - when: nginx_exporter_system_group not in ["root", 'www-data', 'nogroup'] - -- name: Create the nginx_exporter user - ansible.builtin.user: - name: "{{ nginx_exporter_system_user }}" - groups: "{{ nginx_exporter_system_group }}" - append: true - shell: /usr/sbin/nologin - system: true - create_home: false - home: / - when: nginx_exporter_system_user not in ["root", 'www-data', 'nobody'] - -- name: Get binary - when: - - nginx_exporter_binary_local_dir | length == 0 - - not nginx_exporter_skip_install - block: - - - name: Download nginx_exporter binary to local folder - become: false - ansible.builtin.get_url: - url: "{{ nginx_exporter_binary_url }}" - dest: "{{ nginx_exporter_archive_path }}/nginx-prometheus-exporter_{{ nginx_exporter_version }}_linux_{{ go_arch }}.tar.gz" - checksum: "sha256:{{ __nginx_exporter_checksum }}" - mode: '0644' - register: _download_binary - until: _download_binary is succeeded - retries: 5 - delay: 2 - delegate_to: localhost - check_mode: false - - - name: Unpack nginx_exporter binary - become: false - ansible.builtin.unarchive: - src: "{{ nginx_exporter_archive_path }}/nginx-prometheus-exporter_{{ nginx_exporter_version }}_linux_{{ go_arch }}.tar.gz" - dest: "{{ nginx_exporter_archive_path }}" - creates: "{{ nginx_exporter_archive_path }}/nginx-prometheus-exporter" - delegate_to: localhost - check_mode: false - - - name: Propagate nginx_exporter binaries - ansible.builtin.copy: - src: "{{ nginx_exporter_archive_path }}/nginx-prometheus-exporter" - dest: "{{ nginx_exporter_binary_install_dir }}/nginx-prometheus-exporter" - mode: 0755 - owner: root - group: root - notify: restart nginx_exporter - when: not ansible_check_mode - -- name: Propagate locally distributed nginx_exporter binary - ansible.builtin.copy: - src: "{{ nginx_exporter_binary_local_dir }}/nginx-prometheus-exporter" - dest: "{{ nginx_exporter_binary_install_dir }}/nginx-prometheus-exporter" - mode: 0755 - owner: root - group: root - when: - - nginx_exporter_binary_local_dir | length > 0 - - not nginx_exporter_skip_install - notify: restart nginx_exporter diff --git a/roles/nginx_exporter/tasks/main.yml b/roles/nginx_exporter/tasks/main.yml index 760d1b60..1f584ba1 100644 --- a/roles/nginx_exporter/tasks/main.yml +++ b/roles/nginx_exporter/tasks/main.yml @@ -2,51 +2,48 @@ - name: Preflight ansible.builtin.include_tasks: file: preflight.yml - apply: - tags: - - nginx_exporter_install - - nginx_exporter_configure - - nginx_exporter_run tags: - nginx_exporter_install - nginx_exporter_configure - nginx_exporter_run - name: Install - ansible.builtin.include_tasks: - file: install.yml - apply: - become: true - tags: - - nginx_exporter_install - when: - ( not __nginx_exporter_is_installed.stat.exists ) or - ( (__nginx_exporter_current_version_output.stderr_lines | length > 0) - and (__nginx_exporter_current_version_output.stderr_lines[0].split(" ")[2] != nginx_exporter_version) ) or - ( (__nginx_exporter_current_version_output.stdout_lines | length > 0) - and (__nginx_exporter_current_version_output.stdout_lines[0].split(" ")[2] != nginx_exporter_version) ) or - ( nginx_exporter_binary_local_dir | length > 0 ) + ansible.builtin.include_role: + name: prometheus.prometheus._common + tasks_from: install.yml + vars: + _common_local_cache_path: "{{ nginx_exporter_local_cache_path }}" + _common_binaries: "{{ _nginx_exporter_binaries }}" + _common_binary_install_dir: "{{ nginx_exporter_binary_install_dir }}" + _common_binary_url: "{{ nginx_exporter_binary_url }}" + _common_checksums_url: "{{ nginx_exporter_checksums_url }}" + _common_system_group: "{{ nginx_exporter_system_group }}" + _common_system_user: "{{ nginx_exporter_system_user }}" + _common_config_dir: "{{ nginx_exporter_config_dir }}" tags: - nginx_exporter_install - name: SELinux - ansible.builtin.include_tasks: - file: selinux.yml - apply: - become: true - tags: - - nginx_exporter_configure + ansible.builtin.include_role: + name: prometheus.prometheus._common + tasks_from: selinux.yml + vars: + _common_selinux_port: "{{ nginx_exporter_web_listen_address | urlsplit('port') }}" when: ansible_selinux.status == "enabled" tags: - nginx_exporter_configure - name: Configure - ansible.builtin.include_tasks: - file: configure.yml - apply: - become: true - tags: - - nginx_exporter_configure + ansible.builtin.include_role: + name: prometheus.prometheus._common + tasks_from: configure.yml + vars: + _common_system_user: "{{ nginx_exporter_system_user }}" + _common_system_group: "{{ nginx_exporter_system_group }}" + _common_config_dir: "{{ nginx_exporter_config_dir }}" + _common_tls_server_config: "{{ nginx_exporter_tls_server_config }}" + _common_http_server_config: "{{ nginx_exporter_http_server_config }}" + _common_basic_auth_users: "{{ nginx_exporter_basic_auth_users }}" tags: - nginx_exporter_configure diff --git a/roles/nginx_exporter/tasks/preflight.yml b/roles/nginx_exporter/tasks/preflight.yml index 8f63bf74..0d1b55f8 100644 --- a/roles/nginx_exporter/tasks/preflight.yml +++ b/roles/nginx_exporter/tasks/preflight.yml @@ -1,24 +1,8 @@ --- -- name: Assert usage of systemd as an init system - ansible.builtin.assert: - that: ansible_service_mgr == 'systemd' - msg: "This role only works with systemd" - -- name: Install package fact dependencies - become: true - ansible.builtin.package: - name: "{{ _pkg_fact_req }}" - state: present - when: (_pkg_fact_req) - vars: - _pkg_fact_req: "{% if (ansible_pkg_mgr == 'apt') %}\ - {{ ('python-apt' if ansible_python_version is version('3', '<') else 'python3-apt') }} - {% else %}\ - {% endif %}" - -- name: Gather package facts - ansible.builtin.package_facts: - when: "not 'packages' in ansible_facts" +- name: Common preflight + ansible.builtin.include_role: + name: prometheus.prometheus._common + tasks_from: preflight.yml - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -61,23 +45,6 @@ - "__nginx_exporter_cert_file.stat.exists" - "__nginx_exporter_key_file.stat.exists" -- name: Check if nginx_exporter is installed - ansible.builtin.stat: - path: "{{ nginx_exporter_binary_install_dir }}/nginx_exporter" - register: __nginx_exporter_is_installed - check_mode: false - tags: - - nginx_exporter_install - -- name: Gather currently installed nginx_exporter version (if any) - ansible.builtin.command: "{{ nginx_exporter_binary_install_dir }}/nginx_exporter --version" - changed_when: false - register: __nginx_exporter_current_version_output - check_mode: false - when: __nginx_exporter_is_installed.stat.exists - tags: - - nginx_exporter_install - - name: Discover latest version ansible.builtin.set_fact: nginx_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _nginx_exporter_repo }}/releases/latest', headers=_github_api_headers, @@ -87,24 +54,9 @@ retries: 10 when: - nginx_exporter_version == "latest" - - nginx_exporter_binary_local_dir | length == 0 - - not nginx_exporter_skip_install - -- name: Get nginx_exporter binary checksum - when: - - nginx_exporter_binary_local_dir | length == 0 - - not nginx_exporter_skip_install - block: - - name: Get checksum list from github - ansible.builtin.set_fact: - __nginx_exporter_checksums: "{{ lookup('url', nginx_exporter_checksums_url, headers=_github_api_headers, wantlist=True) | list }}" - run_once: true - until: __nginx_exporter_checksums is search('linux_' + go_arch + '.tar.gz') - retries: 10 - - - name: "Get checksum for {{ go_arch }}" - ansible.builtin.set_fact: - __nginx_exporter_checksum: "{{ item.split(' ')[0] }}" - with_items: "{{ __nginx_exporter_checksums }}" - when: - - "item.endswith('nginx-prometheus-exporter_' + nginx_exporter_version + '_linux_' + go_arch + '.tar.gz')" + tags: + - nginx_exporter + - install + - nginx_exporter_install + - download + - nginx_exporter_download diff --git a/roles/nginx_exporter/tasks/selinux.yml b/roles/nginx_exporter/tasks/selinux.yml deleted file mode 100644 index 754cbd0a..00000000 --- a/roles/nginx_exporter/tasks/selinux.yml +++ /dev/null @@ -1,23 +0,0 @@ ---- -- name: Install selinux python packages [RedHat] - ansible.builtin.package: - name: "{{ ['libselinux-python', 'policycoreutils-python'] - if ansible_python_version is version('3', '<') else - ['python3-libselinux', 'python3-policycoreutils'] }}" - state: present - register: _install_selinux_packages - until: _install_selinux_packages is success - retries: 5 - delay: 2 - when: ansible_os_family | lower == "redhat" - -- name: Install selinux python packages [clearlinux] - ansible.builtin.package: - name: sysadmin-basic - state: present - register: _install_selinux_packages - until: _install_selinux_packages is success - retries: 5 - delay: 2 - when: - - ansible_distribution | lower == "clearlinux" diff --git a/roles/nginx_exporter/templates/nginx_exporter.service.j2 b/roles/nginx_exporter/templates/nginx_exporter.service.j2 index 68650de4..54546252 100644 --- a/roles/nginx_exporter/templates/nginx_exporter.service.j2 +++ b/roles/nginx_exporter/templates/nginx_exporter.service.j2 @@ -13,7 +13,7 @@ ExecStart={{ nginx_exporter_binary_install_dir }}/nginx-prometheus-exporter \ '--web.listen-address={{ nginx_exporter_web_listen_address }}' \ '--web.telemetry-path={{ nginx_exporter_web_telemetry_path }}' \ {% if nginx_exporter_tls_server_config | length > 0 or nginx_exporter_http_server_config | length > 0 or nginx_exporter_basic_auth_users | length > 0 %} - '--web.config.file=/etc/nginx_exporter/web_config.yaml' \ + '--web.config.file={{ nginx_exporter_config_dir }}/web_config.yml' \ {% endif %} {% if nginx_exporter_plus %} '--nginx.plus' \ diff --git a/roles/nginx_exporter/templates/web_config.yaml.j2 b/roles/nginx_exporter/templates/web_config.yaml.j2 deleted file mode 100644 index 1b805f5f..00000000 --- a/roles/nginx_exporter/templates/web_config.yaml.j2 +++ /dev/null @@ -1,18 +0,0 @@ ---- -{{ ansible_managed | comment }} -{% if nginx_exporter_tls_server_config | length > 0 %} -tls_server_config: -{{ nginx_exporter_tls_server_config | to_nice_yaml | indent(2, true) }} -{% endif %} - -{% if nginx_exporter_http_server_config | length > 0 %} -http_server_config: -{{ nginx_exporter_http_server_config | to_nice_yaml | indent(2, true) }} -{% endif %} - -{% if nginx_exporter_basic_auth_users | length > 0 %} -basic_auth_users: -{% for k, v in nginx_exporter_basic_auth_users.items() %} - {{ k }}: {{ v | string | password_hash('bcrypt', ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' | shuffle(seed=inventory_hostname) | join)[:22], rounds=9) }} -{% endfor %} -{% endif %} diff --git a/roles/nginx_exporter/vars/main.yml b/roles/nginx_exporter/vars/main.yml index 87698f9e..ad678876 100644 --- a/roles/nginx_exporter/vars/main.yml +++ b/roles/nginx_exporter/vars/main.yml @@ -1,11 +1,9 @@ --- -go_arch_map: - i386: '386' - x86_64: 'amd64' - aarch64: 'arm64' - armv7l: 'armv7' - armv6l: 'armv6' - -go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}" +_nginx_exporter_go_ansible_arch: "{{ {'i386': '386', + 'x86_64': 'amd64', + 'aarch64': 'arm64', + 'armv7l': 'armv7', + 'armv6l': 'armv6'}.get(ansible_architecture, ansible_architecture) }}" _nginx_exporter_repo: "nginxinc/nginx-prometheus-exporter" _github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}" +_nginx_exporter_binaries: ['nginx-prometheus-exporter']