我有两个json,想将一个附加到另一个json并将其保存在一个文件中。我已经完成了一个事实,可以读取值并将其放入变量中,方法如下:
- name: Set json combine to add new event
set_fact:
event_json_create: "{{ lookup('file', 'event_template.json') }}"
- name: Set json combine to get the existing list of events
set_fact:
event_json_existing: "{{ lookup('file', 'notification.json') }}"
现在我想将event_json_create附加到event_json_existing。
event_json_create看起来像这样:
"event_json_create": {
"LambdaFunctionConfigurations": [{
"Events": [
"s3:ObjectCreated:*"
],
"Filter": {
"Key": {
"FilterRules": [{
"Name": "prefix",
"Value": [
"keying_service/response/"
]
}]
}
},
"LambdaFunctionArn": "arn:aws:lambda:us-east-1:*******:function:xyz"
}]
}
event_json_existing看起来像这样:
"event_json_existing": {
"LambdaFunctionConfigurations": [
{
"Events": [
"s3:ObjectCreated:*"
],
"Filter": {
"Key": {
"FilterRules": [
{
"Name": "Prefix",
"Value": "staging/inbound/Source_Contact/ac/input_fia/"
}
]
}
},
"Id": "Eventtry",
"LambdaFunctionArn": "arn:aws:lambda:us-east-1:******:function:abc"
}
]
}
如何将两个json附加在ansible中,确保两个json都属于主要组:LambdaFunctionConfigurations,然后可以将其写入json文件。所以我期望输出:
{
"LambdaFunctionConfigurations": [{
"Events": [
"s3:ObjectCreated:*"
],
"Filter": {
"Key": {
"FilterRules": [{
"Name": "prefix",
"Value": [
"keying_service/response/"
]
}]
}
},
"LambdaFunctionArn": "arn:aws:lambda:us-east-1:*******:function:xyz"
},
{
"Events": [
"s3:ObjectCreated:*"
],
"Filter": {
"Key": {
"FilterRules": [{
"Name": "Prefix",
"Value": "staging/inbound/Source_Contact/ac/input_fia/"
}]
}
},
"Id": "Eventtry",
"LambdaFunctionArn": "arn:aws:lambda:us-east-1:*******:function:abc"
}
]
}
请帮助!
此模板
shell> cat events.json.j2
{{ events|to_nice_json }}
和以下任务
- set_fact:
events: "{{ {'LambdaFunctionConfigurations':
([event_json_create.LambdaFunctionConfigurations.0] +
[event_json_existing.LambdaFunctionConfigurations.0])} }}"
- template:
src: events.json.j2
dest: events.json
给予
shell> cat events.json
{
"LambdaFunctionConfigurations": [
{
"Events": [
"s3:ObjectCreated:*"
],
"Filter": {
"Key": {
"FilterRules": [
{
"Name": "prefix",
"Value": [
"keying_service/response/"
]
}
]
}
},
"LambdaFunctionArn": "arn:aws:lambda:us-east-1:*******:function:xyz"
},
{
"Events": [
"s3:ObjectCreated:*"
],
"Filter": {
"Key": {
"FilterRules": [
{
"Name": "Prefix",
"Value": "staging/inbound/Source_Contact/ac/input_fia/"
}
]
}
},
"Id": "Eventtry",
"LambdaFunctionArn": "arn:aws:lambda:us-east-1:******:function:abc"
}
]
}