【问题标题】:Extract the Tag name from response从响应中提取标签名称
【发布时间】:2021-11-21 14:50:18
【问题描述】:

如何使用 python boto3 从以下响应中提取'Value': 'Ansible'

{
    "Groups": [],
    "Instances": [
        {
            "AmiLaunchIndex": 0,
            "ImageId": "ami-xxxxx",
            "InstanceId": "i-xxxxxxx",
            "InstanceType": "t2.micro",
            "KeyName": "key-xxxxxx-managed-aws-services",
            "LaunchTime": datetime.datetime(2021, 9, 24, 8, 10, 48, tzinfo=tzutc()),
            "Monitoring": {"State": "disabled"},
            "Placement": {
                "AvailabilityZone": "us-west-2a",
                "GroupName": "",
                "Tenancy": "default",
            },
            "PrivateDnsName": "ip-xxxxxx.us-west-2.compute.internal",
            "PrivateIpAddress": "172.31.19.135",
            "ProductCodes": [],
            "PublicDnsName": "",
            "State": {"Code": 80, "Name": "stopped"},
            "StateTransitionReason": "User initiated (2021-09-24 18:30:08 GMT)",
            "SubnetId": "subnet-9e7714e7",
            "VpcId": "vpc-xxxx",
            "Architecture": "x86_64",
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/xvda",
                    "Ebs": {
                        "AttachTime": datetime.datetime(
                            2021, 4, 5, 9, 19, 39, tzinfo=tzutc()
                        ),
                        "DeleteOnTermination": True,
                        "Status": "attached",
                        "VolumeId": "vol-xxxxxx",
                    },
                }
            ],
            "ClientToken": "",
            "EbsOptimized": False,
            "EnaSupport": True,
            "Hypervisor": "xen",
            "IamInstanceProfile": {
                "Arn": "arn:aws:iam::xxxxxx:instance-profile/AmazonSSMRoleForInstancesQuickSetup",
                "Id": "xxxxx",
            },
            "NetworkInterfaces": [
                {
                    "Attachment": {
                        "AttachTime": datetime.datetime(
                            2021, 4, 5, 9, 19, 38, tzinfo=tzutc()
                        ),
                        "AttachmentId": "eni-attach-xxxxx",
                        "DeleteOnTermination": True,
                        "DeviceIndex": 0,
                        "Status": "attached",
                        "NetworkCardIndex": 0,
                    },
                    "Description": "",
                    "Groups": [{"GroupName": "default", "GroupId": "sg-84127bf5"}],
                    "Ipv6Addresses": [],
                    "MacAddress": "xxxxxxxx",
                    "NetworkInterfaceId": "eni-xxxxxx",
                    "OwnerId": "xxxxx",
                    "PrivateDnsName": "ip-xxxxx.us-west-2.compute.internal",
                    "PrivateIpAddress": "xxxxxx",
                    "PrivateIpAddresses": [
                        {
                            "Primary": True,
                            "PrivateDnsName": "ip-xxxxx.us-west-2.compute.internal",
                            "PrivateIpAddress": "xxxxxxxx",
                        }
                    ],
                    "SourceDestCheck": True,
                    "Status": "in-use",
                    "SubnetId": "subnet-9e7714e7",
                    "VpcId": "vpc-d1434da8",
                    "InterfaceType": "interface",
                }
            ],
            "RootDeviceName": "/dev/xvda",
            "RootDeviceType": "ebs",
            "SecurityGroups": [{"GroupName": "default", "GroupId": "sg-84127bf5"}],
            "SourceDestCheck": True,
            "StateReason": {
                "Code": "Client.UserInitiatedShutdown",
                "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
            },
            "Tags": [{"Key": "Name", "Value": "Ansible"}],
            "VirtualizationType": "hvm",
            "CpuOptions": {"CoreCount": 1, "ThreadsPerCore": 1},
            "CapacityReservationSpecification": {
                "CapacityReservationPreference": "open"
            },
            "HibernationOptions": {"Configured": False},
            "MetadataOptions": {
                "State": "applied",
                "HttpTokens": "optional",
                "HttpPutResponseHopLimit": 1,
                "HttpEndpoint": "enabled",
            },
            "EnclaveOptions": {"Enabled": False},
        }
    ],
    "OwnerId": "xxxxx",
    "ReservationId": "r-0c0eb5xxxxxx",
}

【问题讨论】:

    标签: python boto3


    【解决方案1】:

    所以 boto3 响应是一个 python 字典,您只需要在 python 代码中循环它。类似的东西。

    for instance in response["Instances"]:
        print("Instance ID: {}").format(instance["InstanceId"])
        print("Tags: {}\n").format(instance["Tags"])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多