【问题标题】:Provisioning Grafana Datasource in Ansible using Lists使用列表在 Ansible 中配置 Grafana 数据源
【发布时间】:2020-08-06 22:59:35
【问题描述】:

我在为 Grafana 数据源配置 api 密钥和 url 的主机变量时遇到问题。

我的意图是跨多个 Grafana 主机(测试/开发平台)实现一个通用的数据源列表。这些主机中的每一个都有自己的 Grafana URL 和 API 密钥。

显式运行 grafana_datasource 可以正常运行,并且在 debug 中使用变量会按预期打印 API 密钥和 URL 的预期值,但在 grafana_datasource 中使用相同的语法会导致问题。

我在我的清单中创建了主机变量,如下所示:

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

然后我在我的剧本中测试引用这些变量,如下所示:

  - 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

并且输出如预期:

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

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

此外,在运行此命令时明确定义所有参数,即会成功创建数据源。

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"
}

但是,使用变量引用运行相同的临时命令会导致问题:

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

我在描述意外空 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)

我的语法不正确吗?我是否在我的剧本中错误地引用了 grafana_datasource 范围内的变量?

【问题讨论】:

    标签: ansible ansible-inventory grafana-api


    【解决方案1】:

    问题是我的 Grafana URL 中有一个斜杠。删除斜线解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 2023-02-24
      相关资源
      最近更新 更多