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 }}"
---
- name: Find all the rds snapshots
rds:
command: facts
region: us-east-1
register: snapshot_results
this task returning local inbound error if instance_name is not given. ansible documentation for rds module says instance_name is not mandatory while getting facts.
TASK [rds-snapshot-cleanup : Find all the rds snapshots] ***************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: UnboundLocalError: local variable 'resource' referenced before assignment
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n File \"/tmp/ansible_SwP5AP/ansible_module_rds.py\", line 1329, in <module>\n main()\n File \"/tmp/ansible_SwP5AP/ansible_module_rds.py\", line 1325, in main\n invocations[module.params.get('command')](module, conn)\n File \"/tmp/ansible_SwP5AP/ansible_module_rds.py\", line 1018, in facts_db_instance_or_snapshot\n module.exit_json(changed=False, instance=resource.get_data())\nUnboundLocalError: local variable 'resource' referenced before assignment\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}
to retry, use: --limit @/home/ec2-user/ansible/rds-snapshot-cleanup.retry
if instance_name is given
task is working fine but the snapshots are not recorded in snapshot_results
TASK [rds-snapshot-cleanup : Find the snapshots to purge] **************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'snapshots_results' is undefined\n\nThe error appears to have been in '/home/ec2-user/ansible/roles/rds-snapshot-cleanup/tasks/main.yml': line 15, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Find the snapshots to purge\n ^ here\n"}