我正在為 AWS Secret Manager 撰寫 bash 腳本。
問題:
我有一個 aws 秘密管理器,它將包含多個秘密
示例:aws secretsmanager get-secret-value --secret-id 示例 | jq -r ".SecretString"
輸入.json
{"Username":"admin","Password":"admin","Endpoint":"localhost","DatabaseName":"example","Port":"3306"}
使用這個命令得到一個 json 輸出
現在我想提取所有值,例如 (Username , password,..)
獲取所有提取值后,添加另一個名為 (secondfile.json) 的 json 檔案
我有另一個 json 檔案,它將包含
這是 secondfile.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Aurora": {
"Username": "",
"Password": "",
"Endpoint": "",
"DatabaseName": "",
"Port": ""
}
}
示例輸出:提取的值現在存盤在 Aurora[] 陣列元素中的第二個 file.json 檔案中。
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Aurora": {
"Username": "admin",
"Password": "admin",
"Endpoint": "localhost",
"DatabaseName": "example",
"Port": "3306"
}
}
需要有關 bash 腳本的幫助。
注意:Input.json 值不固定,它可能包含 3 個 json 鍵/值或 n 個鍵/值
uj5u.com熱心網友回復:
如果我正確理解您的問題,您只需將輸入檔案嵌入/包裝到頂級檔案中。
echo '{"Username":"admin","Password":"admin","Endpoint":"localhost","DatabaseName":"example","Port":"3306"}' | jq '{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Aurora": .
}'
輸出:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Aurora": {
"Username": "admin",
"Password": "admin",
"Endpoint": "localhost",
"DatabaseName": "example",
"Port": "3306"
}
}
“Aurora”鍵將包含您的輸入檔案。如果您只需要特定欄位,則替換.為{Username,Password}
uj5u.com熱心網友回復:
這是一個解決方案,如何讓 jq--slurpfile選擇使用另一個檔案作為“模板”來格式化您的輸入,然后根據您的要求替換部分:
$ cat template.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Aurora": {
"Username": "",
"Password": "",
"Endpoint": "",
"DatabaseName": "",
"Port": "",
"Environment": ""
}
}
$ cat input.json
{
"Username": "admin",
"Password": "admin",
"Endpoint": "localhost",
"DatabaseName": "example",
"Port": "3306",
"SomethingElse": "ignore me"
}
$ jq --slurpfile tpl template.json '$tpl[0] {Aurora: with_entries(select(.key | IN($tpl[0].Aurora|keys[])))}' <input.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Aurora": {
"Username": "admin",
"Password": "admin",
"Endpoint": "localhost",
"DatabaseName": "example",
"Port": "3306"
}
}
或交換模板和輸入檔案:
$ jq --slurpfile in input.json '.Aurora |= keys as $keys | $in[0] | with_entries(select(.key | IN($keys[])))' <template.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Aurora": {
"Username": "admin",
"Password": "admin",
"Endpoint": "localhost",
"DatabaseName": "example",
"Port": "3306"
}
}
uj5u.com熱心網友回復:
如果您可以更改“模板”檔案,我建議不要將其保留為 JSON,而是將其改為 jq 程式:
$ cat input.json
{
"Username": "admin",
"Password": "admin",
"Endpoint": "localhost",
"DatabaseName": "example",
"Port": "3306",
"SomethingElse": "ignore me"
}
$ cat filter.jq
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Aurora": {
"Username": .Username,
"Password": .Password,
"Endpoint": .Endpoint,
"DatabaseName": .DatabaseName,
"Port": .Port
}
}
$ jq -f filter.jq < input.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Aurora": {
"Username": "admin",
"Password": "admin",
"Endpoint": "localhost",
"DatabaseName": "example",
"Port": "3306"
}
}
請注意,過濾器程式可以用簡寫語法撰寫:
$ cat filter.jq
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Aurora": {
Username,
Password,
Endpoint,
DatabaseName,
Port
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/515841.html
上一篇:sudo阻止執行以下命令
