I want to create volumes every day based on current date , that is ok , but when i want to delete volumes which are older than x days i can't to get that working , which method should i use ? this is my ansible playbook example:
-
name: Delete Old Volumes based on date of creation
hosts: localhost
gather_facts: novars:
hostname: "192.168.1.1"
username: "admin"
password: "admin123"
vserver: "example"
days_to_keep: 6 # Change this to the desired number of daystasks:
-
name: Get list of volumes
na_ontap_volume_info:
hostname: "{{ hostname }}"
username: "{{ username }}"
password: "{{ password }}"
vserver: "{{ vserver }}"
register: volume_list -
name: Calculate cut-off date
set_fact:
cutoff_date: "{{ ansible_date_time.iso8601 | regex_replace('T.*', '') | to_datetime('%Y-%m-%d') - timedelta(days=days_to_keep) }}" -
name: Filter volumes by name containing dates and creation date
set_fact:
filtered_volumes: "{{ volume_list.volumes | selectattr('name', 'match', '.*\d{4}-\d{2}-\d{2}') | selectattr('creation_time', '>=', cutoff_date) | list }}" -
name: Delete filtered volumes
loop: "{{ filtered_volumes }}"
na_ontap_volume:
state: absent
hostname: "{{ hostname }}"
username: "{{ username }}"
password: "{{ password }}"
name: "{{ item.name }}"
-
thanks in advance