如何使用 Ansible 删除包含图像的 AWS ECS 存储库

Pat*_*ötz 3 amazon-web-services amazon-ecs ansible

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

但是它无法按预期工作,因为我的存储库仍然包含 docker 映像。

这是代码片段:

- name: destroy-ecr-repos
  ecs_ecr: name=jenkins-app state=absent
Run Code Online (Sandbox Code Playgroud)

产生的错误消息是:

... 
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 
...
Run Code Online (Sandbox Code Playgroud)

在AWS控制台中它运行得很好。只有一条警告文本提醒您存储库中仍有图像。但您仍然可以强制删除。

现在我的问题是:
是否可以以某种方式强制删除存储库(包括其图像)?
...或者...
我可以在删除存储库之前使用其他工具单独删除它们吗?

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

我非常感谢任何建议。

Pat*_*ötz 5

首先,感谢 @vikas027 他/她/其答案的解决方案: https://docs.aws.amazon.com/cli/latest/reference/ecr/delete-repository.html#examples

历史:

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


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

这是执行此操作的代码片段:

- 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
Run Code Online (Sandbox Code Playgroud)

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

  • AWS cli 现在支持强制删除 ECR 存储库 `$ aws ecr delete-repository --repository-name <repo_name> --force` 。[此处](https://docs.aws.amazon.com/cli/latest/reference/ecr/delete-repository.html#examples) 是更新后的 doco。 (4认同)