Cleaning up manual RDS snapshots

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 }}”

You register “snapshot_results”, but you reference “snapshots_results”.

There may be other problems, but that’s why you are getting the undefined variable error.

Also, you should at some point make sure your code can handle the situation where there really are no snapshots to purge.

Regards, K.

You can use rds_snapshot_facts with Ansible 2.5.3

http://willthames.github.io/2017/12/12/using-updated-modules-with-stable-ansible.html