Ansible:使用json\u查询选择嵌套值
我正试图通过RESTAPI和ansbile的uri模块验证几个服务端点Ansible:使用json\u查询选择嵌套值,ansible,jinja2,Ansible,Jinja2,我正试图通过RESTAPI和ansbile的uri模块验证几个服务端点 - name: Wait until service is fully deployed local_action: module: uri url: http://somerestserver/{{ app_name }}-{{ item }}/tasks methon: GET register: response loop: "
- name: Wait until service is fully deployed
local_action:
module: uri
url: http://somerestserver/{{ app_name }}-{{ item }}/tasks
methon: GET
register: response
loop: "{{ range(0, instances) | list }}"
响应变量E如下所示:
"response": {
"changed": false,
"msg": "All items completed",
"results": [
{
"item": 0,
"json": {
"tasks": [
{
"appId": "node-0",
"healthCheckResults": [
{
"alive": true,
...
}
],
...
}
]
},
"msg": "OK (674 bytes)",
},
{
"item": 1,
"json": {
"tasks": [
{
"appId": "node-1",
"healthCheckResults": [
{
"alive": true,
...
}
],
}
]
},
"msg": "OK (674 bytes)",
},
{
"item": 2,
"json": {
"tasks": [
{
"appId": "node-2",
"healthCheckResults": [
{
"alive": true,
...
}
],
}
]
},
"msg": "OK (674 bytes)",
}
]
}
results[*].json.tasks[*].healthCheckResults[*].alive[][]
我现在想做的是等到我所有的服务报告都激活:true
不幸的是,这不起作用,所以,今天我学到了可以在单个任务上组合循环和直到,这是一个惊喜。所以这很酷 在任何情况下,您都非常接近解决方案。根据您的输入数据,您的查询结果如下:
[
[
[
true
]
],
[
[
true
]
],
[
[
true
]
]
]
这永远不会与您要比较的[真的,真的,真的]列表相匹配。只需应用两个jmespath运算符,如下所示:
"response": {
"changed": false,
"msg": "All items completed",
"results": [
{
"item": 0,
"json": {
"tasks": [
{
"appId": "node-0",
"healthCheckResults": [
{
"alive": true,
...
}
],
...
}
]
},
"msg": "OK (674 bytes)",
},
{
"item": 1,
"json": {
"tasks": [
{
"appId": "node-1",
"healthCheckResults": [
{
"alive": true,
...
}
],
}
]
},
"msg": "OK (674 bytes)",
},
{
"item": 2,
"json": {
"tasks": [
{
"appId": "node-2",
"healthCheckResults": [
{
"alive": true,
...
}
],
}
]
},
"msg": "OK (674 bytes)",
}
]
}
results[*].json.tasks[*].healthCheckResults[*].alive[][]
根据示例输入,结果如下:
[true, true, true]
在你的剧本中,这将是:
- name: Wait until service is fully deployed
local_action:
module: uri
url: http://somerestserver/{{ app_name }}-{{ item }}/tasks
methon: GET
register: response
loop: "{{ range(0, instances) | list }}"
until: response|json_query('results[*].json.tasks[*].healthCheckResults[*].alive[][]') == [true]*instances
将来,您可以通过将数据粘贴到的文本框中,然后在上面键入查询来尝试查询。。。。与迭代后相比,迭代时变量的内容看起来有所不同:因此直到:response | json_query'json.tasks[*].healthCheckResults[*].alive[]'==[true]现在可以工作了。非常感谢!