I am trying to build an array to use for with_nested, but I cannot get past why json_query isn't outputting the actual hosts assigned to each cluster:
Play:
- 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 data:
{
"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"
}
],
},
}
Expected results:
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"
}
]
}
Actual results:
TASK [debug] *********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
{
"hosts": null,
"name": "Cluster_1"
},
{
"hosts": null,
"name": "Cluster_2"
}
]
}