Clean up AMI and related snapshots #8
No reviewers
Labels
No labels
bug
duplicate
enhancement
good first issue
help wanted
invalid
question
todo
todo 🗒️
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: stationgroup/ansible-experiments#8
Loading…
Reference in a new issue
No description provided.
Delete branch "feature-clean-up-ami-snapshots-#2"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Ansible playbook to remove oldest AMI and attached snapshots
Tested with Ansible version: 2.5.4
Requirements
This playbook needs the Python AWS SDK installed (both boto & boto3)
Variables
AWS authentication
AWS profiles
In this example we create an aws profile with the name
aws-ansible
.Create an AWS profile by editing
~/.aws/credentials
and add the followingstanza:
Set the Playbook variable
aws_profile
to the name of this profileexample:
Testing
In the
test
folder of this repository you can find an Ansible playbook thatcreates some test AMI's.
When running the cleanup playbook AMI-001 should be removed first. On subsequent runs AMI-002 should be removed and then AMI-003.
If no AMI's with the tag
DestroyImage: true
are available. The playbook skipsall tasks.
When AMI's with multiple snapshots are deregistered, all the attached snapshots
will be deleted.
NOTE: The deregistration of AMI's takes some time. So subsequent
runs of the cleanup playbook short after each other will result in the playbook
trying to deregister and delete snapshots of the same AMI. This should be an
idempotent action.
This is not the case if AMI's are tagged
Cleanup Playbook
prerequisites
The Playbook requires that you set the
AWS_REGION
environment variable becausesome modules used in this playbook require this even if it's set in the aws
profile.
Example:
Run the Playbook
Logging
Info about the cleanup actions are kept in a log file (default:
/tmp/ansible-ami-cleanup.log)
x-post: https://github.com/stationgroup/ansible-experiments/issues/2