I get the below lists from HOSTS:
"HOSTNAME": [
"H1",
"H2",
"H3"
]
"SW_VERSION": [
"7.2.2",
"5.2.2",
"6.2.2"
]
"OSPF_NEIGHBOR": [
"10.1.1.1",
"10.1.1.2",
"10.1.1.3"
]
And I am converting them to List of Dict Objects as below: (objective is to create a report from the data that we get)
- set_fact:
host_data: "{{ h_data|default([]) + [ { 'HOSTNAME': item.0, 'SW_VERSION': item.1, 'OSPF_NEIGHBOR': item.2} ] }}"
with_together:
- "{{ HOSTNAME }}"
- "{{ SW_VERSION }}"
- "{{ OSPF_NEIGHBOR }}"
I get the output as below:
"host_data": [
{
"HOSTNAME": "H1",
"OSPF_NEIGHBOR": "10.1.1.1",
"SW_VERSION": "7.2.2"
},
{
"HOSTNAME": "H2",
"OSPF_NEIGHBOR": "10.1.1.2",
"SW_VERSION": "6.2.2"
},
{
"HOSTNAME": "H3",
"OSPF_NEIGHBOR": "10.1.1.3",
"SW_VERSION": "5.2.2"
}
]
I gave HOSTNAME as item.0, SW_VERSION as item.1 and OSPF_NEIGHBOR as item.2, but in the output OSPF_NEIGHBOR comes second in the dict elements which affects the table format/column order.
How can we make sure the order is preserved?
Or Is there a way to re-arrange the dict elements order in the host_data list according to the required column order?
