Install and uninstall Prometheus node_exporter with pyinfra.
- Idempotent
install(): system user/group, binary, systemd unit and running service in one call. - Skips re-downloading the binary when the installed version already matches, using a pyinfra fact.
uninstall()reverses everything: service, unit file, binary, user and group.- Deploy functions only, no CLI: import it into any pyinfra project.
- We needed to manage
node_exporterthe same way across every server we operate. - The solution should be reusable across pyinfra projects instead of copy-pasted between deploy scripts.
- The solution should be idempotent and skip work that has already been done.
The following steps will ensure your project is cloned properly.
- Clone repository:
git clone https://github.com/techcode-io/pyinfra-node-exporter cd pyinfra-node-exporter - Install dependencies and setup environment:
uv sync uv run poe env:configure
- To lint you have to use the workflow.
uv run poe lint- To format you have to use the workflow.
uv run poe fmt- It will format the project code using
ruff.
- To test you have to use the workflow.
- Tests are based on
pytestand run the deploy functions against a real systemd container via Podman.
uv run poe testinstall()anduninstall()are pyinfra deploy functions, wrapped with@deploy(...).- They take explicit keyword arguments instead of reading
host.data, so any inventory can use them. install()creates the system user/group, downloads thenode_exporterrelease binary, renders the systemd unit from a bundled template, then enables and starts the service.- Before downloading, it checks the currently installed version using a pyinfra fact and skips the download entirely if it already matches.
uninstall()stops and disables the service, then removes the unit file, binary, user and group.
- This project isn't published to PyPI yet, so add it as a git dependency pinned to a commit.
- Find the commit you want to pin to on the commit history, then add it to your pyinfra project.
uv add git+https://github.com/techcode-io/pyinfra-node-exporter --rev <commit-sha>
# or
pip install git+https://github.com/techcode-io/pyinfra-node-exporter@<commit-sha>- This adds the following to your
pyproject.toml, which you can also edit directly.
[project]
dependencies = ["pyinfra-node-exporter"]
[tool.uv.sources]
pyinfra-node-exporter = { git = "https://github.com/techcode-io/pyinfra-node-exporter", rev = "<commit-sha>" }- Then call
install()from a deploy script.
from pyinfra_node_exporter import install
install()- Call
uninstall()from a deploy script.
from pyinfra_node_exporter import uninstall
uninstall()- All functions accept keyword arguments; defaults match the upstream node_exporter release layout for
linux-amd64.
from pyinfra_node_exporter import DEFAULT_SERVICE_ARGS, install
install(
version="1.12.1",
system_user="node_exporter",
system_group="node_exporter",
service_args={
**DEFAULT_SERVICE_ARGS,
"web.listen-address": "0.0.0.0:9100",
},
)| Function | Parameter | Default | Description |
|---|---|---|---|
install, uninstall |
system_user |
node_exporter |
System user running the service |
install, uninstall |
system_group |
node_exporter |
System group running the service |
install |
version |
1.12.1 |
node_exporter release version to download |
install |
service_args |
DEFAULT_SERVICE_ARGS |
Dict of --flag: value (or None for a bare flag) passed to node_exporter |
If you find this project useful here's how you can help, please click the 👁️ Watch button to avoid missing notifications about new versions, and give it a 🌟 GitHub Star!
You can also contribute by:
- Sending a Pull Request with your awesome new features and bug fixed.
- Be part of the community and help resolve Issues.
The pyinfra-node-exporter project is free and open-source software licensed under the Apache-2.0 license.