-
Notifications
You must be signed in to change notification settings - Fork 2
/
create.yml
84 lines (78 loc) · 2.74 KB
/
create.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
73
74
75
76
77
78
79
80
81
82
83
84
---
- name: Create Longhorn backups
hosts: vm
tags: backup
tasks:
- name: Get Longhorn volumes
become: true
kubernetes.core.k8s_info:
kubeconfig: "{{ kubeconfig_path }}"
kind: Volume
api_version: longhorn.io/v1beta2
register: longhorn_volumes
- name: Create list of backup names
ansible.builtin.set_fact:
longhorn_volumes_map: "{{ longhorn_volumes_map | default([])+
[{'volumeName': item.metadata.name,
'backupName': item.metadata.name ~ ansible_date_time.epoch_int ,
'pvName': item.status.kubernetesStatus.pvName,
'pvcName': item.status.kubernetesStatus.pvcName,
'namespace': item.status.kubernetesStatus.namespace}] }}"
loop: "{{ longhorn_volumes.resources }}"
- name: Write out to file per host
ansible.builtin.copy:
content: "{{ longhorn_volumes_map | to_nice_json }}"
dest: "{{ longhorn_volume_map_local_dir }}/longhorn_volumes_map_{{ inventory_hostname }}.json"
mode: '0644'
delegate_to: localhost
when: longhorn_volumes_map is defined
- name: Create snapshot for every volume
become: true
kubernetes.core.k8s:
kubeconfig: "{{ kubeconfig_path }}"
state: present
definition:
apiVersion: longhorn.io/v1beta2
kind: Snapshot
metadata:
name: "{{ item.backupName }}"
namespace: longhorn-system
spec:
createSnapshot: true
labels: null
volume: "{{ item.volumeName }}"
loop: "{{ longhorn_volumes_map }}"
when: longhorn_volumes_map is defined
- name: Create backup for every snapshot
become: true
kubernetes.core.k8s:
kubeconfig: "{{ kubeconfig_path }}"
state: present
definition:
apiVersion: longhorn.io/v1beta2
kind: Backup
metadata:
labels:
backup-volume: "{{ item.volumeName }}"
name: "{{ item.backupName }}"
namespace: longhorn-system
spec:
snapshotName: "{{ item.backupName }}"
syncRequestedAt: '0000-01-01T00:00:00Z'
loop: "{{ longhorn_volumes_map }}"
when: longhorn_volumes_map is defined
- name: Wait for backup to have status.state completed
become: true
kubernetes.core.k8s_info:
kubeconfig: "{{ kubeconfig_path }}"
kind: Backup
api_version: longhorn.io/v1beta2
namespace: longhorn-system
name: "{{ item.backupName }}"
loop: "{{ longhorn_volumes_map }}"
register: longhorn_backup
until: longhorn_backup.resources[0].status.state == 'Completed'
retries: 180
delay: 10
when: longhorn_volumes_map is defined
...