使用boto3描述具有标签值的Images(AMI)

我有一个描述图像的boto3脚本。

但是,我想自定义脚本,以便它仅向我显示标记的值,即AMI的名称。

当前显示:

([{u'Value': 'NAME OF AMI',u'Key': 'Name'}],'ami-xxxxxxxxxxxxxx','available')

但是我想要这样的东西:

**Name :'NAME OF AMI','available'**

这是我的剧本:

    import boto3
    import sys
    import pprint
    ec2 = boto3.resource('ec2')
    client = boto3.client('ec2')
    response = client.describe_images(Owners=['self'])
    for ami in response['Images']:
        print (ami['Tags'],ami['ImageId'],ami['State'])
s19870418 回答:使用boto3描述具有标签值的Images(AMI)

AMI上可以有多个标签。此代码将显示具有键Name的标签的Value(并且还将显示通常在控制台中以“ AMI名称”出现的AMI的Name名称!):

import boto3
import sys
import pprint
ec2 = boto3.resource('ec2')
client = boto3.client('ec2')
response = client.describe_images(Owners=['self'])
for ami in response['Images']:
    if 'Tags' in ami:
      name = [tag['Value'] for tag in ami['Tags'] if tag['Key'] == 'Name'][0]
    else:
      name = ''
    print (name,ami['Name'],ami['ImageId'],ami['State'])
本文链接:https://www.f2er.com/3052189.html

大家都在问