Using jq to manipulate JSON output from the AWS CLI

所要時間2分
コンテンツレベル: 基礎
0

How to handle JSON output from the AWS CLI

Situation

I've been troubleshooting an issue with certain VMs not showing up as available to be backed up by AWS Backup.

Task

I need to compare the list of VMs in my VMware vCenter Server to the list of VMs reported by the AWS Backup Gateway.

Actions

Getting the VM list is simple with PowerCLI . This dumps all VM names to a text file

Get-VM | Select-Object Name > vm-list.txt

I can list VMs available for backup on my backup gateway with the list-virtual-machines AWS CLI command. However, the output of this command is JSON.

aws backup-gateway list-virtual-machines --hypervisor-arn arn:aws:backup-gateway:us-east-1:01234567890:hypervisor/hype-F2D20A87

Output is truncated for brevity, but the actual VM list was in the hundreds.

{
    "VirtualMachines": [
        {
            "HostName": "10.7.8.9",
            "HypervisorId": "hype-F2D20A87",
            "Name": "lnx01",
            "Path": "/SDDC-Datacenter/vm/",
            "ResourceArn": "arn:aws:backup-gateway:us-east-1:01234567890:vm/vm-00005689EDF16B2A2"
        },
        {
            "HostName": "10.7.8.10",
            "HypervisorId": "hype-F2D20A87",
            "Name": "lnx02",
            "Path": "/SDDC-Datacenter/vm/",
            "ResourceArn": "arn:aws:backup-gateway:us-east-1:01234567890:vm/vm-0000727282DDCFDE4"
        }
   ]
}

jq is a common tool on Linux and Mac to parse JSON output. You can also get jq on Windows, which is what I use, but Windows folks tend to be less familiar with jq. You can download jq here.

Once installed, I run the command again, but pipe it to jq with the syntax '.VirtualMachines[].Name'. This tells jq to look into the JSON array named VirtualMachines, then extract the value of the key named Name for every object in the array.

aws backup-gateway list-virtual-machines --hypervisor-arn arn:aws:backup-gateway:us-east-1:01234567890:hypervisor/hype-F2D20A87  |  jq '.VirtualMachines[].Name'
"lnx01"
"lnx02"

This output has double quotes around it, which I don't want. But jq has an -r switch to remove them

aws backup-gateway list-virtual-machines --hypervisor-arn arn:aws:backup-gateway:us-east-1:01234567890:hypervisor/hype-F2D20A87  |  jq '.VirtualMachines[].Name' -r
lnx01
lnx02

Result

I can now output that list to a text file and run a diff against the PowerCLI output to figure out what's missing.

profile pictureAWS
エキスパート
公開済み 2ヶ月前577ビュー
コメントはありません

関連するコンテンツ