Skip to content

Commit e583765

Browse files
committed
feat(grafana_dashboard): allow imported dashboards to be renamed
1 parent 2330479 commit e583765

File tree

6 files changed

+180
-0
lines changed

6 files changed

+180
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
minor_changes:
3+
- grafana_dashboard - allow imported dashboards to be renamed

plugins/modules/grafana_dashboard.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@
6262
- Used to identify the dashboard when C(state) is C(export) or C(absent).
6363
- When C(state) is C(present), this can be used to set the UID during dashboard creation.
6464
type: str
65+
name:
66+
description:
67+
- A new title to rename the imported Grafana dashboard to.
68+
type: str
69+
aliases: [ title ]
70+
version_added: "2.4.0"
6571
path:
6672
description:
6773
- The path to the json file containing the Grafana dashboard to import or export.
@@ -394,6 +400,10 @@ def grafana_create_dashboard(module, data):
394400
if data.get("uid"):
395401
payload["dashboard"]["uid"] = data["uid"]
396402

403+
if data.get("name"):
404+
# rename dashboard if user has provided a custom name
405+
payload["dashboard"]["title"] = data["name"]
406+
397407
result = {}
398408

399409
# test if the folder exists
@@ -635,6 +645,7 @@ def main():
635645
folder=dict(type="str", default="General"),
636646
parent_folder=dict(type="str"),
637647
uid=dict(type="str"),
648+
name=dict(type="str", aliases=["title"]),
638649
slug=dict(type="str"),
639650
path=dict(aliases=["dashboard_url"], type="str"),
640651
dashboard_id=dict(type="str"),

