Cleaning up manual RDS snapshots

82 views
Skip to first unread message

tharun....@gmail.com

unread,
Jul 22, 2018, 10:58:29 AM7/22/18
to Ansible Project
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"}

Karl Auer

unread,
Jul 22, 2018, 7:27:31 PM7/22/18
to ansible...@googlegroups.com
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 received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/41f8d660-dfa2-4d2f-9677-04cbb154ff7d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Karl Auer

Email  : ka...@2pisoftware.com
Website: 
http://2pisoftware.com

GPG/PGP : 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
Previous: F0AB 6C70 A49D 1927 6E05 81E7 AD95 268F 2AB6 40EA

Will Thames

unread,
Jul 22, 2018, 7:54:55 PM7/22/18
to Ansible Project
Reply all
Reply to author
Forward
0 new messages