---
- name: Creating a volume snapshot
hosts: Test-ctrl
gather_facts: True
tasks:
- name: Creating snapshot of Test
os_volume_snapshot:
auth:
auth_url: http://20.10.X.X:5000/v3/
username: XXXXXXX
password: XCXCXCXC
project_name: test-stack
project_domain_name: Default
user_domain_name: Default
state: absent
validate_certs: False
display_name: Test- {{ lookup('pipe','date +%Y-%m-%d-%H-%M-%S') }}
volume: Test-1
force: yes
how to write a playbook to delete OpenStack volume snapshot of older than 10 days
Here my playbook to create volume. But customize to delete volume older than 10 days or 5 days ????
I also have a need to do this but, sadly, it is not possible with the
os_volume_snapshotmodule. Neither is it possible using any of the OpenStack modules in Ansible (2.9). Also theos_volume_snapshotmakes thevolumeparameter mandatory (which is silly - as you don't need to know the name of the original volume to delete a snapshot).So, it you "must" use the
os_volume_snapshotthen you're out of luck.The built-in
os_modules are very much a "work in progress" with regard to comprehensive control of OpenStack clusters in Ansible, and it is of little use to you for the task you've identified.But ... hold on ...
Like you, I need to automate this and it can be accomplished using Ansible and the official
python-openstackclientmodule. OK - it's not "pure" Ansible (i.e. it's not using purely built-in modules) but it's a playbook that uses the built-incommandmodule and it works.(BTW - no warranty provided in the following - and use at your own risk)
So, here's a playbook that I'm running that deletes snapshot volumes that have reached a defined age in days. You will need to provide the following variables...
os_auth_url(i.e. "https://example.com:5000/v3")os_usernameos_passwordos_project_nameos_project_domain_name(i.e. "Default")os_user_domain_nameAnd
retirement_age_days. A +ve value of days where all snapshot volumes that have reached that age are deleted. If 0 all snapshots are deleted.A summary of what the playbook does: -
openstack volume snapshot listto get a list of snapshot volumes in the projectopenstack volume snapshot showto get information about each snapshot (i.e. itscreated_atdate) and builds a lit of volume ages (in days)openstack volume snapshot deleteto delete all volumes that are considered too old