Im trying to create a Grafana alert using ansible task
- name: Create Grafana Alert
ansible.builtin.uri:
url: http://grafana.staged-by-discourse.com/api/v1/provisioning/alert-rules
method: POST
headers:
Content-Type: application/json
Accept: application/json
Authorization: Bearer {{grafana_api_token}}
body_format: json
body:
AlertRule:
Condition: “B”
Data: alert_data
ExecErrState: “Alerting”
FolderUID: “vjefAN37k”
NoDataState: “NoData”
OrgID: 1
RuleGroup: “Alerts Group”
Title: “GrafanaAPIDashboard”
for: “5m”
register: alert_create_response
My “alert_data” variable is assigned as follows:
[
{
“refId”: “A”,
“queryType”: “”,
“relativeTimeRange”: {
“from”: 600,
“to”: 0
},
“datasourceUid”: “Z9K_66C7z”,
“model”: {
“expr”: “up{app="prometheus"}==1”,
“hide”: false,
“intervalMs”: 1000,
“maxDataPoints”: 43200,
“refId”: “A”
}
},
{
“refId”: “B”,
“queryType”: “”,
“relativeTimeRange”: {
“from”: 0,
“to”: 0
},
“datasourceUid”: “-100”,
“model”: {
“conditions”: [
{
“evaluator”: {
“params”: [
1
],
“type”: “lt”
},
“operator”: {
“type”: “and”
},
“query”: {
“params”: [
“A”
]
},
“reducer”: {
“params”: ,
“type”: “last”
},
“type”: “query”
}
],
“datasource”: {
“type”: “expr”,
“uid”: “-100”
},
“hide”: false,
“intervalMs”: 1000,
“maxDataPoints”: 43200,
“refId”: “B”,
“type”: “classic_conditions”
}
}
].
Im getting the following error and couldn’t figure out the problem.
fatal: [localhost]: FAILED! => {“cache_control”: “no-cache”, “changed”: false, “connection”: “close”, “content_length”: “114”, “content_type”: “application/json”, “date”: “Thu, 23 Jun 2022 13:37:01 GMT”, “elapsed”: 0, “expires”: “-1”, “json”: {“message”: “invalid alert rule: no queries or expressions are found”, “traceID”: “00000000000000000000000000000000”}, “msg”: “Status code was 400 and not [200]: HTTP Error 400: Bad Request”, “pragma”: “no-cache”, “redirected”: false, “status”: 400, “url”: “http://grafana.staged-by-discourse.com/api/v1/provisioning/alert-rules”, “x_content_type_options”: “nosniff”, “x_frame_options”: “deny”, “x_xss_protection”: “1; mode=block”}
What am I doing wrong? Any help would be appreciated.