blob: f50562a221c04ae8be826b51a84d773abc469643 (
plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
# Host-side setup (runs on the jail host via SSH)
- name: "Check if {{ jail_name }} container exists"
shell: "zfs list -o name | grep -Fxq 'zroot/jails/containers/{{ jail_name }}'"
failed_when: false
changed_when: false
register: jail_exists
delegate_to: "{{ jail_delegate_host }}"
- name: "Check {{ jail_name }} userland version"
shell: "zfs get -H -o value origin zroot/jails/containers/{{ jail_name }}"
register: jail_origin
changed_when: false
when: jail_exists.rc == 0
delegate_to: "{{ jail_delegate_host }}"
# ZFS volume migration
- name: "Check ZFS volume state for {{ jail_name }}"
shell: |
if zfs list {{ item.name }} >/dev/null 2>&1; then
echo "exists"
elif [ -d "/usr/local/jails/containers/{{ jail_name }}{{ item.mountpoint }}" ] && \
[ "$(ls -A /usr/local/jails/containers/{{ jail_name }}{{ item.mountpoint }})" ]; then
echo "migrate"
else
echo "empty"
fi
loop: "{{ zfs | default([]) }}"
loop_control:
label: "{{ item.name }}"
register: _zfs_volume_state
changed_when: false
delegate_to: "{{ jail_delegate_host }}"
- name: "*** VOLUME MIGRATION REQUIRED: {{ jail_name }} ***"
pause:
prompt: |
════════════════════════════════════════════════════════════════
VOLUME MIGRATION: {{ jail_name }}
════════════════════════════════════════════════════════════════
The following paths contain data that will be migrated
to new ZFS volumes:
{% for result in _zfs_volume_state.results | default([]) if result.stdout == 'migrate' %}
- {{ result.item.mountpoint }} → {{ result.item.name }}
{% endfor %}
This will:
1. Create ZFS volumes (staged)
2. Stop the jail
3. Copy existing data into the volumes
════════════════════════════════════════════════════════════════
Press Enter to continue or Ctrl+C to abort
when: (_zfs_volume_state.results | default([]) | selectattr('stdout', 'equalto', 'migrate') | list | length) > 0
- name: "Create new ZFS volumes (staged) for {{ jail_name }}"
community.general.zfs:
name: "{{ item.item.name }}"
state: present
extra_zfs_properties:
mountpoint: "/usr/local/jails/volumes/.staging/{{ jail_name }}{{ item.item.mountpoint }}"
loop: "{{ _zfs_volume_state.results | default([]) }}"
loop_control:
label: "{{ item.item.name }}"
when: item.stdout == "migrate"
delegate_to: "{{ jail_delegate_host }}"
- name: "Create staging mount points for {{ jail_name }}"
file:
path: "/usr/local/jails/volumes/.staging/{{ jail_name }}{{ item.item.mountpoint }}"
state: directory
loop: "{{ _zfs_volume_state.results | default([]) }}"
loop_control:
label: "{{ item.item.name }}"
when: item.stdout == "migrate"
delegate_to: "{{ jail_delegate_host }}"
- name: "Mount staged volumes for {{ jail_name }}"
shell: "zfs mount {{ item.item.name }} || true"
loop: "{{ _zfs_volume_state.results | default([]) }}"
loop_control:
label: "{{ item.item.name }}"
when: item.stdout == "migrate"
changed_when: false
delegate_to: "{{ jail_delegate_host }}"
- name: "Stop {{ jail_name }} jail for volume migration"
shell: "service jail stop {{ jail_name }} || true"
when: (_zfs_volume_state.results | default([]) | selectattr('stdout', 'equalto', 'migrate') | list | length) > 0
delegate_to: "{{ jail_delegate_host }}"
- name: "Copy data into staged volumes for {{ jail_name }}"
shell: |
cp -a "/usr/local/jails/containers/{{ jail_name }}{{ item.item.mountpoint }}/." \
"/usr/local/jails/volumes/.staging/{{ jail_name }}{{ item.item.mountpoint }}/"
loop: "{{ _zfs_volume_state.results | default([]) }}"
loop_control:
label: "{{ item.item.name }}"
when: item.stdout == "migrate"
delegate_to: "{{ jail_delegate_host }}"
- name: "Set final mountpoint on migrated volumes for {{ jail_name }}"
shell: |
zfs unmount {{ item.item.name }} || true
zfs set mountpoint="/usr/local/jails/containers/{{ jail_name }}{{ item.item.mountpoint }}" {{ item.item.name }}
loop: "{{ _zfs_volume_state.results | default([]) }}"
loop_control:
label: "{{ item.item.name }}"
when: item.stdout == "migrate"
delegate_to: "{{ jail_delegate_host }}"
# Userland migration
- name: "*** USERLAND MIGRATION REQUIRED: {{ jail_name }} ***"
pause:
prompt: |
════════════════════════════════════════════════════════════════
JAIL USERLAND MIGRATION: {{ jail_name }}
════════════════════════════════════════════════════════════════
Current: {{ jail_origin.stdout | trim }}
Target: zroot/jails/templates/{{ userland }}@base
This will:
1. Stop the jail
2. Rename existing dataset to *.old.<timestamp>
3. Clone fresh from {{ userland }}
════════════════════════════════════════════════════════════════
Press Enter to continue or Ctrl+C to abort
when:
- jail_exists.rc == 0
- jail_origin.stdout is defined
- "userland + '@base' not in jail_origin.stdout"
- name: "Stop {{ jail_name }} jail for userland migration"
shell: "service jail stop {{ jail_name }} || true"
when:
- jail_exists.rc == 0
- jail_origin.stdout is defined
- "userland + '@base' not in jail_origin.stdout"
delegate_to: "{{ jail_delegate_host }}"
- name: "Unmount any remaining mounts inside {{ jail_name }} container"
shell: |
mount | awk '$3 ~ "^/usr/local/jails/containers/{{ jail_name }}/" {print $3}' | sort -r | xargs -I{} umount -f {} 2>/dev/null || true
when:
- jail_exists.rc == 0
- jail_origin.stdout is defined
- "userland + '@base' not in jail_origin.stdout"
delegate_to: "{{ jail_delegate_host }}"
- name: "Migrate {{ jail_name }} to {{ userland }}"
shell: |
zfs rename zroot/jails/containers/{{ jail_name }} zroot/jails/containers/{{ jail_name }}.old.$(date +%s)
when:
- jail_exists.rc == 0
- jail_origin.stdout is defined
- "userland + '@base' not in jail_origin.stdout"
delegate_to: "{{ jail_delegate_host }}"
- name: "Clone {{ jail_name }} from template"
shell: "zfs clone zroot/jails/templates/{{ userland }}@base zroot/jails/containers/{{ jail_name }}"
when: jail_exists.rc != 0 or (jail_origin.stdout is defined and userland + '@base' not in jail_origin.stdout)
delegate_to: "{{ jail_delegate_host }}"
# ZFS volume setup (for all volumes — existing, migrated, and new)
- name: "Ensure ZFS volumes exist for {{ jail_name }}"
community.general.zfs:
name: "{{ item.name }}"
state: present
extra_zfs_properties:
mountpoint: "/usr/local/jails/containers/{{ jail_name }}{{ item.mountpoint }}"
loop: "{{ zfs | default([]) }}"
loop_control:
label: "{{ item.name }}"
delegate_to: "{{ jail_delegate_host }}"
- name: "Create mount points for ZFS volumes in {{ jail_name }}"
file:
path: "/usr/local/jails/containers/{{ jail_name }}{{ item.mountpoint }}"
state: directory
owner: "{{ item.owner | default('root') }}"
group: "{{ item.group | default('wheel') }}"
mode: "{{ item.mode | default('0755') }}"
loop: "{{ zfs | default([]) }}"
loop_control:
label: "{{ item.mountpoint }}"
delegate_to: "{{ jail_delegate_host }}"
- name: "Mount ZFS volumes for {{ jail_name }}"
shell: "zfs mount {{ item.name }} || true"
loop: "{{ zfs | default([]) }}"
loop_control:
label: "{{ item.name }}"
changed_when: false
delegate_to: "{{ jail_delegate_host }}"
# Container setup
- name: "Create directories for {{ jail_name }}"
file:
path: "/usr/local/jails/containers/{{ jail_name }}{{ item }}"
state: directory
owner: root
group: wheel
mode: "0755"
loop: "{{ dirs | default([]) }}"
delegate_to: "{{ jail_delegate_host }}"
- name: "Create mount point sources for {{ jail_name }}"
file:
path: "{{ item.src }}"
state: directory
loop: "{{ nullfs | default([]) }}"
loop_control:
label: "{{ item.src }}"
delegate_to: "{{ jail_delegate_host }}"
- name: "Create mount point destinations for {{ jail_name }}"
file:
path: "/usr/local/jails/containers/{{ jail_name }}{{ item.dst }}"
state: directory
owner: root
group: wheel
mode: "0755"
loop: "{{ nullfs | default([]) }}"
loop_control:
label: "{{ item.dst }}"
delegate_to: "{{ jail_delegate_host }}"
- name: "Deploy jail.conf.d/{{ jail_name }}.conf"
template:
src: jail_conf.j2
dest: "/etc/jail.conf.d/{{ jail_name }}.conf"
owner: root
group: wheel
mode: "0644"
vars:
jail:
name: "{{ jail_name }}"
num: "{{ jail_num }}"
ip: "{{ jail_lan_cidr | ipv4_nth_cidr(jail_num | int + jail_lan_offset | int) }}"
devfs_ruleset: "{{ devfs_ruleset | default(4) }}"
options: "{{ jail_conf_options | default([]) }}"
default_route: "{{ not no_default_route | default(false) }}"
exec_prestart: "{{ exec_prestart | default([]) }}"
exec_start: "{{ exec_start | default([]) }}"
exec_poststart: "{{ exec_poststart | default([]) }}"
exec_prestop: "{{ exec_prestop | default([]) }}"
exec_stop: "{{ exec_stop | default([]) }}"
exec_poststop: "{{ exec_poststop | default([]) }}"
mounts: "{{ nullfs | default([]) }}"
delegate_to: "{{ jail_delegate_host }}"
- name: "Start {{ jail_name }} jail"
shell: "service jail start {{ jail_name }}"
register: jail_start
failed_when: "jail_start.rc != 0 and 'already exists' not in jail_start.stdout"
changed_when: "'already exists' not in jail_start.stdout"
delegate_to: "{{ jail_delegate_host }}"
# In-jail provisioning
- name: Create pkg repos directory
file:
path: /usr/local/etc/pkg/repos
state: directory
owner: root
group: wheel
mode: "0755"
- name: Configure poudriere repo in jail
copy:
content: |
poudriere: {
url: "http://{{ hostvars[jail_delegate_host]['poudriere_repo_ip'] }}/packages",
enabled: no,
priority: 100
}
dest: /usr/local/etc/pkg/repos/poudriere.conf
owner: root
group: wheel
mode: "0644"
- name: Install packages
shell: "pkg install -y {{ pkg | join(' ') }}"
environment:
ASSUME_ALWAYS_YES: "yes"
when: pkg is defined and pkg | length > 0
register: pkg_result
changed_when: "'Number of packages to be installed' in pkg_result.stdout"
- name: Install packages from poudriere
shell: "pkg install -y -r poudriere {{ pkg_custom | join(' ') }}"
when: pkg_custom is defined and pkg_custom | length > 0
register: pkg_custom_result
changed_when: "'Installing' in pkg_custom_result.stdout"
- name: Create parent directories for files
file:
path: "{{ item.dest | dirname }}"
state: directory
owner: root
group: wheel
mode: "0755"
loop: "{{ files | default([]) }}"
loop_control:
label: "{{ item.dest | dirname }}"
when: files is defined
- name: Deploy files
template:
src: "{{ jail_role_dir }}/templates/{{ item.src }}"
dest: "{{ item.dest }}"
owner: "{{ item.owner | default('root') }}"
group: "{{ item.group | default('wheel') }}"
mode: "{{ item.mode | default('0644') }}"
loop: "{{ files | default([]) }}"
loop_control:
label: "{{ item.dest }}"
when: files is defined
notify: Restart jail services
- name: Enable services
community.general.sysrc:
name: "{{ item }}_enable"
value: "YES"
loop: "{{ services | default([]) }}"
- name: Set sysrc values
community.general.sysrc:
name: "{{ item.name }}"
value: "{{ item.value }}"
loop: "{{ sysrc | default([]) }}"
when: sysrc is defined
- name: Set sysctl values
sysctl:
name: "{{ item.name }}"
value: "{{ item.value }}"
state: present
loop: "{{ sysctl | default([]) }}"
when: sysctl is defined
|