按照Boto代码打印出不需要的输出,我会看到我的EBS卷状态的状态而不是挂载点。
EC2预订结构:
object {1}
Reservations [1]
Instances[1]
BlockDeviceMappings[2]
DiviceName
Ebs{4}
Status
DeleteOnTermination
VolumeId
AttachTime
reservations = ec2Conn.get_all_instances(instance_ids=[my_id])
for reservation in reservations:
for instance in reservation.instances:
for BlockDeviceMappings in instance.block_device_mapping:
print(BlockDeviceMappings)
打印我:
/dev/sdf
/dev/sda1
按照AWS CLI命令打印我正确的状态:
aws ec2 describe-instances --instance myinstance-id --query Reservations[*].Instances[*].BlockDeviceMappings[*].{Ebs:Ebs.{s:Status}}
答案 0 :(得分:1)
此代码段将为您提供实例ID,卷ID及其当前状态。使用挂载点,您还可以获得一个boto.ec2.blockdevicemapping对象,您可以使用该对象访问其属性,如下例所示。希望它有所帮助。
from boto.regioninfo import *
from boto.ec2.connection import EC2Connection
# AWS connect info
aws_access_key_id='########## AWS Access Key ############'
aws_secret_access_key='########### AWS Secret Key ############'
region_name='ap-southeast-1'
region_ec2_endpoint='ec2.ap-southeast-1.amazonaws.com'
# Connect EC2
aws_region = RegionInfo(name=region_name, endpoint=region_endpoint)
conn = EC2Connection(aws_access_key_id,aws_secret_access_key,region=aws_region)
reservations = conn.get_all_instances(instance_ids=[my_ids])
for each in reservations:
for vol in each.instances[0].block_device_mapping.items():
print str(each.instances[0].id) + " " + vol[1].volume_id + " "+ vol[1].status
类似的其他一些用例
答案 1 :(得分:0)
如果您查看raw
响应,那么它看起来与此类似:
<blockDeviceMapping>
<item>
<deviceName>/dev/sda1</deviceName>
<ebs>
<volumeId>vol-xxxxxxxxxx</volumeId>
<status>attached</status>
<attachTime>2014-04-01T02:23:54.000Z</attachTime>
<deleteOnTermination>true</deleteOnTermination>
</ebs>
</item>
<item>
<deviceName>/dev/sdb</deviceName>
<ebs>
<volumeId>vol-xxxxxxx</volumeId>
<status>attached</status>
<attachTime>2014-04-01T02:23:54.000Z</attachTime>
<deleteOnTermination>false</deleteOnTermination>
</ebs>
</item>
</blockDeviceMapping>
所以你所要做的只是挖掘比你更深的一层。
>>> import boto
>>> ec2 = boto.connect_ec2()
>>> [ebs.status for _, ebs in ec2.get_all_instances(instance_ids=['i-xxxxxx'])[0].instances[0].block_device_mapping.items()]
[u'attached', u'attached']
如果您的目标是让EBS状态“看起来”非常靠近aws cli,您可以使用boto3
执行此操作:
>>> import boto3
>>> boto3.client('ec2')
>>> c.describe_instances(InstanceIds=['i-xxxxxx'])['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['Status']
'attached'
答案 2 :(得分:0)
使用CLI
response = json.loads(subprocess.check_output(
['aws', 'ec2', 'describe-volumes', '--volume-ids', Volume_ID, '--query',
'Volumes[*].{AZ:AvailabilityZone, size:Size, Device:Attachments[*].{Device:Device}}']))
Volume_AZ = response[0]['AZ']
Volume_Mount_Point = response[0]['Device'][0]['Device']
Volume_size = response[0]['size']