Search code examples
amazon-web-servicesansibleamazon-ecs

How to delete AWS ECS repositories which contain images using Ansible


I want to delete an AWS ECS repository using Ansible. My Ansible version is 2.4.1.0 and it "should" support this as you can lookup here: http://docs.ansible.com/ansible/latest/ecs_ecr_module

However it doesn't work as intended because my repository still contains docker images.

Here's the code snippet:

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

The resulting error message is:

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

In the AWS Console it works perfectly fine. There's just a warning text which reminds you that there are still images left in the repository. But you're still able to force the deletion.

And now my question(s):
Is it somehow possible to force the deletion of the repository including its images?
... OR ...
Can I delete them with another tool separately before deleting the repository?

Maybe there simply is no implementation from the ansible side and I have to use the 'shell' module instead (and maybe open a feature request for that).

I'm very grateful for any advise.


Solution

  • First things first: Thanks to @vikas027 Solution from his/her/its answer: https://docs.aws.amazon.com/cli/latest/reference/ecr/delete-repository.html#examples

    History:

    Ok, now I figured out, that there currently is no ansible functionality which supports the implicit deletion of images when deleting repositories on ecs.

    BUT
    I've implemented a workaround that despite its ugliness works for me. I simply delete the image per shell module using the aws cli before actually removing the ecs repo.

    Here's the snippet to do so:

    - 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
    

    Hope that helps someone who faces this issue before ansible implements a possibility to delete images via built-in module.