Ansible json_query 过滤字典列表中的数据
Ansible json_query to filter data on dict of list of dict
我有一个 Ansible 任务,它根据特定条件收集用户数据并将输出注册到一个变量,并尝试在下一个任务中迭代注册数据以仅获取名称参数。
{
"registered_data": {
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"data_details": [
{
"name": "John",
"street": "SanFisco"
},
{
"name": "Dan",
"street": "Califor"
},
{
"name": "Jack",
"street": "NY"
},
{
"name": "Wills",
"street": "NJ"
}
],
"invocation": {
"module_args": {
"sort_by": null,
"sort_order": null
}
},
"item": {
"status": "READY"
}
},
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"data_details": [],
"invocation": {
"module_args": {
"sort_by": null,
"sort_order": null
}
},
"item": {
"status": "READY"
}
},
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"data_details": [],
"invocation": {
"module_args": {
"sort_by": null,
"sort_order": null
}
},
"item": {
"status": "READY"
}
}
]
}
}
我正在尝试使用以下 ansible 任务来解析和过滤没有 return 任何输出的数据。
- name: 'printing data'
delegate_to: 'localhost'
when: item
loop: "{{ registered_data |json_query(projection_query) }}"
debug:
msg: "{{ item }}"
vars:
projection_query: "results.[*].name"
下面的任务
- debug:
msg: "{{ registered_data.results|json_query(query) }}"
vars:
query: "[].data_details[].name"
给予
msg:
- John
- Dan
- Jack
- Wills
我有一个 Ansible 任务,它根据特定条件收集用户数据并将输出注册到一个变量,并尝试在下一个任务中迭代注册数据以仅获取名称参数。
{
"registered_data": {
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"data_details": [
{
"name": "John",
"street": "SanFisco"
},
{
"name": "Dan",
"street": "Califor"
},
{
"name": "Jack",
"street": "NY"
},
{
"name": "Wills",
"street": "NJ"
}
],
"invocation": {
"module_args": {
"sort_by": null,
"sort_order": null
}
},
"item": {
"status": "READY"
}
},
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"data_details": [],
"invocation": {
"module_args": {
"sort_by": null,
"sort_order": null
}
},
"item": {
"status": "READY"
}
},
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"data_details": [],
"invocation": {
"module_args": {
"sort_by": null,
"sort_order": null
}
},
"item": {
"status": "READY"
}
}
]
}
}
我正在尝试使用以下 ansible 任务来解析和过滤没有 return 任何输出的数据。
- name: 'printing data'
delegate_to: 'localhost'
when: item
loop: "{{ registered_data |json_query(projection_query) }}"
debug:
msg: "{{ item }}"
vars:
projection_query: "results.[*].name"
下面的任务
- debug:
msg: "{{ registered_data.results|json_query(query) }}"
vars:
query: "[].data_details[].name"
给予
msg:
- John
- Dan
- Jack
- Wills