I am trying to cleanup the rds snapshots that are expired. We have expiration tag set with the 14 days from the day the snapshot created. These snapshots are created manually not automated snapshots.
Is there way we can leverage using ansible? I am currently using rds module but not able to get the snapshots facts. rds_snapshot_facts is available new version but we are using ansible 2.5.3.
Is shell scripting is only option available for this requirement?
This is how playbook currently looks like
- hosts: localhost
connection: local
gather_facts: true
vars:
date_fmt: “%Y-%m-%d”
roles: - name: rds-snapshot-cleanup
ansible\roles\rds-snapshot-cleanup\tasks\main.yml
-
name: Set query string to find snapshots to purge
set_fact:
snapshot_query: “[? tags.expiration < ‘{{ ansible_date_time.date|to_datetime(date_fmt) }}’ ]” -
name: Find all the rds snapshots
rds:
command: facts
region: us-east-1
register: snapshot_results -
name: Find the snapshots to purge
set_fact:
snapshots_to_purge: “{{ snapshots_results.snapshots | json_query( snapshot_query ) }}” -
name: Purge snapshots
rds:
command: delete
region: us-east-1
snapshot: “{{item.snapshot_name }}”
with_items: “{{snapshots_to_purge }}”