ansible-collections / community.grafana

Grafana Collection for Ansible
http://galaxy.ansible.com/community/grafana
GNU General Public License v3.0
131 stars 83 forks source link

Dashboard creation fails on Grafana 4 #146

Closed liff closed 3 years ago

liff commented 3 years ago
SUMMARY

When creating a new dashboard on Grafana 4 the operation fails with:

UnboundLocalError: local variable 'folder_exists' referenced before assignment

The folder_exists variable only gets assigned when Grafana version is 5 or greater.

ISSUE TYPE
COMPONENT NAME

community.grafana.grafana_dashboard

ANSIBLE VERSION
ansible 2.10.0
  config file = None
  configured module search path = ['/home/olli/.local/share/ansible/plugins/modules']
  ansible python module location = /nix/store/j7shbb56dxrfpc03kxr7g4chn9kpp95w-python3.8-ansible-2.10.0/lib/python3.8/site-packages/ansible
  executable location = /nix/store/j7shbb56dxrfpc03kxr7g4chn9kpp95w-python3.8-ansible-2.10.0/bin/ansible
  python version = 3.8.7 (default, Dec 21 2020, 17:18:55) [GCC 10.2.0]
CONFIGURATION
OS / ENVIRONMENT

NixOS unstable

STEPS TO REPRODUCE

This command sequence should demonstrate the problem.

mkdir -p api/frontend
echo '{"buildInfo":{"version":"4.0.0"}}' > api/frontend/settings
echo '{}' > test.json
python -m http.server &
ansible -m community.grafana.grafana_dashboard -a 'grafana_url=http://localhost:8000 slug=test path=test.json grafana_api_key=test' localhost
kill %1 # stop http.server
EXPECTED RESULTS

Dashboard creation succeeds. Or with the above command sequence fails due to something that is not a bug :)

ACTUAL RESULTS
127.0.0.1 - - [26/Jan/2021 15:08:22] "GET /api/frontend/settings HTTP/1.1" 200 -
127.0.0.1 - - [26/Jan/2021 15:08:22] "GET /api/frontend/settings HTTP/1.1" 200 -
127.0.0.1 - - [26/Jan/2021 15:08:22] code 404, message File not found
127.0.0.1 - - [26/Jan/2021 15:08:22] "GET /api/dashboards/db/test HTTP/1.1" 404 -
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: UnboundLocalError: local variable 'folder_exists' referenced before assignment
localhost | FAILED! => {
    "changed": false,
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible-tmp-1611666501.8013935-32225-186495449997119/AnsiballZ_grafana_dashboard.py\", line 102, in <module>\n    _ansiballz_main()\n  File \"/tmp/ansible-tmp-1611666501.8013935-32225-186495449997119/AnsiballZ_grafana_dashboard.py\", line 94, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/tmp/ansible-tmp-1611666501.8013935-32225-186495449997119/AnsiballZ_grafana_dashboard.py\", line 40, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.community.grafana.plugins.modules.grafana_dashboard', init_globals=None, run_name='__main__', alter_sys=True)\n  File \"/nix/store/2wwb85hrvvfzkhrlwcj3pqi60nlbi9cq-python3-3.8.7/lib/python3.8/runpy.py\", line 207, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/nix/store/2wwb85hrvvfzkhrlwcj3pqi60nlbi9cq-python3-3.8.7/lib/python3.8/runpy.py\", line 97, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/nix/store/2wwb85hrvvfzkhrlwcj3pqi60nlbi9cq-python3-3.8.7/lib/python3.8/runpy.py\", line 87, in _run_code\n    exec(code, run_globals)\n  File \"/run/user/1000/ansible_community.grafana.grafana_dashboard_payload_71m883ko/ansible_community.grafana.grafana_dashboard_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_dashboard.py\", line 563, in <module>\n  File \"/run/user/1000/ansible_community.grafana.grafana_dashboard_payload_71m883ko/ansible_community.grafana.grafana_dashboard_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_dashboard.py\", line 525, in main\n  File \"/run/user/1000/ansible_community.grafana.grafana_dashboard_payload_71m883ko/ansible_community.grafana.grafana_dashboard_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_dashboard.py\", line 388, in grafana_create_dashboard\nUnboundLocalError: local variable 'folder_exists' referenced before assignment\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}

Unwrapped module_stderr:

Traceback (most recent call last):
  File "/tmp/ansible-tmp-1611666501.8013935-32225-186495449997119/AnsiballZ_grafana_dashboard.py", line 102, in <module>
    _ansiballz_main()
  File "/tmp/ansible-tmp-1611666501.8013935-32225-186495449997119/AnsiballZ_grafana_dashboard.py", line 94, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/tmp/ansible-tmp-1611666501.8013935-32225-186495449997119/AnsiballZ_grafana_dashboard.py", line 40, in invoke_module
    runpy.run_module(mod_name='ansible_collections.community.grafana.plugins.modules.grafana_dashboard', init_globals=None, run_name='__main__', alter_sys=True)
  File "/nix/store/2wwb85hrvvfzkhrlwcj3pqi60nlbi9cq-python3-3.8.7/lib/python3.8/runpy.py", line 207, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/nix/store/2wwb85hrvvfzkhrlwcj3pqi60nlbi9cq-python3-3.8.7/lib/python3.8/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/nix/store/2wwb85hrvvfzkhrlwcj3pqi60nlbi9cq-python3-3.8.7/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/run/user/1000/ansible_community.grafana.grafana_dashboard_payload_71m883ko/ansible_community.grafana.grafana_dashboard_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_dashboard.py", line 563, in <module>
  File "/run/user/1000/ansible_community.grafana.grafana_dashboard_payload_71m883ko/ansible_community.grafana.grafana_dashboard_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_dashboard.py", line 525, in main
  File "/run/user/1000/ansible_community.grafana.grafana_dashboard_payload_71m883ko/ansible_community.grafana.grafana_dashboard_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_dashboard.py", line 388, in grafana_create_dashboard
UnboundLocalError: local variable 'folder_exists' referenced before assignment
rrey commented 3 years ago

Hi @liff This look like a bug in the code that we need to fix, but be aware that Grafana 4 is no more in the list of supported versions.