| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2017, Thierry Sallé (@seuf) |
| 5 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = """ |
| 12 |
module: grafana_datasource |
| 13 |
author: |
| 14 |
- Thierry Sallé (@seuf) |
| 15 |
- Martin Wang (@martinwangjian) |
| 16 |
- Rémi REY (@rrey) |
| 17 |
short_description: Manage Grafana datasources |
| 18 |
description: |
| 19 |
- Create/update/delete Grafana datasources via API. |
| 20 |
options: |
| 21 |
name: |
| 22 |
description: |
| 23 |
- The name of the datasource. |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
uid: |
| 27 |
description: |
| 28 |
- The uid of the datasource. |
| 29 |
required: false |
| 30 |
type: str |
| 31 |
ds_type: |
| 32 |
description: |
| 33 |
- The type of the datasource. |
| 34 |
- Required when C(state=present). |
| 35 |
choices: |
| 36 |
- graphite |
| 37 |
- prometheus |
| 38 |
- elasticsearch |
| 39 |
- influxdb |
| 40 |
- opentsdb |
| 41 |
- mysql |
| 42 |
- postgres |
| 43 |
- cloudwatch |
| 44 |
- alexanderzobnin-zabbix-datasource |
| 45 |
- grafana-azure-monitor-datasource |
| 46 |
- sni-thruk-datasource |
| 47 |
- camptocamp-prometheus-alertmanager-datasource |
| 48 |
- loki |
| 49 |
- redis-datasource |
| 50 |
- tempo |
| 51 |
- quickwit-quickwit-datasource |
| 52 |
- alertmanager |
| 53 |
type: str |
| 54 |
ds_url: |
| 55 |
description: |
| 56 |
- The URL of the datasource. |
| 57 |
- Required when C(state=present). |
| 58 |
type: str |
| 59 |
access: |
| 60 |
description: |
| 61 |
- The access mode for this datasource. |
| 62 |
choices: |
| 63 |
- direct |
| 64 |
- proxy |
| 65 |
default: proxy |
| 66 |
type: str |
| 67 |
database: |
| 68 |
description: |
| 69 |
- Name of the database for the datasource. |
| 70 |
- This options is required when the C(ds_type) is C(influxdb), C(elasticsearch) |
| 71 |
(index name), C(mysql) or C(postgres). |
| 72 |
required: false |
| 73 |
type: str |
| 74 |
default: '' |
| 75 |
user: |
| 76 |
description: |
| 77 |
- The datasource login user for influxdb datasources. |
| 78 |
type: str |
| 79 |
default: '' |
| 80 |
password: |
| 81 |
description: |
| 82 |
- The datasource password. |
| 83 |
- Stored as secure data, see C(enforce_secure_data) and notes! |
| 84 |
type: str |
| 85 |
default: '' |
| 86 |
basic_auth_user: |
| 87 |
description: |
| 88 |
- The datasource basic auth user. |
| 89 |
- Setting this option with basic_auth_password will enable basic auth. |
| 90 |
type: str |
| 91 |
basic_auth_password: |
| 92 |
description: |
| 93 |
- The datasource basic auth password, when C(basic auth) is C(true). |
| 94 |
- Stored as secure data, see C(enforce_secure_data) and notes! |
| 95 |
type: str |
| 96 |
with_credentials: |
| 97 |
description: |
| 98 |
- Whether credentials such as cookies or auth headers should be sent with cross-site |
| 99 |
requests. |
| 100 |
type: bool |
| 101 |
default: false |
| 102 |
tls_servername: |
| 103 |
description: |
| 104 |
- A Servername is used to verify the hostname on the returned certificate |
| 105 |
type: str |
| 106 |
tls_client_cert: |
| 107 |
description: |
| 108 |
- The client TLS certificate. |
| 109 |
- If C(tls_client_cert) and C(tls_client_key) are set, this will enable TLS authentication. |
| 110 |
- Starts with ----- BEGIN CERTIFICATE ----- |
| 111 |
- Stored as secure data, see C(enforce_secure_data) and notes! |
| 112 |
type: str |
| 113 |
tls_client_key: |
| 114 |
description: |
| 115 |
- The client TLS private key |
| 116 |
- Starts with ----- BEGIN RSA PRIVATE KEY ----- |
| 117 |
- Stored as secure data, see C(enforce_secure_data) and notes! |
| 118 |
type: str |
| 119 |
tls_ca_cert: |
| 120 |
description: |
| 121 |
- The TLS CA certificate for self signed certificates. |
| 122 |
- Only used when C(tls_client_cert) and C(tls_client_key) are set. |
| 123 |
- Stored as secure data, see C(enforce_secure_data) and notes! |
| 124 |
type: str |
| 125 |
tls_skip_verify: |
| 126 |
description: |
| 127 |
- Skip the TLS datasource certificate verification. |
| 128 |
type: bool |
| 129 |
default: false |
| 130 |
is_default: |
| 131 |
description: |
| 132 |
- Make this datasource the default one. |
| 133 |
type: bool |
| 134 |
default: false |
| 135 |
org_id: |
| 136 |
description: |
| 137 |
- Grafana organization ID in which the datasource should be created. |
| 138 |
- Not used when C(grafana_api_key) is set, because the C(grafana_api_key) only |
| 139 |
belongs to one organization. |
| 140 |
- Mutually exclusive with C(org_name). |
| 141 |
default: 1 |
| 142 |
type: int |
| 143 |
org_name: |
| 144 |
description: |
| 145 |
- Grafana organization name in which the datasource should be created. |
| 146 |
- Not used when C(grafana_api_key) is set, because the C(grafana_api_key) only |
| 147 |
belongs to one organization. |
| 148 |
- Mutually exclusive with C(org_id). |
| 149 |
type: str |
| 150 |
state: |
| 151 |
description: |
| 152 |
- Status of the datasource |
| 153 |
choices: |
| 154 |
- absent |
| 155 |
- present |
| 156 |
default: present |
| 157 |
type: str |
| 158 |
es_version: |
| 159 |
description: |
| 160 |
- Elasticsearch version (for C(ds_type = elasticsearch) only) |
| 161 |
- Version 56 is for elasticsearch 5.6+ where you can specify the C(max_concurrent_shard_requests) |
| 162 |
option. |
| 163 |
choices: |
| 164 |
- "2" |
| 165 |
- "5" |
| 166 |
- "56" |
| 167 |
- "60" |
| 168 |
- "70" |
| 169 |
- "7.7+" |
| 170 |
- "7.10+" |
| 171 |
- "8.0+" |
| 172 |
default: "7.10+" |
| 173 |
type: str |
| 174 |
max_concurrent_shard_requests: |
| 175 |
description: |
| 176 |
- Starting with elasticsearch 5.6, you can specify the max concurrent shard per |
| 177 |
requests. |
| 178 |
default: 256 |
| 179 |
type: int |
| 180 |
time_field: |
| 181 |
description: |
| 182 |
- Name of the time field in elasticsearch ds. |
| 183 |
- For example C(@timestamp). |
| 184 |
type: str |
| 185 |
default: '@timestamp' |
| 186 |
time_interval: |
| 187 |
description: |
| 188 |
- Minimum group by interval for C(influxdb), C(elasticsearch) or C(prometheus) datasources. |
| 189 |
- for example C(>10s). |
| 190 |
type: str |
| 191 |
interval: |
| 192 |
description: |
| 193 |
- For elasticsearch C(ds_type), this is the index pattern used. |
| 194 |
choices: |
| 195 |
- '' |
| 196 |
- Hourly |
| 197 |
- Daily |
| 198 |
- Weekly |
| 199 |
- Monthly |
| 200 |
- Yearly |
| 201 |
type: str |
| 202 |
default: '' |
| 203 |
tsdb_version: |
| 204 |
description: |
| 205 |
- The opentsdb version. |
| 206 |
- Use C(1) for <=2.1, C(2) for ==2.2, C(3) for ==2.3. |
| 207 |
choices: |
| 208 |
- 1 |
| 209 |
- 2 |
| 210 |
- 3 |
| 211 |
default: 1 |
| 212 |
type: int |
| 213 |
tsdb_resolution: |
| 214 |
description: |
| 215 |
- The opentsdb time resolution. |
| 216 |
choices: |
| 217 |
- millisecond |
| 218 |
- second |
| 219 |
default: second |
| 220 |
type: str |
| 221 |
sslmode: |
| 222 |
description: |
| 223 |
- SSL mode for C(postgres) datasource type. |
| 224 |
choices: |
| 225 |
- disable |
| 226 |
- require |
| 227 |
- verify-ca |
| 228 |
- verify-full |
| 229 |
type: str |
| 230 |
default: disable |
| 231 |
trends: |
| 232 |
required: false |
| 233 |
description: |
| 234 |
- Use trends or not for zabbix datasource type. |
| 235 |
type: bool |
| 236 |
default: false |
| 237 |
alertmanager_implementation: |
| 238 |
description: |
| 239 |
- The implementation to set for the alertmanager datasource type. |
| 240 |
choices: |
| 241 |
- mimir |
| 242 |
- cortex |
| 243 |
- prometheus |
| 244 |
type: str |
| 245 |
alertmanager_handle_grafana_alerts: |
| 246 |
description: |
| 247 |
- Whether Grafana should send alerts to this alertmanager. |
| 248 |
type: bool |
| 249 |
default: false |
| 250 |
aws_auth_type: |
| 251 |
description: |
| 252 |
- Type for AWS authentication for CloudWatch datasource type (authType of grafana |
| 253 |
api) |
| 254 |
default: keys |
| 255 |
choices: |
| 256 |
- keys |
| 257 |
- credentials |
| 258 |
- arn |
| 259 |
- default |
| 260 |
type: str |
| 261 |
aws_default_region: |
| 262 |
description: |
| 263 |
- AWS default region for CloudWatch datasource type |
| 264 |
default: us-east-1 |
| 265 |
type: str |
| 266 |
choices: |
| 267 |
- ap-northeast-1 |
| 268 |
- ap-northeast-2 |
| 269 |
- ap-southeast-1 |
| 270 |
- ap-southeast-2 |
| 271 |
- ap-south-1 |
| 272 |
- ca-central-1 |
| 273 |
- cn-north-1 |
| 274 |
- cn-northwest-1 |
| 275 |
- eu-central-1 |
| 276 |
- eu-west-1 |
| 277 |
- eu-west-2 |
| 278 |
- eu-west-3 |
| 279 |
- sa-east-1 |
| 280 |
- us-east-1 |
| 281 |
- us-east-2 |
| 282 |
- us-gov-west-1 |
| 283 |
- us-west-1 |
| 284 |
- us-west-2 |
| 285 |
aws_credentials_profile: |
| 286 |
description: |
| 287 |
- Profile for AWS credentials for CloudWatch datasource type when C(aws_auth_type) |
| 288 |
is C(credentials) |
| 289 |
default: '' |
| 290 |
required: false |
| 291 |
type: str |
| 292 |
aws_access_key: |
| 293 |
description: |
| 294 |
- AWS access key for CloudWatch datasource type when C(aws_auth_type) is C(keys) |
| 295 |
- Stored as secure data, see C(enforce_secure_data) and notes! |
| 296 |
default: '' |
| 297 |
required: false |
| 298 |
type: str |
| 299 |
aws_secret_key: |
| 300 |
description: |
| 301 |
- AWS secret key for CloudWatch datasource type when C(aws_auth_type) is C(keys) |
| 302 |
- Stored as secure data, see C(enforce_secure_data) and notes! |
| 303 |
default: '' |
| 304 |
required: false |
| 305 |
type: str |
| 306 |
aws_assume_role_arn: |
| 307 |
description: |
| 308 |
- AWS IAM role arn to assume for CloudWatch datasource type when C(aws_auth_type) |
| 309 |
is C(arn) |
| 310 |
default: '' |
| 311 |
required: false |
| 312 |
type: str |
| 313 |
aws_custom_metrics_namespaces: |
| 314 |
description: |
| 315 |
- Namespaces of Custom Metrics for CloudWatch datasource type |
| 316 |
required: false |
| 317 |
type: str |
| 318 |
azure_cloud: |
| 319 |
description: |
| 320 |
- The national cloud for your Azure account |
| 321 |
default: 'azuremonitor' |
| 322 |
required: false |
| 323 |
type: str |
| 324 |
choices: |
| 325 |
- azuremonitor |
| 326 |
- chinaazuremonitor |
| 327 |
- govazuremonitor |
| 328 |
- germanyazuremonitor |
| 329 |
azure_tenant: |
| 330 |
description: |
| 331 |
- The directory/tenant ID for the Azure AD app registration to use for authentication |
| 332 |
required: false |
| 333 |
type: str |
| 334 |
azure_client: |
| 335 |
description: |
| 336 |
- The application/client ID for the Azure AD app registration to use for authentication. |
| 337 |
required: false |
| 338 |
type: str |
| 339 |
azure_secret: |
| 340 |
description: |
| 341 |
- The application client secret for the Azure AD app registration to use for auth |
| 342 |
required: false |
| 343 |
type: str |
| 344 |
zabbix_user: |
| 345 |
description: |
| 346 |
- User for Zabbix API |
| 347 |
required: false |
| 348 |
type: str |
| 349 |
zabbix_password: |
| 350 |
description: |
| 351 |
- Password for Zabbix API |
| 352 |
required: false |
| 353 |
type: str |
| 354 |
additional_json_data: |
| 355 |
description: |
| 356 |
- Defined data is used for datasource jsonData |
| 357 |
- Data may be overridden by specifically defined parameters (like zabbix_user) |
| 358 |
required: false |
| 359 |
type: dict |
| 360 |
default: {} |
| 361 |
additional_secure_json_data: |
| 362 |
description: |
| 363 |
- Defined data is used for datasource secureJsonData |
| 364 |
- Data may be overridden by specifically defined parameters (like tls_client_cert) |
| 365 |
- Stored as secure data, see C(enforce_secure_data) and notes! |
| 366 |
required: false |
| 367 |
type: dict |
| 368 |
default: {} |
| 369 |
enforce_secure_data: |
| 370 |
description: |
| 371 |
- Secure data is not updated per default (see notes!) |
| 372 |
- To update secure data you have to enable this option! |
| 373 |
- Enabling this, the task will always report changed=True |
| 374 |
required: false |
| 375 |
type: bool |
| 376 |
default: false |
| 377 |
extends_documentation_fragment: |
| 378 |
- community.grafana.basic_auth |
| 379 |
- community.grafana.api_key |
| 380 |
notes: |
| 381 |
- Secure data will get encrypted by the Grafana API, thus it can not be compared on subsequent runs. To workaround this, secure |
| 382 |
data will not be updated after initial creation! To force the secure data update you have to set I(enforce_secure_data=True). |
| 383 |
- Hint, with the C(enforce_secure_data) always reporting changed=True, you might just do one Task updating the datasource without |
| 384 |
any secure data and make a separate playbook/task also changing the secure data. This way it will not break any workflow. |
| 385 |
""" |
| 386 |
|
| 387 |
EXAMPLES = """ |
| 388 |
--- |
| 389 |
- name: Create elasticsearch datasource |
| 390 |
community.grafana.grafana_datasource: |
| 391 |
name: "datasource-elastic" |
| 392 |
grafana_url: "https://grafana.company.com" |
| 393 |
grafana_user: "admin" |
| 394 |
grafana_password: "xxxxxx" |
| 395 |
org_id: "1" |
| 396 |
ds_type: "elasticsearch" |
| 397 |
ds_url: "https://elastic.company.com:9200" |
| 398 |
database: "[logstash_]YYYY.MM.DD" |
| 399 |
basic_auth_user: "grafana" |
| 400 |
basic_auth_password: "******" |
| 401 |
time_field: "@timestamp" |
| 402 |
time_interval: "1m" |
| 403 |
interval: "Daily" |
| 404 |
es_version: 56 |
| 405 |
max_concurrent_shard_requests: 42 |
| 406 |
tls_ca_cert: "/etc/ssl/certs/ca.pem" |
| 407 |
|
| 408 |
- name: Create influxdb datasource |
| 409 |
community.grafana.grafana_datasource: |
| 410 |
name: "datasource-influxdb" |
| 411 |
grafana_url: "https://grafana.company.com" |
| 412 |
grafana_user: "admin" |
| 413 |
grafana_password: "xxxxxx" |
| 414 |
org_id: "1" |
| 415 |
ds_type: "influxdb" |
| 416 |
ds_url: "https://influx.company.com:8086" |
| 417 |
database: "telegraf" |
| 418 |
time_interval: ">10s" |
| 419 |
tls_ca_cert: "/etc/ssl/certs/ca.pem" |
| 420 |
|
| 421 |
- name: Create influxdbv2 datasource using fluxql |
| 422 |
community.grafana.grafana_datasource: |
| 423 |
name: "datasource-influxdb-flux" |
| 424 |
grafana_url: "https://grafana.company.com" |
| 425 |
grafana_user: "admin" |
| 426 |
grafana_password: "xxxxxx" |
| 427 |
org_id: "1" |
| 428 |
ds_type: "influxdb" |
| 429 |
ds_url: "https://influx.company.com:8086" |
| 430 |
additional_json_data: |
| 431 |
version: "Flux" |
| 432 |
organization: "organization" |
| 433 |
defaultBucket: "bucket" |
| 434 |
tlsSkipVerify: false |
| 435 |
additional_secure_json_data: |
| 436 |
token: "token" |
| 437 |
|
| 438 |
- name: Create postgres datasource |
| 439 |
community.grafana.grafana_datasource: |
| 440 |
name: "datasource-postgres" |
| 441 |
grafana_url: "https://grafana.company.com" |
| 442 |
grafana_user: "admin" |
| 443 |
grafana_password: "xxxxxx" |
| 444 |
org_id: "1" |
| 445 |
ds_type: "postgres" |
| 446 |
ds_url: "postgres.company.com:5432" |
| 447 |
database: "db" |
| 448 |
user: "postgres" |
| 449 |
sslmode: "verify-full" |
| 450 |
additional_json_data: |
| 451 |
postgresVersion: 12 |
| 452 |
timescaledb: false |
| 453 |
additional_secure_json_data: |
| 454 |
password: "iampgroot" |
| 455 |
|
| 456 |
- name: Create cloudwatch datasource |
| 457 |
community.grafana.grafana_datasource: |
| 458 |
name: "datasource-cloudwatch" |
| 459 |
grafana_url: "https://grafana.company.com" |
| 460 |
grafana_user: "admin" |
| 461 |
grafana_password: "xxxxxx" |
| 462 |
org_id: "1" |
| 463 |
ds_type: "cloudwatch" |
| 464 |
ds_url: "http://monitoring.us-west-1.amazonaws.com" |
| 465 |
aws_auth_type: "keys" |
| 466 |
aws_default_region: "us-west-1" |
| 467 |
aws_access_key: "speakFriendAndEnter" |
| 468 |
aws_secret_key: "mel10n" |
| 469 |
aws_custom_metrics_namespaces: "n1,n2" |
| 470 |
|
| 471 |
- name: grafana - add thruk datasource |
| 472 |
community.grafana.grafana_datasource: |
| 473 |
name: "datasource-thruk" |
| 474 |
grafana_url: "https://grafana.company.com" |
| 475 |
grafana_user: "admin" |
| 476 |
grafana_password: "xxxxxx" |
| 477 |
org_id: "1" |
| 478 |
ds_type: "sni-thruk-datasource" |
| 479 |
ds_url: "https://thruk.company.com/sitename/thruk" |
| 480 |
basic_auth_user: "thruk-user" |
| 481 |
basic_auth_password: "******" |
| 482 |
|
| 483 |
# handle secure data - workflow example |
| 484 |
# this will create/update the datasource but dont update the secure data on updates |
| 485 |
# so you can assert if all tasks are changed=False |
| 486 |
- name: create prometheus datasource |
| 487 |
community.grafana.grafana_datasource: |
| 488 |
name: openshift_prometheus |
| 489 |
ds_type: prometheus |
| 490 |
ds_url: https://openshift-monitoring.company.com |
| 491 |
access: proxy |
| 492 |
tls_skip_verify: true |
| 493 |
additional_json_data: |
| 494 |
httpHeaderName1: "Authorization" |
| 495 |
additional_secure_json_data: |
| 496 |
httpHeaderValue1: "Bearer ihavenogroot" |
| 497 |
|
| 498 |
# in a separate task or even play you then can force to update |
| 499 |
# and assert if each datasource is reporting changed=True |
| 500 |
- name: update prometheus datasource |
| 501 |
community.grafana.grafana_datasource: |
| 502 |
name: openshift_prometheus |
| 503 |
ds_type: prometheus |
| 504 |
ds_url: https://openshift-monitoring.company.com |
| 505 |
access: proxy |
| 506 |
tls_skip_verify: true |
| 507 |
additional_json_data: |
| 508 |
httpHeaderName1: "Authorization" |
| 509 |
additional_secure_json_data: |
| 510 |
httpHeaderValue1: "Bearer ihavenogroot" |
| 511 |
enforce_secure_data: true |
| 512 |
""" |
| 513 |
|
| 514 |
RETURN = """ |
| 515 |
--- |
| 516 |
datasource: |
| 517 |
description: datasource created/updated by module |
| 518 |
returned: changed |
| 519 |
type: dict |
| 520 |
sample: { "access": "proxy", |
| 521 |
"basicAuth": false, |
| 522 |
"database": "test_*", |
| 523 |
"id": 1035, |
| 524 |
"isDefault": false, |
| 525 |
"jsonData": { |
| 526 |
"esVersion": 5, |
| 527 |
"timeField": "@timestamp", |
| 528 |
"timeInterval": "10s", |
| 529 |
}, |
| 530 |
"secureJsonFields": { |
| 531 |
"JustASecureTest": true, |
| 532 |
}, |
| 533 |
"name": "grafana_datasource_test", |
| 534 |
"orgId": 1, |
| 535 |
"type": "elasticsearch", |
| 536 |
"url": "http://elastic.company.com:9200", |
| 537 |
"user": "", |
| 538 |
"password": "", |
| 539 |
"withCredentials": false } |
| 540 |
""" |
| 541 |
|
| 542 |
import json |
| 543 |
|
| 544 |
from ansible.module_utils.basic import AnsibleModule |
| 545 |
from ansible.module_utils.six.moves.urllib.parse import quote |
| 546 |
from ansible.module_utils.urls import fetch_url, basic_auth_header |
| 547 |
from ansible_collections.community.grafana.plugins.module_utils import base |
| 548 |
|
| 549 |
|
| 550 |
ES_VERSION_MAPPING = { |
| 551 |
"7.7+": "7.7.0", |
| 552 |
"7.10+": "7.10.0", |
| 553 |
"8.0+": "8.0.0", |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
def compare_datasources(new, current, compareSecureData=True): |
| 558 |
if new.get("uid") is None: |
| 559 |
new.pop("uid", None) |
| 560 |
current.pop("uid", None) |
| 561 |
|
| 562 |
for field in [ |
| 563 |
"apiVersion", |
| 564 |
"basicAuthPassword", |
| 565 |
"id", |
| 566 |
"password", |
| 567 |
"readOnly", |
| 568 |
"typeLogoUrl", |
| 569 |
"version", |
| 570 |
]: |
| 571 |
current.pop(field, None) |
| 572 |
|
| 573 |
if not current.get("basicAuth", True): |
| 574 |
current.pop("basicAuthUser", None) |
| 575 |
|
| 576 |
if ( |
| 577 |
current.get("type") == "grafana-postgresql-datasource" |
| 578 |
and new.get("type") == "postgres" |
| 579 |
): |
| 580 |
new.pop("type", None) |
| 581 |
current.pop("type", None) |
| 582 |
|
| 583 |
# check if secureJsonData should be compared |
| 584 |
if not compareSecureData: |
| 585 |
# if we should ignore it just drop alltogether |
| 586 |
new.pop("secureJsonData", None) |
| 587 |
new.pop("secureJsonFields", None) |
| 588 |
current.pop("secureJsonData", None) |
| 589 |
current.pop("secureJsonFields", None) |
| 590 |
else: |
| 591 |
# handle secureJsonData/secureJsonFields, some current facts: |
| 592 |
# - secureJsonFields is reporting each field set as true |
| 593 |
# - secureJsonFields once set cant be removed (DS has to be deleted) |
| 594 |
if not new.get("secureJsonData"): |
| 595 |
# secureJsonData is not provided so just remove both for comparision |
| 596 |
new.pop("secureJsonData", None) |
| 597 |
current.pop("secureJsonFields", None) |
| 598 |
else: |
| 599 |
# we have some secure data so just "rename" secureJsonFields for comparison as it will change anyhow everytime |
| 600 |
current["secureJsonData"] = current.pop("secureJsonFields") |
| 601 |
|
| 602 |
return dict(before=current, after=new) |
| 603 |
|
| 604 |
|
| 605 |
def get_datasource_payload(data, org_id=None): |
| 606 |
payload = { |
| 607 |
"orgId": data["org_id"] if org_id is None else org_id, |
| 608 |
"name": data["name"], |
| 609 |
"uid": data["uid"], |
| 610 |
"type": data["ds_type"], |
| 611 |
"access": data["access"], |
| 612 |
"url": data["ds_url"], |
| 613 |
"database": data["database"], |
| 614 |
"withCredentials": data["with_credentials"], |
| 615 |
"isDefault": data["is_default"], |
| 616 |
"user": data["user"], |
| 617 |
"jsonData": data["additional_json_data"], |
| 618 |
"secureJsonData": data["additional_secure_json_data"], |
| 619 |
} |
| 620 |
|
| 621 |
json_data = payload["jsonData"] |
| 622 |
secure_json_data = payload["secureJsonData"] |
| 623 |
|
| 624 |
# define password |
| 625 |
if data.get("password"): |
| 626 |
secure_json_data["password"] = data["password"] |
| 627 |
|
| 628 |
# define basic auth |
| 629 |
if ( |
| 630 |
"basic_auth_user" in data |
| 631 |
and data["basic_auth_user"] |
| 632 |
and "basic_auth_password" in data |
| 633 |
and data["basic_auth_password"] |
| 634 |
): |
| 635 |
payload["basicAuth"] = True |
| 636 |
payload["basicAuthUser"] = data["basic_auth_user"] |
| 637 |
secure_json_data["basicAuthPassword"] = data["basic_auth_password"] |
| 638 |
else: |
| 639 |
payload["basicAuth"] = False |
| 640 |
|
| 641 |
# define tls auth |
| 642 |
if data.get("tls_client_cert") and data.get("tls_client_key"): |
| 643 |
json_data["tlsAuth"] = True |
| 644 |
if data.get("tls_ca_cert"): |
| 645 |
secure_json_data["tlsCACert"] = data["tls_ca_cert"] |
| 646 |
json_data["tlsAuthWithCACert"] = True |
| 647 |
json_data["serverName"] = data["tls_servername"] |
| 648 |
secure_json_data["tlsClientCert"] = data["tls_client_cert"] |
| 649 |
secure_json_data["tlsClientKey"] = data["tls_client_key"] |
| 650 |
else: |
| 651 |
json_data["tlsAuth"] = False |
| 652 |
json_data["tlsAuthWithCACert"] = False |
| 653 |
if data.get("tls_ca_cert"): |
| 654 |
json_data["tlsAuthWithCACert"] = True |
| 655 |
secure_json_data["tlsCACert"] = data["tls_ca_cert"] |
| 656 |
|
| 657 |
if data.get("tls_skip_verify"): |
| 658 |
json_data["tlsSkipVerify"] = True |
| 659 |
|
| 660 |
# datasource type related parameters |
| 661 |
if data["ds_type"] == "alertmanager": |
| 662 |
json_data["implementation"] = data["alertmanager_implementation"] |
| 663 |
json_data["handleGrafanaManagedAlerts"] = data[ |
| 664 |
"alertmanager_handle_grafana_alerts" |
| 665 |
] |
| 666 |
|
| 667 |
if data["ds_type"] == "elasticsearch": |
| 668 |
json_data["maxConcurrentShardRequests"] = data["max_concurrent_shard_requests"] |
| 669 |
json_data["timeField"] = data["time_field"] |
| 670 |
if data.get("interval"): |
| 671 |
json_data["interval"] = data["interval"] |
| 672 |
|
| 673 |
# Handle changes in es_version format in Grafana < 8.x which used to |
| 674 |
# be integers and is now semver format |
| 675 |
try: |
| 676 |
es_version = int(data["es_version"]) |
| 677 |
if es_version < 56: |
| 678 |
json_data.pop("maxConcurrentShardRequests") |
| 679 |
except ValueError: |
| 680 |
# Retrieve the Semver format expected by API |
| 681 |
es_version = ES_VERSION_MAPPING.get(data["es_version"]) |
| 682 |
json_data["esVersion"] = es_version |
| 683 |
|
| 684 |
if data["ds_type"] in ["elasticsearch", "influxdb", "prometheus"]: |
| 685 |
if data.get("time_interval"): |
| 686 |
json_data["timeInterval"] = data["time_interval"] |
| 687 |
|
| 688 |
if data["ds_type"] == "opentsdb": |
| 689 |
json_data["tsdbVersion"] = data["tsdb_version"] |
| 690 |
if data["tsdb_resolution"] == "second": |
| 691 |
json_data["tsdbResolution"] = 1 |
| 692 |
else: |
| 693 |
json_data["tsdbResolution"] = 2 |
| 694 |
|
| 695 |
if data["ds_type"] == "postgres": |
| 696 |
json_data["sslmode"] = data["sslmode"] |
| 697 |
|
| 698 |
if data["ds_type"] == "alexanderzobnin-zabbix-datasource": |
| 699 |
if data.get("trends"): |
| 700 |
json_data["trends"] = True |
| 701 |
json_data["username"] = data["zabbix_user"] |
| 702 |
json_data["password"] = data["zabbix_password"] |
| 703 |
|
| 704 |
if data["ds_type"] == "grafana-azure-monitor-datasource": |
| 705 |
json_data["tenantId"] = data["azure_tenant"] |
| 706 |
json_data["clientId"] = data["azure_client"] |
| 707 |
json_data["cloudName"] = data["azure_cloud"] |
| 708 |
json_data["clientsecret"] = "clientsecret" |
| 709 |
if data.get("azure_secret"): |
| 710 |
secure_json_data["clientSecret"] = data["azure_secret"] |
| 711 |
|
| 712 |
if data["ds_type"] == "cloudwatch": |
| 713 |
if data.get("aws_credentials_profile"): |
| 714 |
payload["database"] = data.get("aws_credentials_profile") |
| 715 |
|
| 716 |
json_data["authType"] = data["aws_auth_type"] |
| 717 |
json_data["defaultRegion"] = data["aws_default_region"] |
| 718 |
|
| 719 |
if data.get("aws_custom_metrics_namespaces"): |
| 720 |
json_data["customMetricsNamespaces"] = data.get( |
| 721 |
"aws_custom_metrics_namespaces" |
| 722 |
) |
| 723 |
if data.get("aws_assume_role_arn"): |
| 724 |
json_data["assumeRoleArn"] = data.get("aws_assume_role_arn") |
| 725 |
if data.get("aws_access_key") and data.get("aws_secret_key"): |
| 726 |
secure_json_data["accessKey"] = data.get("aws_access_key") |
| 727 |
secure_json_data["secretKey"] = data.get("aws_secret_key") |
| 728 |
|
| 729 |
payload["jsonData"] = json_data |
| 730 |
payload["secureJsonData"] = secure_json_data |
| 731 |
return payload |
| 732 |
|
| 733 |
|
| 734 |
class GrafanaInterface(object): |
| 735 |
def __init__(self, module): |
| 736 |
self._module = module |
| 737 |
self.grafana_url = base.clean_url(module.params.get("url")) |
| 738 |
self.org_id = None |
| 739 |
# {{{ Authentication header |
| 740 |
self.headers = {"Content-Type": "application/json"} |
| 741 |
if module.params.get("grafana_api_key", None): |
| 742 |
self.headers["Authorization"] = ( |
| 743 |
"Bearer %s" % module.params["grafana_api_key"] |
| 744 |
) |
| 745 |
else: |
| 746 |
self.headers["Authorization"] = basic_auth_header( |
| 747 |
module.params["url_username"], module.params["url_password"] |
| 748 |
) |
| 749 |
self.org_id = ( |
| 750 |
self.organization_by_name(module.params["org_name"]) |
| 751 |
if module.params["org_name"] |
| 752 |
else module.params["org_id"] |
| 753 |
) |
| 754 |
self.switch_organization(self.org_id) |
| 755 |
# }}} |
| 756 |
|
| 757 |
def _send_request(self, url, data=None, headers=None, method="GET"): |
| 758 |
if data is not None: |
| 759 |
data = json.dumps(data, sort_keys=True) |
| 760 |
if not headers: |
| 761 |
headers = [] |
| 762 |
|
| 763 |
full_url = "{grafana_url}{path}".format(grafana_url=self.grafana_url, path=url) |
| 764 |
resp, info = fetch_url( |
| 765 |
self._module, full_url, data=data, headers=headers, method=method |
| 766 |
) |
| 767 |
status_code = info["status"] |
| 768 |
if status_code == 404: |
| 769 |
return None |
| 770 |
elif status_code == 401: |
| 771 |
self._module.fail_json( |
| 772 |
failed=True, |
| 773 |
msg="Unauthorized to perform action '%s' on '%s'" % (method, full_url), |
| 774 |
) |
| 775 |
elif status_code == 403: |
| 776 |
self._module.fail_json(failed=True, msg="Permission Denied") |
| 777 |
elif status_code == 200: |
| 778 |
return self._module.from_json(resp.read()) |
| 779 |
self._module.fail_json( |
| 780 |
failed=True, |
| 781 |
msg="Grafana API answered with HTTP %d for url %s and data %s" |
| 782 |
% (status_code, url, data), |
| 783 |
) |
| 784 |
|
| 785 |
def switch_organization(self, org_id): |
| 786 |
url = "/api/user/using/%d" % org_id |
| 787 |
self._send_request(url, headers=self.headers, method="POST") |
| 788 |
|
| 789 |
def organization_by_name(self, org_name): |
| 790 |
url = "/api/user/orgs" |
| 791 |
organizations = self._send_request(url, headers=self.headers, method="GET") |
| 792 |
orga = next((org for org in organizations if org["name"] == org_name)) |
| 793 |
if orga: |
| 794 |
return orga["orgId"] |
| 795 |
|
| 796 |
return self._module.fail_json( |
| 797 |
failed=True, msg="Current user isn't member of organization: %s" % org_name |
| 798 |
) |
| 799 |
|
| 800 |
def datasource_by_name(self, name): |
| 801 |
url = "/api/datasources/name/%s" % quote(name, safe="") |
| 802 |
return self._send_request(url, headers=self.headers, method="GET") |
| 803 |
|
| 804 |
def delete_datasource(self, name): |
| 805 |
url = "/api/datasources/name/%s" % quote(name, safe="") |
| 806 |
self._send_request(url, headers=self.headers, method="DELETE") |
| 807 |
|
| 808 |
def update_datasource(self, ds_id, data): |
| 809 |
url = "/api/datasources/%d" % ds_id |
| 810 |
self._send_request(url, data=data, headers=self.headers, method="PUT") |
| 811 |
|
| 812 |
def create_datasource(self, data): |
| 813 |
url = "/api/datasources" |
| 814 |
self._send_request(url, data=data, headers=self.headers, method="POST") |
| 815 |
|
| 816 |
|
| 817 |
def setup_module_object(): |
| 818 |
argument_spec = base.grafana_argument_spec() |
| 819 |
|
| 820 |
argument_spec.update( |
| 821 |
name=dict(required=True, type="str"), |
| 822 |
uid=dict(type="str"), |
| 823 |
ds_type=dict( |
| 824 |
choices=[ |
| 825 |
"graphite", |
| 826 |
"prometheus", |
| 827 |
"elasticsearch", |
| 828 |
"influxdb", |
| 829 |
"opentsdb", |
| 830 |
"mysql", |
| 831 |
"postgres", |
| 832 |
"cloudwatch", |
| 833 |
"alexanderzobnin-zabbix-datasource", |
| 834 |
"grafana-azure-monitor-datasource", |
| 835 |
"camptocamp-prometheus-alertmanager-datasource", |
| 836 |
"sni-thruk-datasource", |
| 837 |
"redis-datasource", |
| 838 |
"loki", |
| 839 |
"tempo", |
| 840 |
"quickwit-quickwit-datasource", |
| 841 |
"alertmanager", |
| 842 |
] |
| 843 |
), |
| 844 |
ds_url=dict(type="str"), |
| 845 |
access=dict(default="proxy", choices=["proxy", "direct"]), |
| 846 |
database=dict(type="str", default=""), |
| 847 |
user=dict(default="", type="str"), |
| 848 |
password=dict(default="", no_log=True, type="str"), |
| 849 |
basic_auth_user=dict(type="str"), |
| 850 |
basic_auth_password=dict(type="str", no_log=True), |
| 851 |
with_credentials=dict(default=False, type="bool"), |
| 852 |
tls_servername=dict(type="str"), |
| 853 |
tls_client_cert=dict(type="str", no_log=True), |
| 854 |
tls_client_key=dict(type="str", no_log=True), |
| 855 |
tls_ca_cert=dict(type="str", no_log=True), |
| 856 |
tls_skip_verify=dict(type="bool", default=False), |
| 857 |
is_default=dict(default=False, type="bool"), |
| 858 |
org_id=dict(default=1, type="int"), |
| 859 |
org_name=dict(type="str"), |
| 860 |
es_version=dict( |
| 861 |
type="str", |
| 862 |
default="7.10+", |
| 863 |
choices=["2", "5", "56", "60", "70", "7.7+", "7.10+", "8.0+"], |
| 864 |
), |
| 865 |
max_concurrent_shard_requests=dict(type="int", default=256), |
| 866 |
time_field=dict(default="@timestamp", type="str"), |
| 867 |
time_interval=dict(type="str"), |
| 868 |
interval=dict( |
| 869 |
type="str", |
| 870 |
choices=["", "Hourly", "Daily", "Weekly", "Monthly", "Yearly"], |
| 871 |
default="", |
| 872 |
), |
| 873 |
tsdb_version=dict(type="int", default=1, choices=[1, 2, 3]), |
| 874 |
tsdb_resolution=dict( |
| 875 |
type="str", default="second", choices=["second", "millisecond"] |
| 876 |
), |
| 877 |
sslmode=dict( |
| 878 |
default="disable", |
| 879 |
choices=["disable", "require", "verify-ca", "verify-full"], |
| 880 |
), |
| 881 |
trends=dict(default=False, type="bool"), |
| 882 |
alertmanager_implementation=dict(choices=["mimir", "cortex", "prometheus"]), |
| 883 |
alertmanager_handle_grafana_alerts=dict(default=False, type="bool"), |
| 884 |
aws_auth_type=dict( |
| 885 |
default="keys", choices=["keys", "credentials", "arn", "default"] |
| 886 |
), |
| 887 |
aws_default_region=dict( |
| 888 |
default="us-east-1", |
| 889 |
choices=[ |
| 890 |
"ap-northeast-1", |
| 891 |
"ap-northeast-2", |
| 892 |
"ap-southeast-1", |
| 893 |
"ap-southeast-2", |
| 894 |
"ap-south-1", |
| 895 |
"ca-central-1", |
| 896 |
"cn-north-1", |
| 897 |
"cn-northwest-1", |
| 898 |
"eu-central-1", |
| 899 |
"eu-west-1", |
| 900 |
"eu-west-2", |
| 901 |
"eu-west-3", |
| 902 |
"sa-east-1", |
| 903 |
"us-east-1", |
| 904 |
"us-east-2", |
| 905 |
"us-gov-west-1", |
| 906 |
"us-west-1", |
| 907 |
"us-west-2", |
| 908 |
], |
| 909 |
), |
| 910 |
aws_access_key=dict(default="", no_log=True, type="str"), |
| 911 |
aws_secret_key=dict(default="", no_log=True, type="str"), |
| 912 |
aws_credentials_profile=dict(default="", type="str"), |
| 913 |
aws_assume_role_arn=dict(default="", type="str"), |
| 914 |
aws_custom_metrics_namespaces=dict(type="str"), |
| 915 |
azure_cloud=dict( |
| 916 |
type="str", |
| 917 |
default="azuremonitor", |
| 918 |
choices=[ |
| 919 |
"azuremonitor", |
| 920 |
"chinaazuremonitor", |
| 921 |
"govazuremonitor", |
| 922 |
"germanyazuremonitor", |
| 923 |
], |
| 924 |
), |
| 925 |
azure_tenant=dict(type="str"), |
| 926 |
azure_client=dict(type="str"), |
| 927 |
azure_secret=dict(type="str", no_log=True), |
| 928 |
zabbix_user=dict(type="str"), |
| 929 |
zabbix_password=dict(type="str", no_log=True), |
| 930 |
additional_json_data=dict(type="dict", default={}, required=False), |
| 931 |
additional_secure_json_data=dict(type="dict", default={}, required=False), |
| 932 |
enforce_secure_data=dict(type="bool", default=False, required=False), |
| 933 |
) |
| 934 |
|
| 935 |
module = AnsibleModule( |
| 936 |
argument_spec=argument_spec, |
| 937 |
supports_check_mode=False, |
| 938 |
required_together=[ |
| 939 |
["url_username", "url_password", "org_id"], |
| 940 |
["tls_client_cert", "tls_client_key"], |
| 941 |
], |
| 942 |
mutually_exclusive=[ |
| 943 |
["url_username", "grafana_api_key"], |
| 944 |
["tls_ca_cert", "tls_skip_verify"], |
| 945 |
["org_id", "org_name"], |
| 946 |
], |
| 947 |
required_if=[ |
| 948 |
["state", "present", ["ds_type", "ds_url"]], |
| 949 |
["ds_type", "opentsdb", ["tsdb_version", "tsdb_resolution"]], |
| 950 |
["ds_type", "influxdb", ["database"]], |
| 951 |
[ |
| 952 |
"ds_type", |
| 953 |
"elasticsearch", |
| 954 |
["database", "es_version", "time_field", "interval"], |
| 955 |
], |
| 956 |
["ds_type", "mysql", ["database"]], |
| 957 |
["ds_type", "postgres", ["database", "sslmode"]], |
| 958 |
["ds_type", "cloudwatch", ["aws_auth_type", "aws_default_region"]], |
| 959 |
["es_version", "56", ["max_concurrent_shard_requests"]], |
| 960 |
["es_version", "60", ["max_concurrent_shard_requests"]], |
| 961 |
["es_version", "70", ["max_concurrent_shard_requests"]], |
| 962 |
], |
| 963 |
) |
| 964 |
return module |
| 965 |
|
| 966 |
|
| 967 |
def main(): |
| 968 |
module = setup_module_object() |
| 969 |
|
| 970 |
state = module.params["state"] |
| 971 |
name = module.params["name"] |
| 972 |
enforce_secure_data = module.params["enforce_secure_data"] |
| 973 |
|
| 974 |
grafana_iface = GrafanaInterface(module) |
| 975 |
ds = grafana_iface.datasource_by_name(name) |
| 976 |
|
| 977 |
if state == "present": |
| 978 |
payload = get_datasource_payload(module.params, grafana_iface.org_id) |
| 979 |
if ds is None: |
| 980 |
grafana_iface.create_datasource(payload) |
| 981 |
ds = grafana_iface.datasource_by_name(name) |
| 982 |
if ds.get("isDefault") != module.params["is_default"]: |
| 983 |
grafana_iface.update_datasource(ds.get("id"), payload) |
| 984 |
ds = grafana_iface.datasource_by_name(name) |
| 985 |
module.exit_json( |
| 986 |
changed=True, datasource=ds, msg="Datasource %s created" % name |
| 987 |
) |
| 988 |
else: |
| 989 |
diff = compare_datasources(payload.copy(), ds.copy(), enforce_secure_data) |
| 990 |
if diff.get("before") == diff.get("after"): |
| 991 |
module.exit_json( |
| 992 |
changed=False, datasource=ds, msg="Datasource %s unchanged" % name |
| 993 |
) |
| 994 |
grafana_iface.update_datasource(ds.get("id"), payload) |
| 995 |
ds = grafana_iface.datasource_by_name(name) |
| 996 |
if diff.get("before") == diff.get("after"): |
| 997 |
module.exit_json( |
| 998 |
changed=False, datasource=ds, msg="Datasource %s unchanged" % name |
| 999 |
) |
| 1000 |
|
| 1001 |
module.exit_json( |
| 1002 |
changed=True, |
| 1003 |
diff=diff, |
| 1004 |
datasource=ds, |
| 1005 |
msg="Datasource %s updated" % name, |
| 1006 |
) |
| 1007 |
else: |
| 1008 |
if ds is None: |
| 1009 |
module.exit_json( |
| 1010 |
changed=False, |
| 1011 |
datasource=None, |
| 1012 |
msg="Datasource %s does not exist." % name, |
| 1013 |
) |
| 1014 |
grafana_iface.delete_datasource(name) |
| 1015 |
module.exit_json( |
| 1016 |
changed=True, datasource=None, msg="Datasource %s deleted." % name |
| 1017 |
) |
| 1018 |
|
| 1019 |
|
| 1020 |
if __name__ == "__main__": |
| 1021 |
main() |