-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathready-external-device.yaml
182 lines (152 loc) · 5.65 KB
/
ready-external-device.yaml
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
---
- name: Configure and format NVMe drive
hosts: localhost
gather_facts: no
vars_files:
- vars.yaml
tasks:
- name: Ensure required packages are installed (Debian/Ubuntu)
become: yes
apt:
name:
- restic
- zfsutils-linux
state: present
- name: Check if serial_number variable is provided
fail:
msg: "The serial_number variable is required. Set in vars.yaml or pass as an argument, e.g., -e 'serial_number=12345'"
when: serial_number == ""
# -----------------------------------------------------------------------
# Section: Get device from provided serial number
# -----------------------------------------------------------------------
- name: List all block devices with their serial numbers
command: lsblk -o NAME,SERIAL
register: all_devices
- name: Find the device with the matching serial number
shell: |
lsblk -d -o NAME,SERIAL | grep {{ serial_number }} | awk '{print $1}'
register: matching_device
changed_when: false
- name: Check if matching device was found
fail:
msg: "No device found with the serial number {{ serial_number }}"
when: matching_device.stdout == ""
# -----------------------------------------------------------------------
# Section: Show device info and confirm with user
# -----------------------------------------------------------------------
- name: Get detailed information about the matching device using lsblk
command: lsblk -o NAME,MODEL,SIZE,VENDOR,SERIAL /dev/{{ matching_device.stdout | trim }}
register: device_details_lsblk
changed_when: false
- name: Get detailed information about the matching device using udevadm
command: udevadm info --query=all --name=/dev/{{ matching_device.stdout | trim }}
register: device_details_udevadm
changed_when: false
- name: Display matching device details from lsblk
debug:
msg: "Device details (lsblk):\n{{ device_details_lsblk.stdout }}"
- name: Display matching device details from udevadm
debug:
msg: "Device details (udevadm):\n{{ device_details_udevadm.stdout }}"
- name: Confirm matching device with user
pause:
prompt: |
Is this the correct device?
Device details (lsblk):
{{ device_details_lsblk.stdout }}
Device details (udevadm):
{{ device_details_udevadm.stdout }}
(y/n)
register: user_confirmation
- name: Fail if user does not confirm the device
fail:
msg: "Operation aborted by user."
when: user_confirmation.user_input != "y"
- name: Set device variable
set_fact:
device: "/dev/{{ matching_device.stdout | trim }}"
# -----------------------------------------------------------------------
# Section: Create ZFS pool and dataset on device
# -----------------------------------------------------------------------
- name: Unmount device if it is mounted
become: yes
shell: |
umount {{ device }}* 2>/dev/null || true
ignore_errors: yes
- name: Set zpool name based on serial number
set_fact:
zpool_name: "zpool_{{ serial_number }}"
- name: Check if ZFS pool exists
become: yes
command: zpool list {{ zpool_name }}
register: zpool_check
ignore_errors: true
changed_when: false
- name: Create ZFS pool
become: yes
command: zpool create -f {{ zpool_name }} {{ device }}
when: zpool_check.rc != 0
- name: Check if ZFS dataset exists
become: yes
command: zfs list {{ zpool_name }}/{{ dataset_name }}
register: zfs_dataset_check
ignore_errors: true
changed_when: false
- name: Create ZFS dataset
become: yes
command: zfs create {{ zpool_name }}/{{ dataset_name }}
when: zfs_dataset_check.rc != 0
- name: Set ZFS dataset properties
become: yes
command: >
zfs set
atime=off
compression=on
checksum=sha256
dedup=off
copies=2
{{ zpool_name }}/{{ dataset_name }}
- name: Display ZFS pool details
become: yes
command: zpool get all {{ zpool_name }}
register: zpool_details
- name: Display ZFS pool status
become: yes
command: zpool status {{ zpool_name }}
register: zpool_status
- name: Display ZFS dataset properties
become: yes
command: zfs get all {{ zpool_name }}/{{ dataset_name }}
register: zfs_properties
- name: Show ZFS pool details
debug:
var: zpool_details.stdout_lines
- name: Show ZFS pool status
debug:
var: zpool_status.stdout_lines
- name: Show ZFS dataset properties
debug:
var: zfs_properties.stdout_lines
# -----------------------------------------------------------------------
# Section: Create restic repo
# -----------------------------------------------------------------------
- name: Ensure Restic password file exists and is readable
become: yes
file:
path: "{{ restic_password_file }}"
state: file
mode: '0600'
- name: Validate Restic password file content
become: yes
command: cat {{ restic_password_file }}
register: password_file_content
- name: Fail if password file is empty
fail:
msg: "Restic password file is empty or unreadable!"
when: password_file_content.stdout == ""
- name: Initialize Restic repository
become: yes
command: >
restic -r {{ restic_repo }} --password-file {{ restic_password_file }} init
args:
creates: "{{ restic_repo }}/config"