简体   繁体   中英

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. 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:

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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