【问题标题】:How to list snapshots that DONT have a specific tag using Boto3如何使用 Boto3 列出没有特定标签的快照
【发布时间】:2020-06-08 20:01:08
【问题描述】:

我正在尝试查找没有特定标签的快照。

对于快照,我想要所有没有Do-Not-Delete 标签的快照。不管标签的价值是什么。

这就是我现在正在做的事情:

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) 

我不认为是否有基于负面比较的过滤器。循环并过滤掉带有特定标签的列表的正确方法是什么?

【问题讨论】:

    标签: python amazon-web-services amazon-ec2 boto3


    【解决方案1】:

    如果快照包含标签,并且其中一个标签的 Key 为“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) 
    

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 2017-11-18
      • 2020-01-07
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多