I need help with merging List of Dictionaries. Below is a sample data of 2 dictionaries. I have tried several methods. The below json shows the sample input. While the only element that changes is the Host in the entire list of Dictionaries. I need to combine the data into a single list of dictionary to post an API call. And it is not always there will be only 4 Dictionaries part of the list.
Basically looking to group / merge the data such that the common details does not change and only new details get appended.
Input
[
{
"Batch_number": "Batch1",
"Date": "20221228",
"Host": "Host1",
"Job_Template_ID": 7,
"Patch_Cycle": 30,
"Patch_Day": "Day1",
"RRule": "INTERVAL=1;COUNT=1;FREQ=MINUTELY",
"Recurrency": "Once",
"Start_Time": "17:00:00",
"Technical_Tower": "Unix",
"Timezone": "Asia/Culcutta"
},
{
"Batch_number": "Batch1",
"Date": "20221230",
"Host": "Host11",
"Job_Template_ID": 7,
"Patch_Cycle": 30,
"Patch_Day": "Day2",
"RRule": "INTERVAL=1;COUNT=1;FREQ=MINUTELY",
"Recurrency": "Once",
"Start_Time": "17:00:00",
"Technical_Tower": "Unix",
"Timezone": "Asia/Culcutta"
},
{
"Batch_number": "Batch1",
"Date": "20230101",
"Host": "Host21",
"Job_Template_ID": 7,
"Patch_Cycle": 30,
"Patch_Day": "Day3",
"RRule": "INTERVAL=1;COUNT=1;FREQ=MINUTELY",
"Recurrency": "Once",
"Start_Time": "17:00:00",
"Technical_Tower": "Unix",
"Timezone": "Asia/Culcutta"
},
{
"Batch_number": "Batch1",
"Date": "20230103",
"Host": "Host31",
"Job_Template_ID": 7,
"Patch_Cycle": 30,
"Patch_Day": "Day4",
"RRule": "INTERVAL=1;COUNT=1;FREQ=MINUTELY",
"Recurrency": "Once",
"Start_Time": "17:00:00",
"Technical_Tower": "Unix",
"Timezone": "Asia/Culcutta"
}
]
Expected Output
{
"Batch_number": "Batch1",
"Date": "20230103",
"Host": ["Host1","Host11","Host21","Host31"],
"Job_Template_ID": 7,
"Patch_Cycle": 30,
"Patch_Day": "Day4",
"RRule": "INTERVAL=1;COUNT=1;FREQ=MINUTELY",
"Recurrency": "Once",
"Start_Time": "17:00:00",
"Technical_Tower": "Unix",
"Timezone": "Asia/Culcutta"}
I managed to achieve expected output by simple combine
filters.
Test playbook giving the expected result is below:
---
- name: Combine batches
hosts: localhost
connection: local
vars:
batches: >-
[
{
"Batch_number": "Batch1",
"Date": "20221228",
"Host": "Host1",
"Job_Template_ID": 7,
"Patch_Cycle": 30,
"Patch_Day": "Day1",
"RRule": "INTERVAL=1;COUNT=1;FREQ=MINUTELY",
"Recurrency": "Once",
"Start_Time": "17:00:00",
"Technical_Tower": "Unix",
"Timezone": "Asia/Culcutta"
},
{
"Batch_number": "Batch1",
"Date": "20221230",
"Host": "Host11",
"Job_Template_ID": 7,
"Patch_Cycle": 30,
"Patch_Day": "Day2",
"RRule": "INTERVAL=1;COUNT=1;FREQ=MINUTELY",
"Recurrency": "Once",
"Start_Time": "17:00:00",
"Technical_Tower": "Unix",
"Timezone": "Asia/Culcutta"
},
{
"Batch_number": "Batch1",
"Date": "20230101",
"Host": "Host21",
"Job_Template_ID": 7,
"Patch_Cycle": 30,
"Patch_Day": "Day3",
"RRule": "INTERVAL=1;COUNT=1;FREQ=MINUTELY",
"Recurrency": "Once",
"Start_Time": "17:00:00",
"Technical_Tower": "Unix",
"Timezone": "Asia/Culcutta"
},
{
"Batch_number": "Batch1",
"Date": "20230103",
"Host": "Host31",
"Job_Template_ID": 7,
"Patch_Cycle": 30,
"Patch_Day": "Day4",
"RRule": "INTERVAL=1;COUNT=1;FREQ=MINUTELY",
"Recurrency": "Once",
"Start_Time": "17:00:00",
"Technical_Tower": "Unix",
"Timezone": "Asia/Culcutta"
}
]
tasks:
- name: Show combined batch
debug:
var: batches | from_json | combine | combine({'Host':batch_hosts}) | to_nice_json
vars:
batch_hosts: >-
{{ batches | from_json | map(attribute='Host') }}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.