├── .ansible-lint ├── .github ├── FUNDING.yml └── workflows │ ├── ci.yml │ ├── release.yml │ └── stale.yml ├── .gitignore ├── .yamllint ├── LICENSE ├── README.md ├── defaults └── main.yml ├── handlers └── main.yml ├── meta └── main.yml ├── molecule └── default │ ├── converge.yml │ └── molecule.yml ├── tasks ├── config-version.yaml └── main.yml └── templates └── node_exporter.service.j2 /.ansible-lint: -------------------------------------------------------------------------------- 1 | skip_list: 2 | - 'yaml' 3 | - 'role-name' 4 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | --- 3 | github: geerlingguy 4 | patreon: geerlingguy 5 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: CI 3 | 'on': 4 | pull_request: 5 | push: 6 | branches: 7 | - master 8 | schedule: 9 | - cron: "30 6 * * 3" 10 | 11 | defaults: 12 | run: 13 | working-directory: 'geerlingguy.node_exporter' 14 | 15 | jobs: 16 | 17 | lint: 18 | name: Lint 19 | runs-on: ubuntu-latest 20 | steps: 21 | - name: Check out the codebase. 22 | uses: actions/checkout@v4 23 | with: 24 | path: 'geerlingguy.node_exporter' 25 | 26 | - name: Set up Python 3. 27 | uses: actions/setup-python@v5 28 | with: 29 | python-version: '3.x' 30 | 31 | - name: Install test dependencies. 32 | run: pip3 install yamllint 33 | 34 | - name: Lint code. 35 | run: | 36 | yamllint . 37 | 38 | molecule: 39 | name: Molecule 40 | runs-on: ubuntu-latest 41 | strategy: 42 | matrix: 43 | distro: 44 | - rockylinux9 45 | - ubuntu2404 46 | - debian12 47 | 48 | steps: 49 | - name: Check out the codebase. 50 | uses: actions/checkout@v4 51 | with: 52 | path: 'geerlingguy.node_exporter' 53 | 54 | - name: Set up Python 3. 55 | uses: actions/setup-python@v5 56 | with: 57 | python-version: '3.x' 58 | 59 | - name: Install test dependencies. 60 | run: pip3 install ansible molecule molecule-plugins[docker] docker 61 | 62 | - name: Run Molecule tests. 63 | run: molecule test 64 | env: 65 | PY_COLORS: '1' 66 | ANSIBLE_FORCE_COLOR: '1' 67 | MOLECULE_DISTRO: ${{ matrix.distro }} 68 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # This workflow requires a GALAXY_API_KEY secret present in the GitHub 3 | # repository or organization. 4 | # 5 | # See: https://github.com/marketplace/actions/publish-ansible-role-to-galaxy 6 | # See: https://github.com/ansible/galaxy/issues/46 7 | 8 | name: Release 9 | 'on': 10 | push: 11 | tags: 12 | - '*' 13 | 14 | defaults: 15 | run: 16 | working-directory: 'geerlingguy.node_exporter' 17 | 18 | jobs: 19 | 20 | release: 21 | name: Release 22 | runs-on: ubuntu-latest 23 | steps: 24 | - name: Check out the codebase. 25 | uses: actions/checkout@v4 26 | with: 27 | path: 'geerlingguy.node_exporter' 28 | 29 | - name: Set up Python 3. 30 | uses: actions/setup-python@v5 31 | with: 32 | python-version: '3.x' 33 | 34 | - name: Install Ansible. 35 | run: pip3 install ansible-core 36 | 37 | - name: Trigger a new import on Galaxy. 38 | run: >- 39 | ansible-galaxy role import --api-key ${{ secrets.GALAXY_API_KEY }} 40 | $(echo ${{ github.repository }} | cut -d/ -f1) $(echo ${{ github.repository }} | cut -d/ -f2) 41 | -------------------------------------------------------------------------------- /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Close inactive issues 3 | 'on': 4 | schedule: 5 | - cron: "55 17 * * 6" # semi-random time 6 | 7 | jobs: 8 | close-issues: 9 | runs-on: ubuntu-latest 10 | permissions: 11 | issues: write 12 | pull-requests: write 13 | steps: 14 | - uses: actions/stale@v8 15 | with: 16 | days-before-stale: 120 17 | days-before-close: 60 18 | exempt-issue-labels: bug,pinned,security,planned 19 | exempt-pr-labels: bug,pinned,security,planned 20 | stale-issue-label: "stale" 21 | stale-pr-label: "stale" 22 | stale-issue-message: | 23 | This issue has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution! 24 | 25 | Please read [this blog post](https://www.jeffgeerling.com/blog/2020/enabling-stale-issue-bot-on-my-github-repositories) to see the reasons why I mark issues as stale. 26 | close-issue-message: | 27 | This issue has been closed due to inactivity. If you feel this is in error, please reopen the issue or file a new issue with the relevant details. 28 | stale-pr-message: | 29 | This pr has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution! 30 | 31 | Please read [this blog post](https://www.jeffgeerling.com/blog/2020/enabling-stale-issue-bot-on-my-github-repositories) to see the reasons why I mark issues as stale. 32 | close-pr-message: | 33 | This pr has been closed due to inactivity. If you feel this is in error, please reopen the issue or file a new issue with the relevant details. 34 | repo-token: ${{ secrets.GITHUB_TOKEN }} 35 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.retry 2 | */__pycache__ 3 | *.pyc 4 | .cache 5 | 6 | -------------------------------------------------------------------------------- /.yamllint: -------------------------------------------------------------------------------- 1 | --- 2 | extends: default 3 | 4 | rules: 5 | line-length: 6 | max: 200 7 | level: warning 8 | 9 | ignore: | 10 | .github/workflows/stale.yml 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 Jeff Geerling 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ansible Role: Node exporter 2 | 3 | [![CI](https://github.com/geerlingguy/ansible-role-node_exporter/actions/workflows/ci.yml/badge.svg)](https://github.com/geerlingguy/ansible-role-node_exporter/actions/workflows/ci.yml) 4 | 5 | This role installs Prometheus' [Node exporter](https://github.com/prometheus/node_exporter) on Linux hosts, and configures a systemd unit file so the service can run and be controlled by systemd. 6 | 7 | **Note**: If you're running in a Kubernetes cluster, you could run Node exporter as a DaemonSet in the cluster, instead of installing it on individual nodes. 8 | 9 | ## Requirements 10 | 11 | N/A 12 | 13 | ## Role Variables 14 | 15 | Available variables are listed below, along with default values (see `defaults/main.yml`): 16 | 17 | node_exporter_version: '0.18.1' 18 | 19 | The version of Node exporter to install. Available releases can be found on the [tags](https://github.com/prometheus/node_exporter/tags) listing in the Node exporter repository. Drop the `v` off the tag. 20 | 21 | If you change the version, the `node_exporter` binary will be replaced with the updated version, and the service will be restarted. 22 | 23 | node_exporter_arch: 'amd64' 24 | node_exporter_download_url: https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-{{ node_exporter_arch }}.tar.gz 25 | 26 | The architecture and download URL for Node exporter. If you're on a Raspberry Pi running Raspbian, you may need to override the `arch` value with `armv7`. 27 | 28 | node_exporter_bin_path: /usr/local/bin/node_exporter 29 | 30 | The path where the `node_exporter` binary will be installed. 31 | 32 | node_exporter_host: 'localhost' 33 | node_exporter_port: 9100 34 | 35 | Host and port on which node exporter will listen. 36 | 37 | node_exporter_options: '' 38 | 39 | Any additional options to pass to `node_exporter` when it starts, e.g. `--no-collector.wifi` if you want to ignore any WiFi data. 40 | 41 | node_exporter_state: started 42 | node_exporter_enabled: true 43 | 44 | Controls for the `node_exporter` service. 45 | 46 | ## Dependencies 47 | 48 | None. 49 | 50 | ## Example Playbook 51 | 52 | - hosts: all 53 | roles: 54 | - role: geerlingguy.node_exporter 55 | 56 | ## License 57 | 58 | MIT / BSD 59 | 60 | ## Author Information 61 | 62 | This role was created in 2020 by [Jeff Geerling](https://www.jeffgeerling.com/), author of [Ansible for DevOps](https://www.ansiblefordevops.com/). 63 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # Use the latest node_exporter release 3 | node_exporter_version: 'latest' 4 | 5 | # Alternatively, set a specific version 6 | # See available releases: https://github.com/prometheus/node_exporter/releases/ 7 | # node_exporter_version: '0.18.1' 8 | 9 | node_exporter_arch: 'amd64' 10 | node_exporter_download_url: https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-{{ node_exporter_arch }}.tar.gz 11 | 12 | node_exporter_bin_path: /usr/local/bin/node_exporter 13 | 14 | # Set node_exporter_host to localhost if you wish to expose node_exporter on localhost only. 15 | node_exporter_host: '' 16 | node_exporter_port: 9100 17 | node_exporter_options: '' 18 | 19 | node_exporter_state: started 20 | node_exporter_enabled: true 21 | node_exporter_restart: on-failure 22 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: restart node_exporter 3 | service: 4 | name: node_exporter 5 | state: restarted 6 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | dependencies: [] 3 | galaxy_info: 4 | author: geerlingguy 5 | role_name: node_exporter 6 | description: Prometheus' node_exporter for Linux hosts. 7 | company: Midwestern Mac, LLC 8 | license: license (MIT) 9 | min_ansible_version: 2.10 10 | platforms: 11 | - name: GenericUNIX 12 | versions: 13 | - all 14 | - name: Fedora 15 | versions: 16 | - all 17 | - name: opensuse 18 | versions: 19 | - all 20 | - name: GenericBSD 21 | versions: 22 | - all 23 | - name: FreeBSD 24 | versions: 25 | - all 26 | - name: Ubuntu 27 | versions: 28 | - all 29 | - name: SLES 30 | versions: 31 | - all 32 | - name: GenericLinux 33 | versions: 34 | - all 35 | - name: Debian 36 | versions: 37 | - all 38 | galaxy_tags: 39 | - system 40 | - docker 41 | - containers 42 | - monitoring 43 | - prometheus 44 | - node 45 | - exporter 46 | - metrics 47 | -------------------------------------------------------------------------------- /molecule/default/converge.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Converge 3 | hosts: all 4 | #become: true 5 | 6 | pre_tasks: 7 | - name: Update apt cache. 8 | apt: update_cache=yes cache_valid_time=600 9 | when: ansible_os_family == 'Debian' 10 | changed_when: false 11 | 12 | roles: 13 | - role: geerlingguy.node_exporter 14 | -------------------------------------------------------------------------------- /molecule/default/molecule.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_name_check: 1 3 | dependency: 4 | name: galaxy 5 | options: 6 | ignore-errors: true 7 | driver: 8 | name: docker 9 | platforms: 10 | - name: instance 11 | image: "geerlingguy/docker-${MOLECULE_DISTRO:-rockylinux9}-ansible:latest" 12 | command: ${MOLECULE_DOCKER_COMMAND:-""} 13 | volumes: 14 | - /sys/fs/cgroup:/sys/fs/cgroup:rw 15 | cgroupns_mode: host 16 | privileged: true 17 | pre_build_image: true 18 | provisioner: 19 | name: ansible 20 | playbooks: 21 | converge: ${MOLECULE_PLAYBOOK:-converge.yml} 22 | -------------------------------------------------------------------------------- /tasks/config-version.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Determine latest GitHub release (local) 3 | delegate_to: localhost 4 | become: false 5 | uri: 6 | url: "https://api.github.com/repos/prometheus/node_exporter/releases/latest" 7 | body_format: json 8 | register: _github_release 9 | until: _github_release.status == 200 10 | run_once: true 11 | retries: 5 12 | 13 | - name: Set node_exporter_version 14 | set_fact: 15 | node_exporter_version: "{{ _github_release.json.tag_name 16 | | regex_replace('^v?([0-9\\.]+)$', '\\1') }}" 17 | 18 | - name: Set node_exporter_download_url 19 | set_fact: 20 | node_exporter_download_url: "https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-{{ node_exporter_arch }}.tar.gz" 21 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Check current node_exporter version. 3 | command: "{{ node_exporter_bin_path }} --version" 4 | failed_when: false 5 | changed_when: false 6 | register: node_exporter_version_check 7 | 8 | - name: Configure latest version 9 | include_tasks: config-version.yaml 10 | when: > 11 | node_exporter_version is match("latest") 12 | or node_exporter_version is not defined 13 | 14 | - name: Download and unarchive node_exporter into temporary location. 15 | unarchive: 16 | src: "{{ node_exporter_download_url }}" 17 | dest: /tmp 18 | remote_src: true 19 | mode: 0755 20 | when: > 21 | node_exporter_version_check.stdout is not defined 22 | or node_exporter_version not in node_exporter_version_check.stdout 23 | register: node_exporter_download_check 24 | 25 | - name: Move node_exporter binary into place. 26 | copy: 27 | src: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ node_exporter_arch }}/node_exporter" 28 | dest: "{{ node_exporter_bin_path }}" 29 | mode: 0755 30 | remote_src: true 31 | notify: restart node_exporter 32 | when: > 33 | node_exporter_download_check is changed 34 | or node_exporter_version_check.stdout | length == 0 35 | 36 | - name: Create node_exporter user. 37 | user: 38 | name: node_exporter 39 | shell: /sbin/nologin 40 | create_home: false 41 | state: present 42 | 43 | - name: Copy the node_exporter systemd unit file. 44 | template: 45 | src: node_exporter.service.j2 46 | dest: /etc/systemd/system/node_exporter.service 47 | mode: 0644 48 | register: node_exporter_service 49 | 50 | - name: Reload systemd daemon if unit file is changed. 51 | systemd: 52 | daemon_reload: true 53 | notify: restart node_exporter 54 | when: node_exporter_service is changed 55 | 56 | - name: Ensure node_exporter is running and enabled at boot. 57 | service: 58 | name: node_exporter 59 | state: "{{ node_exporter_state }}" 60 | enabled: "{{ node_exporter_enabled }}" 61 | 62 | - name: Verify node_exporter is responding to requests. 63 | uri: 64 | url: "http://{% if node_exporter_host !='' %}{{ node_exporter_host }}{% else %}localhost{% endif %}:{{ node_exporter_port }}/" 65 | return_content: true 66 | register: metrics_output 67 | failed_when: "'Metrics' not in metrics_output.content" 68 | -------------------------------------------------------------------------------- /templates/node_exporter.service.j2: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=NodeExporter 3 | 4 | [Service] 5 | TimeoutStartSec=0 6 | User=node_exporter 7 | ExecStart={{ node_exporter_bin_path }} --web.listen-address={{ node_exporter_host }}:{{ node_exporter_port }} {{ node_exporter_options }} 8 | Restart={{ node_exporter_restart }} 9 | 10 | [Install] 11 | WantedBy=multi-user.target 12 | --------------------------------------------------------------------------------