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
@@ -342,6 +358,27 @@ def is_grafana_dashboard_changed(payload, dashboard):
342358 if "meta" in payload :
343359 del payload ["meta" ]
344360
361+ # remove elements which are stripped by the import api
362+ for key in ["__inputs" , "__elements" , "__requires" ]:
363+ if key in dashboard ["dashboard" ]:
364+ del dashboard ["dashboard" ][key ]
365+ if key in payload ["dashboard" ]:
366+ del payload ["dashboard" ][key ]
367+
368+ if "inputs" in payload :
369+ # ignore inputs for compare
370+ dashboard ["inputs" ] = payload ["inputs" ]
371+
372+ # replace inputs in payload with actual values
373+ payload_string = json .dumps (payload )
374+ for input_var in payload ["inputs" ]:
375+ if "name" not in input_var or "value" not in input_var :
376+ continue
377+ payload_string = payload_string .replace (
378+ "${" + input_var ["name" ] + "}" , input_var ["value" ]
379+ )
380+ payload = json .loads (payload_string )
381+
345382 # Ignore dashboard ids since real identifier is uuid
346383 if "id" in dashboard ["dashboard" ]:
347384 del dashboard ["dashboard" ]["id" ]
@@ -394,6 +431,24 @@ def grafana_create_dashboard(module, data):
394431 if data .get ("uid" ):
395432 payload ["dashboard" ]["uid" ] = data ["uid" ]
396433
434+ # set dashboard inputs
435+ api_endpoint = "db"
436+ if data ["inputs" ] is not None :
437+ inputs = []
438+ for input_var in copy .deepcopy (payload ["dashboard" ].get ("__inputs" , [])):
439+ if input_var .get ("name" ) is None :
440+ continue
441+ if input_var ["name" ] in data ["inputs" ]:
442+ # user has overriden input value
443+ input_var ["value" ] = data ["inputs" ].get (input_var ["name" ])
444+ inputs .append (input_var )
445+ elif "value" in input_var :
446+ # use default value for input
447+ inputs .append (input_var )
448+ if len (inputs ) >= 1 :
449+ api_endpoint = "import"
450+ payload ["inputs" ] = inputs
451+
397452 result = {}
398453
399454 # test if the folder exists
@@ -429,7 +484,10 @@ def grafana_create_dashboard(module, data):
429484 )
430485
431486 if dashboard_exists is True :
432- grafana_dashboard_changed = is_grafana_dashboard_changed (payload , dashboard )
487+ # pass a copy of payload so that the original isn't modified by this function
488+ grafana_dashboard_changed = is_grafana_dashboard_changed (
489+ copy .deepcopy (payload ), dashboard
490+ )
433491
434492 if grafana_dashboard_changed :
435493 if module .check_mode :
@@ -447,7 +505,7 @@ def grafana_create_dashboard(module, data):
447505
448506 r , info = fetch_url (
449507 module ,
450- "%s/api/dashboards/db " % data ["url" ],
508+ "%s/api/dashboards/%s " % ( data ["url" ], api_endpoint ) ,
451509 data = json .dumps (payload ),
452510 headers = headers ,
453511 method = "POST" ,
@@ -487,7 +545,7 @@ def grafana_create_dashboard(module, data):
487545
488546 r , info = fetch_url (
489547 module ,
490- "%s/api/dashboards/db " % data ["url" ],
548+ "%s/api/dashboards/%s " % ( data ["url" ], api_endpoint ) ,
491549 data = json .dumps (payload ),
492550 headers = headers ,
493551 method = "POST" ,
@@ -641,6 +699,7 @@ def main():
641699 dashboard_revision = dict (type = "str" , default = "1" ),
642700 overwrite = dict (type = "bool" , default = False ),
643701 commit_message = dict (type = "str" ),
702+ inputs = dict (type = "dict" ),
644703 )
645704 module = AnsibleModule (
646705 argument_spec = argument_spec ,
0 commit comments