我在ansible中有以下json输出:
[{
"active_transaction": null,
"cores": 4,
"hostname": "alpha-auth-wb01"
},
{
"active_transaction": null,
"cores": 4,
"hostname": "beta-auth-wb01"
}]
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试过滤输出以仅显示主机名包含 alpha 的输出。
输出应该是:
[{
"active_transaction": null,
"cores": 4,
"hostname": "alpha-auth-wb01"
}]
Run Code Online (Sandbox Code Playgroud)
代码和结果:
jq: "[?contains(hostname, 'alpha')]"
fatal: [worker.domain]: FAILED! => {"msg": "JMESPathError in json_query filter plugin:\\nIn function contains(), invalid type for value: None, expected one of: ['array', 'string'], received: \\"null\\""}
Run Code Online (Sandbox Code Playgroud)
还尝试添加 from_json | to_json 反之亦然。还是失败了。
任何想法都非常感谢!
As @Matthew L Daniel mentioned, you should store your query in a variable, because of quoting issues. Also your query is incorrect, for what you want. As I understood, you would like to select all elements, where the hostname contains the string alpha. A fully working solution is the following:
---
- hosts: localhost
gather_facts: False
vars:
jq: "[?contains(hostname, 'alpha')]"
json: |
[{
"active_transaction": null,
"cores": 4,
"hostname": "alpha-auth-wb01"
},
{
"active_transaction": null,
"cores": 4,
"hostname": "beta-auth-wb01"
}]
tasks:
- name: DEBUG
debug:
msg: "{{ json | from_json | json_query(jq) }}"
Run Code Online (Sandbox Code Playgroud)
If you don't want to write your json_query in a var you could quote it like this:
"{{ json | json_query(\"[?contains(hostname, 'alpha')]\") }}"
Run Code Online (Sandbox Code Playgroud)
But I would recommend, to put it in a var.
| 归档时间: |
|
| 查看次数: |
7930 次 |
| 最近记录: |