0

I'm having an issue provisioning Grafana datasources with host variables for api key and url.

My intent is to implement a common list of datasources across multiple Grafana hosts (test/dev platforms.) Each of these hosts has their own Grafana URL and API Key.

Running grafana_datasource explicitly works fine, and using variables in debug prints my expected values for API key and URL as expected, but using the same syntax in grafana_datasource causes issues.

I have created host variables in my inventory like so:

[hostgroup]
host1 grafapikey=loTsoFLeTTers grafurl=https://host1.net
host2 grafapikey=loTsoFLeTTers grafurl=https://host2.net

I then test referencing these variables in my playbook like this:

  - name: what is grafurl
    debug:
      msg: "{{ hostvars[inventory_hostname].grafurl }}"
  - name: what is grafapi
    debug:
      msg: "{{ hostvars[inventory_hostname].grafapikey }}"

# note output is identical whether using msg or var here

and the output is as expected:

TASK [what is grafurl] **************************
ok: [host1.net] => {
    "msg": "https://host1.net/"
}

TASK [what is grafapi] **************************
ok: [host1.net] => {
    "msg": "loTsoFLeTTers"
}

Also, defining all arguments explictly when running this command ad-hoc creates the datasource successfully.

user@ansible-host:~$ ansible host1.net -m grafana_datasource -a "grafana_api_key='LoTsofLettERs' grafana_url='https://host1.net' name=testds ds_type=mysql url='testurl' database='test'" -K
BECOME password:
host1.net | CHANGED => {
    "changed": true,
    "id": 2,
    "msg": "Datasource testds created : Datasource added",
    "name": "testds"
}

However, running the same adhoc command using variable references causes issues:

user@ansible-host:~$ ansible host1.net -m grafana_datasource -a "grafana_api_key='{{ hostvars[inventory_hostname].grafapikey }}' grafana_url='{{ hostvars[inventory_hostname].grafurl }}' name=testds ds_type=mysql url='testurl' database='test' validate_certs=no" -K
BECOME password:
scada.ifctrl.net | FAILED! => {
    "changed": false,
    "module_stderr": "Shared connection to scada.ifctrl.net closed.\r\n",
    "module_stdout": ##see below

I get lengthy errors in playbook output describing unexpected empty JSON:

Traceback (most recent call last):
  File \"/root/.ansible/tmp/ansible-tmp-1596750425.3152304-29947-100749071777145/AnsiballZ_grafana_datasource.py\", line 102, in <module>
    _ansiballz_main()
  File \"/root/.ansible/tmp/ansible-tmp-1596750425.3152304-29947-100749071777145/AnsiballZ_grafana_datasource.py\", line 94, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File \"/root/.ansible/tmp/ansible-tmp-1596750425.3152304-29947-100749071777145/AnsiballZ_grafana_datasource.py\", line 40, in invoke_module
    runpy.run_module(mod_name='ansible.modules.monitoring.grafana_datasource', init_globals=None, run_name='__main__', alter_sys=True)
  File \"/usr/lib/python3.6/runpy.py\", line 205, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File \"/usr/lib/python3.6/runpy.py\", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File \"/usr/lib/python3.6/runpy.py\", line 85, in _run_code
    exec(code, run_globals)
  File \"/tmp/ansible_grafana_datasource_payload_pwpa837u/ansible_grafana_datasource_payload.zip/ansible/modules/monitoring/grafana_datasource.py\", line 683, in <module>
  File \"/tmp/ansible_grafana_datasource_payload_pwpa837u/ansible_grafana_datasource_payload.zip/ansible/modules/monitoring/grafana_datasource.py\", line 665, in main
  File \"/tmp/ansible_grafana_datasource_payload_pwpa837u/ansible_grafana_datasource_payload.zip/ansible/modules/monitoring/grafana_datasource.py\", line 504, in grafana_create_datasource
  File \"/tmp/ansible_grafana_datasource_payload_pwpa837u/ansible_grafana_datasource_payload.zip/ansible/modules/monitoring/grafana_datasource.py\", line 397, in grafana_datasource_exists
  File \"/usr/lib/python3.6/json/__init__.py\", line 354, in loads
    return _default_decoder.decode(s)
  File \"/usr/lib/python3.6/json/decoder.py\", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File \"/usr/lib/python3.6/json/decoder.py\", line 357, in raw_decode
    raise JSONDecodeError(\"Expecting value\", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Is my syntax incorrect? Am I referencing variables incorrectly in the scope of grafana_datasource in my playbook?

derek
  • 103
  • 1
  • 4

1 Answers1

0

The issue was that I had a trailing slash in my Grafana URL. Removing the slash resolved the problem.

derek
  • 103
  • 1
  • 4