簡體   English   中英

從 cloudfromation 將 json output 存儲在 bash 中

[英]storing json output in bash from cloudfromation

我正在使用 aws ecs 查詢來獲取當前正在運行的任務正在使用的屬性列表。

命令 - cft = "aws ecs describe-tasks --cluster arn:aws:ecs:us-west-2:4984314772:cluster/secrets --tasks arn:aws:ecs:us-west-2:4984314772:task/secrets/86855757eec4487f9d4475a1f7c4cb0b

我將其存儲在 output 變量中

輸出 = $( 評估 $cft)

Output:

    "tasks": [
        {
            "attachments": [
                {
                    "id": "da8a1312-8278-46d5-8e3b-6b6a1d96f820",
                    "type": "ElasticNetworkInterface",
                    "status": "ATTACHED",
                    "details": [
                        {
                            "name": "subnetId",
                            "value": "subnet-0a151f2eb959ad4"
                        },
                        {
                            "name": "networkInterfaceId",
                            "value": "eni-081948e3666253f"
                        },
                        {
                            "name": "macAddress",
                            "value": "02:2a:9i:5c:4a:77"
                        },
                        {
                            "name": "privateDnsName",
                            "value": "ip-172-56-17-177.us-west-2.compute.internal"
                        },
                        {
                            "name": "privateIPv4Address",
                            "value": "172.56.17.177"
                        }
                    ]
                }
            ],
            "availabilityZone": "us-west-2a",
            "clusterArn": "arn:aws:ecs:us-west-2:4984314772:cluster/secrets",
            "containers": [
                {
                    "taskArn": "arn:aws:ecs:us-west-2:4984314772:task/secrets/86855757eec4487f9d4475a1f7c4cb0b",
                    "name": "nginx",
                    "image": "nginx",
                    "lastStatus": "PENDING",
                    "networkInterfaces": [
                        {
                            "attachmentId": "da8a1312-8278-46d5-6b6a1d96f820",
                            "privateIpv4Address": "172.31.17.176"
                        }
                    ],
                    "healthStatus": "UNKNOWN",
                    "cpu": "0"
                }
            ],
            "cpu": "256",
            "createdAt": "2020-12-10T18:00:16.320000+05:30",
            "desiredStatus": "RUNNING",
            "group": "family:nginx",
            "healthStatus": "UNKNOWN",
            "lastStatus": "PENDING",
            "launchType": "FARGATE",
            "memory": "512",
            "overrides": {
                "containerOverrides": [
                    {
                        "name": "nginx"
                    }
                ],
                "inferenceAcceleratorOverrides": []
            },
            "platformVersion": "1.4.0",
            "tags": [],
            "taskArn": "arn:aws:ecs:us-west-2:4984314772:task/secrets/86855757eec4487f9d4475a1f7c4cb0b",
            "taskDefinitionArn": "arn:aws:ecs:us-west-2:4984314772:task-definition/nginx:17",
            "version": 2
        }
    ],
    "failures": []
}

現在,如果回顯 $output.tasks[0].containers[0] 沒有任何反應,它會再次打印整個內容,我想將結果存儲在 output 變量中,並像我們在 json 格式中那樣引用不同的參數。

您將需要使用 json 解析器,例如 jq 等:

eval $cft | jq '.tasks[].containers[]'

為了避免使用 eval 你可以簡單的 pipe aws 命令到 jq 等:

aws ecs describe-tasks --cluster arn:aws:ecs:us-west-2:4984314772:cluster/secrets --tasks arn:aws:ecs:us-west-2:4984314772:task/secrets/86855757eec4487f9d4475a1f7c4cb0b | jq '.tasks[].containers[]'

或者:

cft=$(aws ecs describe-tasks --cluster arn:aws:ecs:us-west-2:4984314772:cluster/secrets --tasks arn:aws:ecs:us-west-2:4984314772:task/secrets/86855757eec4487f9d4475a1f7c4cb0b | jq '.tasks[].containers[]')

echo $cft | jq '.tasks[].containers[]'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM