-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvm_snapshot_clone_older.yml
72 lines (62 loc) · 2.88 KB
/
vm_snapshot_clone_older.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
- name: clone all snapshots older than use_date
hosts: all
become: false
gather_facts: false
connection: ansible.builtin.local
strategy: host_pinned
environment: #if set here - hypercore modules will automatically use this for each remote cluster - avoiding need to specify cluster_instance for each test
SC_HOST: "https://{{ inventory_hostname }}"
SC_USERNAME: "{{ scale_user | default('admin') }}"
SC_PASSWORD: "{{ scale_pass | default('admin') }}"
SC_TIMEOUT: 600
vars:
# Format: 'YYYY-MM-DD hh:mm:ss'
# All snapshots older than this date will be deleted.
# use_date timezone should match the Scale cluster timezone
use_date: '2022-01-01 12:52:00'
tasks:
# ------------------------------------------------------
- name: List all snapshots
scale_computing.hypercore.vm_snapshot_info:
register: snapshot_results
- name: Convert date to unix timestamp 'epoch'
ansible.builtin.set_fact:
epoch_timestamp: "{{ (use_date | to_datetime).strftime('%s') }}"
- name: Show epoch_timestamp
ansible.builtin.debug:
var: epoch_timestamp
- name: Create filtered_snapshots list
ansible.builtin.set_fact:
filtered_snapshots: []
- name: Loop through snapshots and add snapshots that are older than 'use_date'
ansible.builtin.set_fact:
filtered_snapshots: "{{ filtered_snapshots + [item] }}"
when: item.timestamp < epoch_timestamp | int
loop: "{{ snapshot_results.records }}"
no_log: true
- name: Show only snapshots that are older than 'use_date'
ansible.builtin.debug:
var: filtered_snapshots
# We could reuse "filtered_snapshots" here instead of "snapshot_results" and avoid the "when" statement.
# But leaving it as is for example purposes.
# Since this is the only mandatory task of the playbook, can be copy-pasted and reused as standalone task.
- name: Loop through list of snapshots and clone all older than the 'use_date' to vm_name+serial_number
scale_computing.hypercore.vm_clone:
vm_name: "{{ item.vm.name }}-{{ item.vm.snapshot_serial_number }}"
source_vm_name: "{{ item.vm.name }}"
source_snapshot_uuid: "{{ item.snapshot_uuid }}"
when: item.timestamp < epoch_timestamp | int
loop: "{{ snapshot_results.records }}"
# - name: Create filtered_snapshots list - second time
# ansible.builtin.set_fact:
# filtered_snapshots: []
# - name: Loop through snapshots and add snapshots that are older than 'use_date' - second time
# ansible.builtin.set_fact:
# filtered_snapshots: "{{ filtered_snapshots + [item] }}"
# when: item.timestamp < epoch_timestamp | int
# loop: "{{ snapshot_results.records }}"
# no_log: true
# - name: Show only snapshots that are older than 'use_date' - second time
# ansible.builtin.debug:
# var: filtered_snapshots