我在为 Grafana 数据源配置 api 密钥和 url 的主机变量时遇到问题。
我的意图是跨多个 Grafana 主机(测试/开发平台)实现一个通用的数据源列表。这些主机中的每一个都有自己的 Grafana URL 和 API 密钥。
显式运行grafana_datasource
可以正常运行,并且使用变量 indebug
会按预期打印我对 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
我是否在我的剧本范围内错误地引用了变量?