Skip to content

Commit 095479c

Browse files
authored
Update grafana role for ansible-lint, consolidate tasks, and add README (#211)
Signed-off-by: Webster Mudge <[email protected]>
1 parent f579624 commit 095479c

File tree

10 files changed

+199
-121
lines changed

10 files changed

+199
-121
lines changed

roles/grafana/README.md

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,70 @@
1-
<!--
2-
Copyright 2024 Cloudera, Inc.
1+
# grafana_server
32

4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
3+
Set up Grafana server, connected to a Prometheus server.
74

8-
https://www.apache.org/licenses/LICENSE-2.0
5+
The role will:
6+
- Install the Grafana server package(s).
7+
- Configure Grafana data sources, primarily for Prometheus, based on the provided `prometheus_url`.
8+
- Configure Grafana dashboard providers.
9+
- Provision a default dashboard.
910

10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
-->
11+
## Requirements
1612

17-
# grafana
13+
- A running Prometheus server accessible from the Grafana host.
14+
15+
## Dependencies
16+
17+
None.
18+
19+
## Role Variables (Argument Specification)
20+
21+
| Parameter | Type | Default Value | Description |
22+
|----------------------------------|------|-------------------------------------------------|---------------------------------------------------------------------------|
23+
| `grafana_datasource_directory` | `str`| `/etc/grafana/provisioning/datasources/automatic.yml`| Location of the Grafana data sources configuration file. |
24+
| `grafana_providers_configuration`| `str`| `/etc/grafana/provisioning/dashboards/providers.yml` | Location of the Grafana dashboard provider configurations file. |
25+
| `grafana_dashboard_directory` | `str`| `/var/lib/grafana/dashboards` | Location of the Grafana dashboard configurations directory. |
26+
| `prometheus_url` | `str`| `localhost:9090` | URL (host:port) to the Prometheus server that Grafana will connect to. |
27+
28+
## Examples
29+
30+
Basic installation connecting to a local Prometheus server:
31+
32+
```yaml
33+
- name: Set up Grafana server with local Prometheus
34+
ansible.builtin.import_role:
35+
name: grafana_server
36+
# No variables needed here as defaults will be used for local Prometheus
37+
38+
- name: Set up Grafana server for a specific Prometheus endpoint
39+
ansible.builtin.import_role:
40+
name: grafana_server
41+
vars:
42+
prometheus_url: "http://my-prometheus-server.example.com:9090"
43+
44+
- name: Set up Grafana with custom provisioning directories
45+
ansible.builtin.import_role:
46+
name: grafana_server
47+
vars:
48+
grafana_datasource_directory: "/opt/grafana/configs/datasources.yml"
49+
grafana_providers_configuration: "/opt/grafana/configs/providers.yml"
50+
grafana_dashboard_directory: "/opt/grafana/dashboards_custom"
51+
prometheus_url: "http://monitoring-cluster.internal:9090"
52+
```
53+
54+
## License
55+
56+
```
57+
Copyright 2025 Cloudera, Inc.
58+
59+
Licensed under the Apache License, Version 2.0 (the "License");
60+
you may not use this file except in compliance with the License.
61+
You may obtain a copy of the License at
62+
63+
https://www.apache.org/licenses/LICENSE-2.0
64+
65+
Unless required by applicable law or agreed to in writing, software
66+
distributed under the License is distributed on an "AS IS" BASIS,
67+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
68+
See the License for the specific language governing permissions and
69+
limitations under the License.
70+
```

roles/grafana/defaults/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
---
2-
31
# Copyright 2024 Cloudera, Inc. All Rights Reserved.
42
#
53
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,6 +12,8 @@
1412
# See the License for the specific language governing permissions and
1513
# limitations under the License.
1614

15+
---
16+
1717
grafana_datasource_directory: /etc/grafana/provisioning/datasources/automatic.yml
1818
grafana_providers_configuration: /etc/grafana/provisioning/dashboards/providers.yml
1919
grafana_dashboard_directory: /var/lib/grafana/dashboards
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2025 Cloudera, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
argument_specs:
16+
main:
17+
short_description: Set up Grafana server
18+
description: |
19+
Set up Grafana server, connected to a Prometheus server.
20+
author: Cloudera Labs
21+
options:
22+
grafana_datasource_directory:
23+
description:
24+
- Location of the Grafana data sources.
25+
type: str
26+
default: /etc/grafana/provisioning/datasources/automatic.yml
27+
grafana_providers_configuration:
28+
description:
29+
- Location of the Grafana provider configurations.
30+
type: str
31+
default: /etc/grafana/provisioning/dashboards/providers.yml
32+
grafana_dashboard_directory:
33+
description:
34+
- Locattion of the Grafana dashboard configurations.
35+
type: str
36+
default: /var/lib/grafana/dashboards
37+
prometheus_url:
38+
description:
39+
- URL (host:port) to the Prometheus server.
40+
type: str
41+
default: localhost:9090

roles/grafana/tasks/CentOS.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2024 Cloudera, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
---
16+
17+
- name: Import Grafana GPG key
18+
ansible.builtin.rpm_key:
19+
state: present
20+
key: "{{ grafana_gpg_key_url }}"
21+
22+
- name: Install Grafana packages
23+
ansible.builtin.yum:
24+
name: "{{ grafana_tarball_url }}"
25+
state: present

roles/grafana/tasks/Grafana-CentOS.yml

Lines changed: 0 additions & 52 deletions
This file was deleted.

roles/grafana/tasks/Grafana-Ubuntu.yml

Lines changed: 0 additions & 50 deletions
This file was deleted.

roles/grafana/tasks/Ubuntu.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2024 Cloudera, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
---
16+
17+
- name: Update APT package cache
18+
ansible.builtin.apt:
19+
update_cache: true
20+
21+
- name: Install Grafana packages
22+
ansible.builtin.apt:
23+
deb: "{{ grafana_deb_url }}"

roles/grafana/tasks/main.yml

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
# Copyright 2024 Cloudera, Inc. All Rights Reserved.
32
#
43
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,5 +11,40 @@
1211
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1312
# See the License for the specific language governing permissions and
1413
# limitations under the License.
15-
- name: Include distribution specific tasks
16-
ansible.builtin.include_tasks: "Grafana-{{ ansible_facts['distribution'] }}.yml"
14+
15+
---
16+
17+
- name: Load OS-specific Grafana variables
18+
ansible.builtin.include_vars: "{{ ansible_facts['distribution'] }}.yml"
19+
20+
- name: Execute OS-specific Grafana installation
21+
ansible.builtin.include_tasks: "{{ ansible_facts['distribution'] }}.yml"
22+
23+
- name: Connect Prometheus datasource
24+
ansible.builtin.template:
25+
src: prometheus_source.yml.j2
26+
dest: "{{ grafana_datasource_directory }}"
27+
mode: "0755"
28+
29+
- name: Add configuration for reading dashboards
30+
ansible.builtin.template:
31+
src: providers.yml.j2
32+
dest: "{{ grafana_providers_configuration }}"
33+
mode: "0755"
34+
35+
- name: Ensure that directory for dashboards exist
36+
ansible.builtin.file:
37+
path: "{{ grafana_dashboard_directory }}"
38+
mode: "0755"
39+
state: directory
40+
41+
- name: Copy default node exporter dashboard
42+
ansible.builtin.copy:
43+
src: files/base_dashboard.json
44+
dest: "{{ grafana_dashboard_directory }}/base_dashboard.json"
45+
mode: "0755"
46+
47+
- name: Start Grafana service
48+
ansible.builtin.systemd:
49+
name: "{{ grafana_service }}"
50+
state: started

roles/grafana/vars/CentOS.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
# limitations under the License.
1616

1717
grafana_tarball_url: https://dl.grafana.com/oss/release/grafana-10.2.3-1.x86_64.rpm
18-
gpg_key_url: https://rpm.grafana.com/gpg.key
18+
grafana_gpg_key_url: https://rpm.grafana.com/gpg.key
19+
20+
grafana_service: grafana-server

roles/grafana/vars/Ubuntu.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616

1717
grafana_deb_url: https://dl.grafana.com/oss/release/grafana_10.2.3_amd64.deb
1818
grafana_deb_file: grafana.deb
19+
20+
grafana_service: grafana-server

0 commit comments

Comments
 (0)