简体   繁体   English

如何使用 Boto3 列出没有特定标签的快照

[英]How to list snapshots that DONT have a specific tag using Boto3

I'm trying to find the snapshots that don't have a certain tag.我正在尝试查找没有特定标签的快照。

For snapshots, I want all snapshots that don't have the Do-Not-Delete tag.对于快照,我想要所有没有Do-Not-Delete标签的快照。 No matter what is the value of a tag.不管标签的价值是什么。

This is what I'm doing now:这就是我现在正在做的事情:

snaps_to_remove = ec2_client.describe_snapshots(OwnerIds=account_ids)
    for snap in snaps_to_remove['Snapshots']:
        # Remove all snapshots with the tag Do-Not-Delete functionality goes here
        print(snap) 

I don't think if there's a filter for negative comparison based.我不认为是否有基于负面比较的过滤器。 What is the correct way to loop through and get filter out the list with the specific tags?循环并过滤掉带有特定标签的列表的正确方法是什么?

If the snapshot contains Tags and one of the tags has a Key of 'Do-Not-Delete', skip the snapshot:如果快照包含标签,并且其中一个标签的键为“Do-Not-Delete”,则跳过快照:

snaps_to_remove = ec2_client.describe_snapshots(OwnerIds=account_ids)
for snap in snaps_to_remove['Snapshots']:
    # Skip snapshots with a Do-Not-Delete tag
    if 'Tags' in snap and [tag for tag in snap['Tags'] if tag['Key'] == 'Do-Not-Delete']:
        continue
    print(snap) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM