【发布时间】:2021-12-02 01:01:23
【问题描述】:
我正在尝试根据标签过滤 ebs 卷,我计划删除那些标签键(名称)对于该特定卷不存在的卷。我试过这个脚本,但只有在为该卷创建标签并且它的值为空字符串时才会过滤标签键(名称)。但我的要求是获取该卷不存在标签 Key(Name) 的那些卷。仅供参考,对于我要删除的那些卷,大约有 4-5 个标签。非常感谢任何形式的指导。
#Filtering volumes based on tag and status, and deletion
for region in region_list: #list of regions
to_terminate=[]
ec2_volume = boto3.resource('ec2',region_name=region)
#volumes = ec2_volume.volumes.all()
print(region)
for volume in ec2_volume.volumes.filter(
Filters=[
{
'Name': 'tag:Name',
'Values': [
'',
]
},
{
'Name': 'status',
'Values': [
'available',
]
}
]
):
print('Evaluating volume {0}'.format(volume.id))
print('The number of attachments for this volume is {0}'.format(len(volume.attachments)))
to_terminate.append(volume)
print(to_terminate)
【问题讨论】:
-
为什么不简单地检索 all 卷,然后使用 Python 检查是否有
Name标签。如果它不存在,那么采取你想要的行动。
标签: python amazon-web-services boto3 volumes