![json_query 帮助-未产生预期的输出](https://linux22.com/image/777639/json_query%20%E5%B8%AE%E5%8A%A9-%E6%9C%AA%E4%BA%A7%E7%94%9F%E9%A2%84%E6%9C%9F%E7%9A%84%E8%BE%93%E5%87%BA.png)
我正在尝试构建一个数组来使用with_nested但是我无法理解为什么 json_query 没有输出分配给每个集群的实际主机:
玩:
- name: Index clusters.json
shell: cat {{ tower_var_path }}/clusters.json
register: result_clusters
- name: Save the Json data to a Variable as a Fact
set_fact:
clusters_jsondata: "{{ result_clusters.stdout | from_json }}"
- debug:
msg: " {{ clusters_jsondata }}"
- name: Set cluster facts
set_fact:
cluster: "{{ cluster|default([]) + [ {
'name': item,
'hosts': item | json_query('*.hosts[*].name')
} ] }}"
with_items: "{{ clusters_jsondata.clusters }}"
- debug:
msg: "{{ cluster }}"
clusters.json 数据:
{
"clusters": {
"Cluster_1": {
"hosts": [
{
"folder": "/path",
"name": "host1.domain.com"
},
{
"folder": "/path",
"name": "host2.domain.com"
}
],
},
"Cluster_2": {
"hosts": [
{
"folder": "/path",
"name": "host3.domain.com"
},
{
"folder": "/path",
"name": "host4.domain.com"
}
],
},
}
预期成绩:
TASK [debug] *********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
{
"hosts": "host1.domain.com, host2.domain.com",
"name": "Cluster_1"
},
{
"hosts": "host3.domain.com, host4.domain.com",
"name": "Cluster_2"
}
]
}
实际结果:
TASK [debug] *********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
{
"hosts": null,
"name": "Cluster_1"
},
{
"hosts": null,
"name": "Cluster_2"
}
]
}
答案1
简而言之,您只需一个表达式即可获得结果,无需任何循环。
以下剧本:
---
- hosts: localhost
gather_facts: false
vars:
# Your original data on a single line for legibility
result_clusters:
stdout: >-
{"clusters":{"Cluster_1":{"hosts":[{"folder":"/path","name":"host1.domain.com"},{"folder":"/path","name":"host2.domain.com"}]},"Cluster_2":{"hosts":[{"folder":"/path","name":"host3.domain.com"},{"folder":"/path","name":"host4.domain.com"}]}}}
tasks:
- name: get my expected output from json data in a single task
vars:
query: >-
[].{name: key, hosts: join(', ', value.hosts[].name)}
clusters: "{{ (result_clusters.stdout | from_json).clusters }}"
debug:
msg: "{{ clusters | dict2items | json_query(query) }}"
给出:
PLAY [localhost] ******************************************************
TASK [get my expected output from json data in a single task] *********
ok: [localhost] => {
"msg": [
{
"hosts": "host1.domain.com, host2.domain.com",
"name": "Cluster_1"
},
{
"hosts": "host3.domain.com, host4.domain.com",
"name": "Cluster_2"
}
]
}
PLAY RECAP ************************************************************
localhost: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
注意:尽管上面的结果非常精确地给出了您要求的输出,但我强烈怀疑您正在寻找略有不同的结果。将上述查询替换为:
query: >-
[].{name: key, hosts: value.hosts[].name}
将给出如下结果:
TASK [get my expected output from json data in a single task] **************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
{
"hosts": [
"host1.domain.com",
"host2.domain.com"
],
"name": "Cluster_1"
},
{
"hosts": [
"host3.domain.com",
"host4.domain.com"
],
"name": "Cluster_2"
}
]
}