我在ansible中有以下json输出:
[{
"active_transaction": null,
"cores": 4,
"hostname": "alpha-auth-wb01"
},
{
"active_transaction": null,
"cores": 4,
"hostname": "beta-auth-wb01"
}]现在,我试图过滤输出,以便只显示主机名包含alpha的输出。
产出应是:
[{
"active_transaction": null,
"cores": 4,
"hostname": "alpha-auth-wb01"
}]守则和结果:
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\\""}还尝试添加from_json \ to_json和相反的方法。还是失败了。
任何想法都非常感谢!
发布于 2018-12-04 17:41:25
正如@Matthew所提到的,由于引用问题,您应该将查询存储在变量中。另外,您的查询是不正确的,您想要的。正如我所理解的,您希望选择所有元素,其中hostname包含字符串alpha。充分发挥作用的解决办法如下:
---
- 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) }}"如果不想在变量中写入json_query,可以引用如下所示:
"{{ json | json_query(\"[?contains(hostname, 'alpha')]\") }}"但我建议,把它放在一个变量中。
发布于 2018-12-05 09:40:47
谢谢你的回答。
我犯了一个错误,在错误的变量上使用了查询。哈!
在vsdetails上使用建议的json_query变量就像一种魅力。谢谢!:)
https://stackoverflow.com/questions/53615369
复制相似问题