8585 default: '1'
8686 version_added: "1.0.0"
8787 type: str
88+ inputs:
89+ description:
90+ - Set replacement values for dashboard input variables
91+ - The dictionary key is the input name and the value is the input value
92+ version_added: "2.4.0"
93+ type: dict
8894 commit_message:
8995 description:
9096 - Set a commit message for the version history.
128134 folder: myteam
129135 dashboard_url: https://grafana.com/api/dashboards/6098/revisions/1/download
130136
137+ - name: Import Grafana dashboard Node Exporter Full and set value for an input
138+ community.grafana.grafana_dashboard:
139+ grafana_url: http://grafana.company.com
140+ grafana_api_key: "{{ grafana_api_key }}"
141+ dashboard_id: 1860
142+ dashboard_revision: 40
143+ inputs:
144+ DS_PROMETHEUS: DatasourceName
145+
131146- name: Export dashboard
132147 community.grafana.grafana_dashboard:
133148 grafana_url: http://grafana.company.com
148163 sample: 000000063
149164"""
150165
166+ import copy
151167import json
152168from ansible .module_utils .basic import AnsibleModule
153169from ansible .module_utils .urls import fetch_url
@@ -341,6 +357,27 @@ def is_grafana_dashboard_changed(payload, dashboard):
341357 if "meta" in payload :
342358 del payload ["meta" ]
343359
360+ # remove elements which are stripped by the import api
361+ for key in ["__inputs" , "__elements" , "__requires" ]:
362+ if key in dashboard ["dashboard" ]:
363+ del dashboard ["dashboard" ][key ]
364+ if key in payload ["dashboard" ]:
365+ del payload ["dashboard" ][key ]
366+
367+ if "inputs" in payload :
368+ # ignore inputs for compare
369+ dashboard ["inputs" ] = payload ["inputs" ]
370+
371+ # replace inputs in payload with actual values
372+ payload_string = json .dumps (payload )
373+ for input_var in payload ["inputs" ]:
374+ if "name" not in input_var or "value" not in input_var :
375+ continue
376+ payload_string = payload_string .replace (
377+ "${" + input_var ["name" ] + "}" , input_var ["value" ]
378+ )
379+ payload = json .loads (payload_string )
380+
344381 # Ignore dashboard ids since real identifier is uuid
345382 if "id" in dashboard ["dashboard" ]:
346383 del dashboard ["dashboard" ]["id" ]
@@ -393,6 +430,24 @@ def grafana_create_dashboard(module, data):
393430 if data .get ("uid" ):
394431 payload ["dashboard" ]["uid" ] = data ["uid" ]
395432
433+ # set dashboard inputs
434+ api_endpoint = "db"
435+ if data ["inputs" ] is not None :
436+ inputs = []
437+ for input_var in copy .deepcopy (payload ["dashboard" ].get ("__inputs" , [])):
438+ if input_var .get ("name" ) is None :
439+ continue
440+ if input_var ["name" ] in data ["inputs" ]:
441+ # user has overriden input value
442+ input_var ["value" ] = data ["inputs" ].get (input_var ["name" ])
443+ inputs .append (input_var )
444+ elif "value" in input_var :
445+ # use default value for input
446+ inputs .append (input_var )
447+ if len (inputs ) >= 1 :
448+ api_endpoint = "import"
449+ payload ["inputs" ] = inputs
450+
396451 result = {}
397452
398453 # test if the folder exists
@@ -428,7 +483,10 @@ def grafana_create_dashboard(module, data):
428483 )
429484
430485 if dashboard_exists is True :
431- grafana_dashboard_changed = is_grafana_dashboard_changed (payload , dashboard )
486+ # pass a copy of payload so that the original isn't modified by this function
487+ grafana_dashboard_changed = is_grafana_dashboard_changed (
488+ copy .deepcopy (payload ), dashboard
489+ )
432490
433491 if grafana_dashboard_changed :
434492 if module .check_mode :
@@ -446,7 +504,7 @@ def grafana_create_dashboard(module, data):
446504
447505 r , info = fetch_url (
448506 module ,
449- "%s/api/dashboards/db " % data ["url" ],
507+ "%s/api/dashboards/%s " % ( data ["url" ], api_endpoint ) ,
450508 data = json .dumps (payload ),
451509 headers = headers ,
452510 method = "POST" ,
@@ -486,7 +544,7 @@ def grafana_create_dashboard(module, data):
486544
487545 r , info = fetch_url (
488546 module ,
489- "%s/api/dashboards/db " % data ["url" ],
547+ "%s/api/dashboards/%s " % ( data ["url" ], api_endpoint ) ,
490548 data = json .dumps (payload ),
491549 headers = headers ,
492550 method = "POST" ,
@@ -640,6 +698,7 @@ def main():
640698 dashboard_revision = dict (type = "str" , default = "1" ),
641699 overwrite = dict (type = "bool" , default = False ),
642700 commit_message = dict (type = "str" ),
701+ inputs = dict (type = "dict" ),
643702 )
644703 module = AnsibleModule (
645704 argument_spec = argument_spec ,
0 commit comments