【问题标题】:Using jq to parse json output of AWS CLI tools使用 jq 解析 AWS CLI 工具的 json 输出
【发布时间】:2014-05-07 10:37:26
【问题描述】:

我想使用 jq (http://stedolan.github.io/jq/) 解析来自 aws ec2 describe-instances 的 json 输出,并仅在标签包含名称的情况下返回实例 ID 和标签。业主,成本中心。

这是表示源输出的部分编辑 json:

{
  "OwnerId":"121456789127",
  "ReservationId":"r-48465168",
  "Groups":[

  ],
  "Instances":[
    {
      "Monitoring":{
        "State":"disabled"
      },
      "PublicDnsName":null,
      "RootDeviceType":"ebs",
      "State":{
        "Code":16,
        "Name":"running"
      },
      "EbsOptimized":false,
      "LaunchTime":"2014-03-19T09:16:56.000Z",
      "PrivateIpAddress":"10.250.171.248",
      "ProductCodes":[
        {
          "ProductCodeId":"aacglxeowvn5hy8sznltowyqe",
          "ProductCodeType":"marketplace"
        }
      ],
      "VpcId":"vpc-86bab0e4",
      "StateTransitionReason":null,
      "InstanceId":"i-1234576",
      "ImageId":"ami-b7f6c5de",
      "PrivateDnsName":"ip-10-120-134-248.ec2.internal",
      "KeyName":"Test_Virginia",
      "SecurityGroups":[
        {
          "GroupName":"Test",
          "GroupId":"sg-12345b"
        }
      ],
      "ClientToken":"VYeFw1395220615808",
      "SubnetId":"subnet-12345314",
      "InstanceType":"t1.micro",
      "NetworkInterfaces":[
        {
          "Status":"in-use",
          "SourceDestCheck":true,
          "VpcId":"vpc-123456e4",
          "Description":"Primary network interface",
          "NetworkInterfaceId":"eni-3619f31d",
          "PrivateIpAddresses":[
            {
              "Primary":true,
              "PrivateIpAddress":"10.120.134.248"
            }
          ],
          "Attachment":{
            "Status":"attached",
            "DeviceIndex":0,
            "DeleteOnTermination":true,
            "AttachmentId":"eni-attach-9210dee8",
            "AttachTime":"2014-03-19T09:16:56.000Z"
          },
          "Groups":[
            {
              "GroupName":"Test",
              "GroupId":"sg-123456cb"
            }
          ],
          "SubnetId":"subnet-31236514",
          "OwnerId":"109030037527",
          "PrivateIpAddress":"10.120.134.248"
        }
      ],
      "SourceDestCheck":true,
      "Placement":{
        "Tenancy":"default",
        "GroupName":null,
        "AvailabilityZone":"us-east-1c"
      },
      "Hypervisor":"xen",
      "BlockDeviceMappings":[
        {
          "DeviceName":"/dev/sda",
          "Ebs":{
            "Status":"attached",
            "DeleteOnTermination":false,
            "VolumeId":"vol-37ff097b",
            "AttachTime":"2014-03-19T09:17:00.000Z"
          }
        }
      ],
      "Architecture":"x86_64",
      "KernelId":"aki-88aa75e1",
      "RootDeviceName":"/dev/sda1",
      "VirtualizationType":"paravirtual",
      "Tags":[
        {
          "Value":"Server for testing RDS feature in us-east-1c AZ",
          "Key":"Description"
        },
        {
          "Value":"RDS_Machine (us-east-1c)",
          "Key":"Name"
        },
          {
          "Value":"1234",
          "Key":"Cost.centre"
        },
        {
          "Value":"Jyoti Bhanot",
          "Key":"Owner"
        }
      ],
      "AmiLaunchIndex":0
    }
  ]
}

我正在使用这个命令:

 aws ec2 describe-instances | jq '.Instances[] | select(.Tags==["Name","Owner","cost.centre") | .InstanceId, .Tags'

但它给了我错误:

                                                           ^
   error: Invalid character
.Reservations[].Instances | map(select(has("Tags"))) |.Tags[].Key=="cost.centre")) | map(select(has("InstanceId"))) | .[].InstanceId,.[].Tags
                                                                                ^
error: Invalid character
.Reservations[].Instances | map(select(has("Tags"))) |.Tags[].Key=="cost.centre")) | map(select(has("InstanceId"))) | .[].InstanceId,.[].Tags
                                                                                 ^
2 compile errors

[Errno 32] Broken pipe

请帮我解决这个错误

【问题讨论】:

    标签: json amazon-web-services amazon-ec2 jq


    【解决方案1】:

    主要问题是您的查询缺少结束“]”,但我认为过滤具有这些标签的 EC2 实例有点棘手。

    这可能有点令人费解,但这样的事情对我有用:

    aws ec2 describe-instances | jq '.Instances | map(select(has("Tags"))) | map(select(.Tags[].Key=="Name" and .Tags[].Key=="Owner" and .Tags[].Key=="Cost.centre")) | map(select(has("InstanceId"))) | .[].InstanceId,.[].Tags'
    

    【讨论】:

    • 抱歉,我的 aws 命令有一个“Reservations”顶级元素,“cost.centre”应该是 Cost.centre”。我已经更新了命令以匹配您提供的 JSON。
    • 你也可以区分大小写吗?
    • @user3086014 我在 jq 中看不到不区分大小写的支持。
    • @MeghaSharma 你能告诉我错误吗?你提供的 JSON 对我有用。
    【解决方案2】:

    试试这个:

    $ aws ec2 describe-instances --output json | jq '.Reservations[].Instances[] | (.Tags | from_entries) as $tags | select($tags.Owner != null) | {InstanceID: .InstanceId, Name: $tags.Name, Owner: $tags.Owner, CostCenter: $tags."cost.center"}'
    

    ...返回:

    {
      "InstanceID": "i-734cbc51",
      "Name": "someserver001",
      "Owner": "john.smith@null",
      "CostCenter": "ABC001"
    }
    

    【讨论】:

    • 这适用于 aws ec2 describe-instances 的当前输出,但我只是注意到它需要针对您的示例输入进行调整;这适用于您的样品:猫样品 | jq '.Instances[] | (.Tags | from_entries) 作为 $tags |选择($tags.Owner != null) | {InstanceID:.InstanceId,名称:$tags.Name,所有者:$tags.Owner,成本中心:$tags."Cost.centre"}'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2023-04-08
    相关资源
    最近更新 更多