Ansible uri JSON 解析问题
Issue with Ansible uri JSON parsing
在 Ansible 中,使用 uri
模块检索一些 JSON,然后使用 debug
.
输出其中一个字段
- name: debug
debug:
msg: "{{ result.json.results[0].series[0].values }}"
JSON 数据包含一个名为 values
的键,这似乎与 Python 关键字 values()
匹配。因此,它不是返回该键的值,而是返回一个对象。
ok: [XXX] => {
"msg": "<built-in method values of dict object at 0x7fd131418280>"
}
知道如何防止调用以这种方式被解释吗?
您可以尝试通过方括号表示法访问它:
- debug:
msg: "{{ result.json.results[0].series[0]['values'] }}"
如果还是不行,Python字典的.get(key[, default])
方法应该派上用场了:
- debug:
msg: "{{ result.json.results[0].series[0].get('values') }}"
确实如此:
- hosts: all
gather_facts: no
tasks:
- debug:
msg: "{{ result.json.results[0].series[0].values }}"
vars:
result:
json:
results:
- series:
- values: foo
我们得到:
PLAY [all] **********************************************************************************************************
TASK [debug] ********************************************************************************************************
ok: [localhost] => {
"msg": "<built-in method values of dict object at 0x7fb3111e9cc0>"
}
PLAY RECAP **********************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
但是有:
- 要么
- hosts: all
gather_facts: no
tasks:
- debug:
msg: "{{ result.json.results[0].series[0]['values'] }}"
vars:
result:
json:
results:
- series:
- values: foo
- 或
- hosts: all
gather_facts: no
tasks:
- debug:
msg: "{{ result.json.results[0].series[0].get('values') }}"
vars:
result:
json:
results:
- series:
- values: foo
你确实得到了:
PLAY [all] **********************************************************************************************************
TASK [debug] ********************************************************************************************************
ok: [localhost] => {
"msg": "foo"
}
PLAY RECAP **********************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在 Ansible 中,使用 uri
模块检索一些 JSON,然后使用 debug
.
- name: debug
debug:
msg: "{{ result.json.results[0].series[0].values }}"
JSON 数据包含一个名为 values
的键,这似乎与 Python 关键字 values()
匹配。因此,它不是返回该键的值,而是返回一个对象。
ok: [XXX] => {
"msg": "<built-in method values of dict object at 0x7fd131418280>"
}
知道如何防止调用以这种方式被解释吗?
您可以尝试通过方括号表示法访问它:
- debug:
msg: "{{ result.json.results[0].series[0]['values'] }}"
如果还是不行,Python字典的.get(key[, default])
方法应该派上用场了:
- debug:
msg: "{{ result.json.results[0].series[0].get('values') }}"
确实如此:
- hosts: all
gather_facts: no
tasks:
- debug:
msg: "{{ result.json.results[0].series[0].values }}"
vars:
result:
json:
results:
- series:
- values: foo
我们得到:
PLAY [all] **********************************************************************************************************
TASK [debug] ********************************************************************************************************
ok: [localhost] => {
"msg": "<built-in method values of dict object at 0x7fb3111e9cc0>"
}
PLAY RECAP **********************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
但是有:
- 要么
- hosts: all gather_facts: no tasks: - debug: msg: "{{ result.json.results[0].series[0]['values'] }}" vars: result: json: results: - series: - values: foo
- 或
- hosts: all gather_facts: no tasks: - debug: msg: "{{ result.json.results[0].series[0].get('values') }}" vars: result: json: results: - series: - values: foo
你确实得到了:
PLAY [all] **********************************************************************************************************
TASK [debug] ********************************************************************************************************
ok: [localhost] => {
"msg": "foo"
}
PLAY RECAP **********************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0