【问题标题】:How to delete AWS ECS repositories which contain images using Ansible如何使用 Ansible 删除包含图像的 AWS ECS 存储库
【发布时间】:2020-04-12 05:12:29
【问题描述】:

我想使用 Ansible 删除 AWS ECS 存储库。 我的 Ansible 版本是 2.4.1.0,它“应该”支持这一点,您可以在此处查找:http://docs.ansible.com/ansible/latest/ecs_ecr_module

但是它没有按预期工作,因为我的存储库仍然包含 docker 图像。

这里是sn-p的代码:

- name: destroy-ecr-repos
  ecs_ecr: name=jenkins-app state=absent

产生的错误信息是:

... 
The error was: RepositoryNotEmptyException: An error occurred (RepositoryNotEmptyException) when calling the DeleteRepository operation: The repository with name 'jenkins-app' in registry with id 'xyz' cannot be deleted because it still contains images 
...

在 AWS 控制台中它工作得非常好。只有一个警告文本提醒您存储库中仍有图像。但您仍然可以强制删除。

现在我的问题是:
是否可以强制删除包括其图像在内的存储库?
... 或 ...
在删除存储库之前,我可以使用另一个工具单独删除它们吗?

也许在 ansible 方面根本没有实现,我必须改用“shell”模块(并且可能为此打开一个功能请求)。

我非常感谢任何建议。

【问题讨论】:

    标签: amazon-web-services ansible amazon-ecs


    【解决方案1】:

    第一件事:感谢@vikas027 他/她/它的答案的解决方案: https://docs.aws.amazon.com/cli/latest/reference/ecr/delete-repository.html#examples

    历史:

    好的,现在我发现,在删除 ecs 上的存储库时,目前没有支持隐式删除图像的 ansible 功能。

    但是
    我已经实施了一种解决方法,尽管它对我来说很丑陋。 在实际删除 ecs 存储库之前,我只需使用 aws cli 删除每个 shell 模块的图像。

    这是执行此操作的 sn-p:

    - name: Delete remaining images in our repositories
      shell: |
        aws ecr list-images --repository-name jenkins-app --query 'imageIds[*]' --output text | while read imageId; do aws ecr batch-delete-image --repository-name jenkins-app --image-ids imageDigest=$imageId; done
    
    - name: destroy-ecr-repo jenkins-app
      ecs_ecr: name=jenkins-app state=absent
    

    希望在 ansible 实现通过内置模块删除图像的可能性之前帮助遇到此问题的人。

    【讨论】:

    • AWS cli 现在支持强制删除 ECR 存储库 $ aws ecr delete-repository --repository-name <repo_name> --forceHere 是更新的文档。
    • @vikas027 非常感谢您的更新。我会将此添加到我的自我接受的;-) 答案中。
    • 查询imageIds[*]返回图像摘要和标签,导致一些错误。 imageIds[*].imageDigest 只选择摘要。
    猜你喜欢
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2021-01-03
    • 2012-06-13
    相关资源
    最近更新 更多