Query Prometheus metric data through the Prometheus connector, supporting PromQL queries, result formatting, and server information retrieval.
Configure Prometheus connection information in the inventory, for example:
prometheus:
connector:
type: prometheus
url: http://prometheus-server:9090 # Prometheus address
username: admin # Optional: Basic auth username
password: password # Optional: Basic auth password
token: my-token # Optional: Bearer Token
timeout: 15s # Optional: request timeout, default 10s
headers: # Optional: custom HTTP headers
X-Custom-Header: custom-value| Parameter | Description | Type | Required | Default |
|---|---|---|---|---|
| query | PromQL query | string | Yes (when not using info) | - |
| format | Result format: raw, value, table |
string | No | raw |
| time | Query timestamp (RFC3339 or Unix timestamp) | string | No | Current time |
format:
- raw: Raw JSON
- value: Single scalar/vector value (if applicable)
- table: Table format, includes metric, value, timestamp, etc.
1. Basic query
- name: query Prometheus
prometheus:
query: up
register: prometheus_result2. Specify formatting
- name: CPU idle
prometheus:
query: sum(rate(node_cpu_seconds_total{mode='idle'}[5m]))
format: value
register: cpu_idle3. Specify time
- name: goroutines at time
prometheus:
query: go_goroutines
time: 2023-01-01T12:00:00Z
register: goroutines4. Table format
- name: node CPU as table
prometheus:
query: node_cpu_seconds_total{mode="idle"}
format: table
register: cpu_tablequeryis required when executing queries.timemust be RFC3339 (e.g.,2023-01-01T12:00:00Z) or Unix timestamp.format: tableonly applies to vector results.- HTTPS is recommended for production environments.