roles/grafana/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Configure Grafana organizations, dashboards, folders, datasources, teams and use
9292
| slug | no |
9393
| state | no |
9494
| uid | no |
95+
| name | no |
9596
| [**grafana_organization_users**](https://docs.ansible.com/ansible/latest/collections/community/grafana/grafana_organization_user_module.html) |
9697
| login | yes |
9798
| org_id | no |

roles/grafana/tasks/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@
336336
slug: "{{ dashboard.slug | default(omit) }}"
337337
state: "{{ dashboard.state | default(omit) }}"
338338
uid: "{{ dashboard.uid | default(omit) }}"
339+
name: "{{ dashboard.name | default(omit) }}"
339340
loop: "{{ grafana_dashboards }}"
340341
loop_control: {loop_var: dashboard}
341342
tags: [dashboard, molecule-idempotence-notest]
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
---
2+
- name: Create dashboard from ID with custom name | check mode | dashboard does not exist
3+
community.grafana.grafana_dashboard:
4+
state: present
5+
commit_message: Updated by ansible
6+
dashboard_id: "7587"
7+
dashboard_revision: "1"
8+
overwrite: true
9+
uid: "dfi"
10+
name: "Different Name"
11+
check_mode: true
12+
register: dfi_result1
13+
- ansible.builtin.assert:
14+
that:
15+
- dfi_result1.failed == false
16+
- dfi_result1.changed == true
17+
- dfi_result1.uid is not defined
18+
- dfi_result1.msg == 'Dashboard Different Name will be created'
19+
20+
- name: Create dashboard from ID with custom name | run mode | dashboard does not exist
21+
community.grafana.grafana_dashboard:
22+
state: present
23+
commit_message: Updated by ansible
24+
dashboard_id: "7587"
25+
dashboard_revision: "1"
26+
overwrite: true
27+
uid: "dfi"
28+
name: "Different Name"
29+
check_mode: false
30+
register: dfi_result2
31+
- ansible.builtin.assert:
32+
that:
33+
- dfi_result2.failed == false
34+
- dfi_result2.changed == true
35+
- dfi_result2.uid is truthy
36+
- dfi_result2.uid == 'dfi'
37+
- dfi_result2.msg == 'Dashboard Different Name created'
38+
39+
- name: Create dashboard from ID with custom name | check mode | dashboard exists
40+
community.grafana.grafana_dashboard:
41+
state: present
42+
commit_message: Updated by ansible
43+
dashboard_id: "7587"
44+
dashboard_revision: "1"
45+
overwrite: true
46+
uid: "dfi"
47+
name: "Different Name"
48+
check_mode: true
49+
register: dfi_result3
50+
- ansible.builtin.assert:
51+
that:
52+
- dfi_result3.failed == false
53+
- dfi_result3.changed == false
54+
- dfi_result3.uid is truthy
55+
- dfi_result3.uid == 'dfi'
56+
- dfi_result3.msg == 'Dashboard Different Name unchanged.'
57+
58+
- name: Create dashboard from ID with custom name | run mode | dashboard exists
59+
community.grafana.grafana_dashboard:
60+
state: present
61+
commit_message: Updated by ansible
62+
dashboard_id: "7587"
63+
dashboard_revision: "1"
64+
overwrite: true
65+
uid: "dfi"
66+
name: "Different Name"
67+
check_mode: false
68+
register: dfi_result4
69+
- ansible.builtin.assert:
70+
that:
71+
- dfi_result4.failed == false
72+
- dfi_result4.changed == false
73+
- dfi_result4.uid is truthy
74+
- dfi_result4.uid == 'dfi'
75+
- dfi_result4.msg == 'Dashboard Different Name unchanged.'
76+
77+
- ansible.builtin.include_tasks:
78+
file: delete-dashboard.yml
79+
vars:
80+
uid_to_delete: "{{ dfi_result4.uid }}"
81+
82+
- name: Create dashboard from ID with default name | run mode | dashboard does not exist
83+
community.grafana.grafana_dashboard:
84+
state: present
85+
commit_message: Updated by ansible
86+
dashboard_id: "9578"
87+
dashboard_revision: "1"
88+
overwrite: true
89+
uid: "dfi"
90+
check_mode: false
91+
register: dfi_result5
92+
- ansible.builtin.assert:
93+
that:
94+
- dfi_result5.failed == false
95+
- dfi_result5.changed == true
96+
- dfi_result5.uid is truthy
97+
- dfi_result5.uid == 'dfi'
98+
- dfi_result5.msg == 'Dashboard Alertmanager created'
99+
100+
- name: Rename existing dashboard | check mode | dashboard has original name
101+
community.grafana.grafana_dashboard:
102+
state: present
103+
commit_message: Updated by ansible
104+
dashboard_id: "9578"
105+
dashboard_revision: "1"
106+
overwrite: true
107+
uid: "dfi"
108+
name: "Renamed"
109+
check_mode: true
110+
register: dfi_result6
111+
- ansible.builtin.assert:
112+
that:
113+
- dfi_result6.failed == false
114+
- dfi_result6.changed == true
115+
- dfi_result6.uid is truthy
116+
- dfi_result6.uid == 'dfi'
117+
- dfi_result6.msg == 'Dashboard Renamed will be updated'
118+
119+
- name: Rename existing dashboard | run mode | dashboard has original name
120+
community.grafana.grafana_dashboard:
121+
state: present
122+
commit_message: Updated by ansible
123+
dashboard_id: "9578"
124+
dashboard_revision: "1"
125+
overwrite: true
126+
uid: "dfi"
127+
name: "Renamed"
128+
check_mode: false
129+
register: dfi_result7
130+
- ansible.builtin.assert:
131+
that:
132+
- dfi_result7.failed == false
133+
- dfi_result7.changed == true
134+
- dfi_result7.uid is truthy
135+
- dfi_result7.uid == 'dfi'
136+
- dfi_result7.msg == 'Dashboard Renamed updated'
137+
138+
- name: Rename existing dashboard | run mode | dashboard has been renamed
139+
community.grafana.grafana_dashboard:
140+
state: present
141+
commit_message: Updated by ansible
142+
dashboard_id: "9578"
143+
dashboard_revision: "1"
144+
overwrite: true
145+
uid: "dfi"
146+
name: "Renamed"
147+
check_mode: false
148+
register: dfi_result8
149+
- ansible.builtin.assert:
150+
that:
151+
- dfi_result8.failed == false
152+
- dfi_result8.changed == false
153+
- dfi_result8.uid is truthy
154+
- dfi_result8.uid == 'dfi'
155+
- dfi_result8.msg == 'Dashboard Renamed unchanged.'
156+
157+
- ansible.builtin.include_tasks:
158+
file: delete-dashboard.yml
159+
vars:
160+
uid_to_delete: "{{ dfi_result8.uid }}"

tests/integration/targets/grafana_dashboard/tasks/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
ansible.builtin.include_tasks:
88
file: dashboard-from-id.yml
99

10+
- name: Create dashboard with custom name
11+
ansible.builtin.include_tasks:
12+
file: dashboard-rename.yml
13+
1014
- name: Create dashboard from file and export
1115
ansible.builtin.include_tasks:
1216
file: dashboard-from-file-export.yml

0 commit comments

Comments
 (0)