Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Decode Error when grafana_url ends with trailing slash #135

Closed
nogweii opened this issue Oct 28, 2020 · 3 comments · Fixed by #150
Closed

JSON Decode Error when grafana_url ends with trailing slash #135

nogweii opened this issue Oct 28, 2020 · 3 comments · Fixed by #150
Labels
bug This issue/PR relates to a bug.

Comments

@nogweii
Copy link

nogweii commented Oct 28, 2020

SUMMARY

When crafting a URL for Grafana's API, the grafana_datasource module (and maybe others, I didn't check them) appends two strings together, the passed in grafana_url and the appropriate endpoint. This results in a URL like "...com//api/..." Grafana is picky about matching routes, so returns an error page in HTML, not JSON as the module expects. It attempts to parse the page as JSON which results in a JSONDecodeError.

ISSUE TYPE
  • Bug Report
  • Feature Request

A bit of both, I think.

COMPONENT NAME
ANSIBLE VERSION
ansible 2.10.2
  config file = /home/user/code/homelab/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.8/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.6 (default, Sep 30 2020, 04:00:38) [GCC 10.2.0]
CONFIGURATION
DEPRECATION_WARNINGS(/home/user/code/homelab/ansible.cfg) = False
DIFF_ALWAYS(/home/user/code/homelab/ansible.cfg) = True
LOCALHOST_WARNING(/home/user/code/homelab/ansible.cfg) = False
OS / ENVIRONMENT

Arch Linux, targeting itself. Everything is installed from the distribution packages.

STEPS TO REPRODUCE

Simply running this playbook should trigger the error:

---
- name: create a datasource in grafana to read metrics
  community.grafana.grafana_datasource:
    name: "{{ item }}"
    grafana_url: "https://grafana.example.com/" # NB: the trailing slash
    grafana_api_key: "{{ grafana_admin_api_key }}"
    org_id: "1"
    ds_type: "influxdb"
    ds_url: "http://influx:8086"
    database: "telegraf"
    time_interval: ">10s"
    user: grafana
    password: "{{ influxdb_users_grafana }}"
EXPECTED RESULTS

Not a python exception, but either it gracefully handling it or returning a human friendly error message rather than a screen full of red error text. 😄

ACTUAL RESULTS
"module_stderr": "Traceback (most recent call last):\n  File \"/home/user/.ansible/tmp/ansible-tmp-1603857202.7836478-2983891-108174010981857/AnsiballZ_grafana_datasource.py\", line 102, in <module>\n    _ansiballz_main()\n  File \"/home/user/.ansible/tmp/ansible-tmp-1603857202.7836478-2983891-108174010981857/AnsiballZ_grafana_datasource.py\", line 94, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/user/.ansible/tmp/ansible-tmp-1603857202.7836478-2983891-108174010981857/AnsiballZ_grafana_datasource.py\", line 40, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.community.grafana.plugins.modules.grafana_datasource', init_globals=None, run_name='__main__', alter_sys=True)\n  File \"/usr/lib/python3.8/runpy.py\", line 207, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib/python3.8/runpy.py\", line 97, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/usr/lib/python3.8/runpy.py\", line 87, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_community.grafana.grafana_datasource_payload_oszg32vj/ansible_community.grafana.grafana_datasource_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_datasource.py\", line 752, in <module>\n  File \"/tmp/ansible_community.grafana.grafana_datasource_payload_oszg32vj/ansible_community.grafana.grafana_datasource_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_datasource.py\", line 726, in main\n  File \"/tmp/ansible_community.grafana.grafana_datasource_payload_oszg32vj/ansible_community.grafana.grafana_datasource_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_datasource.py\", line 628, in datasource_by_name\n  File \"/tmp/ansible_community.grafana.grafana_datasource_payload_oszg32vj/ansible_community.grafana.grafana_datasource_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_datasource.py\", line 617, in _send_request\n  File \"/tmp/ansible_community.grafana.grafana_datasource_payload_oszg32vj/ansible_community.grafana.grafana_datasource_payload.zip/ansible/module_utils/basic.py\", line 2136, in from_json\n  File \"/usr/lib/python3.8/json/__init__.py\", line 357, in loads\n    return _default_decoder.decode(s)\n  File \"/usr/lib/python3.8/json/decoder.py\", line 337, in decode\n    obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n  File \"/usr/lib/python3.8/json/decoder.py\", line 355, in raw_decode\n    raise JSONDecodeError(\"Expecting value\", s, err.value) from None\njson.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n
@mator
Copy link

mator commented Jan 26, 2021

i can't reproduce the issue... tested as
grafana_url: "https://localhost/grafana/"
and
grafana_url: "https://localhost/grafana"
both works

@rrey rrey added the bug This issue/PR relates to a bug. label Feb 7, 2021
@rrey
Copy link
Collaborator

rrey commented Feb 7, 2021

Managed to reproduce this and latest collection version and same module.

@nogweii
Copy link
Author

nogweii commented Feb 10, 2021

Thank for getting to this. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants