-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
provision-proxmox-lxc.yml
386 lines (343 loc) · 14.6 KB
/
provision-proxmox-lxc.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
---
- name: Preliminary System Checks
hosts: all
gather_facts: no
tasks:
- name: Verify if target system is Proxmox VE
ansible.builtin.script: '../../PCSMenu/src/Functions/Proxmox/Scripts/checkProxmox.sh'
register: proxmox_check
ignore_errors: yes
- name: Halt if any target system is not Proxmox VE
ansible.builtin.fail:
msg: 'One or more targeted systems are not Proxmox VE. Process halted.'
when: proxmox_check is failed
- name: Proxmox VE Node Preparation
hosts: all
become: yes
gather_facts: yes
tasks:
- name: Update and Refresh apt cache
ansible.builtin.apt:
update_cache: yes
cache_valid_time: 3600
- name: Ensure Python3 and pip are Installed
ansible.builtin.apt:
name:
- python3
- python3-pip
state: present
- name: Install Proxmox API Utilities
ansible.builtin.apt:
name:
- python3-proxmoxer
- python3-requests
state: present
- name: Debian 12 Template Verification and Installation
hosts: all
become: yes
vars_files:
- /opt/Wolflith/Temp/lxc_provisioning_vars.yml
tasks:
- name: Check for Debian 12 Template Presence and Install if Missing
community.general.proxmox_template:
node: '{{ node }}'
api_host: '{{ api_host }}'
api_user: '{{ api_user }}'
api_password: '{{ api_password }}'
storage: '{{ ct_storage }}'
template: 'debian-12-standard_12.2-1_amd64.tar.zst'
state: present
- name: LXC Container Provisioning
hosts: all
become: yes
vars_files:
- /opt/Wolflith/Temp/lxc_provisioning_vars.yml
tasks:
- name: Provision LXC Container with Debian 12 Template
community.general.proxmox:
api_user: '{{ api_user }}'
api_password: '{{ api_password }}'
api_host: '{{ api_host }}'
node: '{{ node }}'
hostname: '{{ hostname }}'
ostemplate: '{{ ct_storage }}:vztmpl/debian-12-standard_12.2-1_amd64.tar.zst'
password: '{{ lxc_root_password }}'
cores: '{{ vCPU }}'
memory: '{{ RAM }}'
swap: '{{ RAM }}'
disk: '{{ lxc_storage_name }}:{{ lxc_storage_amount }}'
netif: '{{ netif }}'
onboot: true
unprivileged: true
state: present
features:
- keyctl=1
- nesting=1
delegate_to: localhost
register: provisioning_output
- name: Extract and Set VMID from Provisioning Output
set_fact:
vmid: "{{ provisioning_output.msg | regex_search('VM (\\d+) from template', '\\1') | first }}"
delegate_to: localhost
- name: Update VMID in Local Configuration File
ansible.builtin.lineinfile:
path: /opt/Wolflith/Temp/lxc_provisioning_vars.yml
regexp: '^vmid:.*$'
line: 'vmid: {{ vmid }}'
backrefs: yes
create: yes
delegate_to: localhost
- name: Check for CIFS Share Setup Requirements
hosts: all
become: yes
vars_files:
- /opt/Wolflith/Temp/lxc_provisioning_vars.yml
tasks:
- name: Set fact for CIFS setup based on cifs_share_details presence
set_fact:
cifs_setup_required: "{{ 'cifs_share_details' in lookup('file', '/opt/Wolflith/Temp/lxc_provisioning_vars.yml') | from_yaml }}"
ignore_errors: yes
- name: CIFS Share Setup for LXC Container
hosts: all
become: yes
vars_files:
- /opt/Wolflith/Temp/provisioning_docker_service_vars.yml
- /opt/Wolflith/Temp/lxc_provisioning_vars.yml
tasks:
- name: Configure LXC and PVE for CIFS share
block:
- name: Start LXC Container
community.general.proxmox:
api_user: '{{ api_user }}'
api_password: '{{ api_password }}'
api_host: '{{ api_host }}'
node: '{{ node }}'
vmid: '{{ vmid }}'
unprivileged: true
state: started
- name: Install sudo and add a new user with sudo privileges in the LXC Container
block:
- name: Ensure sudo is installed in the LXC Container
ansible.builtin.shell: |
pct exec {{ vmid }} -- apt-get update &&
pct exec {{ vmid }} -- apt-get install -y sudo
- name: Create a new user with specified username and add to the sudo group
ansible.builtin.shell: |
pct exec {{ vmid }} -- useradd -m {{ selected_service }} -s /bin/bash &&
pct exec {{ vmid }} -- bash -c "echo '{{ selected_service }}:{{ lxc_user_password }}' | chpasswd" &&
pct exec {{ vmid }} -- usermod -aG sudo {{ selected_service }}
ignore_errors: yes
- name: Creating group 'lxc_shares' with GID=110000 in LXC
ansible.builtin.shell: |
pct exec {{ vmid }} -- groupadd -g 110000 lxc_shares
ignore_errors: yes
- name: Adding user to group 'lxc_shares' in LXC
ansible.builtin.shell: |
pct exec {{ vmid }} -- usermod -aG lxc_shares {{ cifs_share_details.lxc_username }}
- name: Ensure CIFS utilities are installed on the Proxmox host
ansible.builtin.package:
name: cifs-utils
state: present
- name: Create mount point on PVE host
ansible.builtin.file:
path: '/mnt/lxc_shares/{{ cifs_share_details.folder_name }}'
state: directory
- name: Transfer the addCifsShare script to the target host
ansible.builtin.copy:
src: '../static/addCifsShare.sh'
dest: '/tmp/addCifsShare.sh'
mode: '0755'
- name: Execute the addCifsShare script
ansible.builtin.shell:
cmd: >
/tmp/addCifsShare.sh "{{ cifs_share_details.folder_name }}"
"{{ cifs_share_details.cifs_host }}"
"{{ cifs_share_details.share_name }}"
"{{ cifs_share_details.smb_username }}"
"{{ cifs_share_details.smb_password }}"
ignore_errors: true
- name: Remove the temporary addCifsShare script
ansible.builtin.file:
path: '/tmp/addCifsShare.sh'
state: absent
- name: Check if CIFS share line exists in LXC config
ansible.builtin.shell:
cmd: "grep -q 'mp0: /mnt/lxc_shares/{{ cifs_share_details.folder_name }}' /etc/pve/lxc/{{ vmid }}.conf"
register: grep_lxc_config
ignore_errors: true
changed_when: false
- name: Add a bind mount of the share to the LXC config if not already present
ansible.builtin.lineinfile:
path: '/etc/pve/lxc/{{ vmid }}.conf'
line: 'mp0: /mnt/lxc_shares/{{ cifs_share_details.folder_name }},mp=/mnt/{{ cifs_share_details.folder_name }},shared=1'
create: yes
when: grep_lxc_config.rc != 0
when: cifs_setup_required
- name: Docker Setup in Provisioned LXC Container
hosts: all
become: yes
vars_files:
- /opt/Wolflith/Temp/lxc_provisioning_vars.yml
tasks:
- name: Start LXC Container Before Docker Setup
community.general.proxmox:
api_user: '{{ api_user }}'
api_password: '{{ api_password }}'
api_host: '{{ api_host }}'
node: '{{ node }}'
vmid: '{{ vmid }}'
unprivileged: true
state: started
- name: Update & Upgrade Packages inside LXC Container
ansible.builtin.shell: |
pct exec {{ vmid }} -- apt-get update &&
pct exec {{ vmid }} -- apt-get upgrade -y
- name: Install Docker and Dependencies in LXC Container
ansible.builtin.shell: |
pct exec {{ vmid }} -- bash -c "\
export DEBIAN_FRONTEND=noninteractive && \
apt-get update && \
apt-get install -y ca-certificates curl gnupg lsb-release && \
OS_ID=\$(grep ^ID= /etc/os-release | cut -d'=' -f2 | tr -d '\"') && \
if [ \"\$OS_ID\" = 'ubuntu' ]; then REPO_DISTRO='ubuntu'; \
elif [ \"\$OS_ID\" = 'debian' ]; then REPO_DISTRO='debian'; \
elif [ \"\$OS_ID\" = 'raspbian' ]; then REPO_DISTRO='raspbian'; \
else echo 'Unsupported OS' && exit 1; fi && \
mkdir -p /etc/apt/keyrings && \
curl -fsSL https://download.docker.com/linux/\$REPO_DISTRO/gpg -o /etc/apt/keyrings/docker.asc && \
echo \"deb [arch=\$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/\$REPO_DISTRO \$(. /etc/os-release && echo \"\$VERSION_CODENAME\") stable\" | tee /etc/apt/sources.list.d/docker.list > /dev/null && \
apt-get update && \
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin"
- name: Reboot LXC Container Post Docker Installation
community.general.proxmox:
api_user: '{{ api_user }}'
api_password: '{{ api_password }}'
api_host: '{{ api_host }}'
node: '{{ node }}'
vmid: '{{ vmid }}'
unprivileged: true
state: restarted
- name: Final Configuration and Cleanup
hosts: all
become: yes
vars_files:
- /opt/Wolflith/Temp/provisioning_docker_service_vars.yml
- /opt/Wolflith/Temp/lxc_provisioning_vars.yml
tasks:
- name: Docker Network and Directory Setup
block:
- name: Create Docker Network in the LXC Container
ansible.builtin.shell: |
pct exec {{ vmid }} -- docker network create {{ network_name }}
ignore_errors: yes
- name: Ensure Host Destination Directory Exists
ansible.builtin.file:
path: '/tmp/docker/{{ selected_service }}'
state: directory
mode: '0755'
- name: Ensure LXC Container Destination Directories Exist
ansible.builtin.shell: |
pct exec {{ vmid }} -- mkdir -p /home/docker/{{ selected_service }}
pct exec {{ vmid }} -- mkdir -p /tmp
- name: Check if .env file exists for docker service
ansible.builtin.stat:
path: '{{ selected_service_path }}/.env'
register: env_file_stat
delegate_to: localhost
- name: Set flag for .env file existence
set_fact:
env_file_exists: '{{ env_file_stat.stat.exists }}'
delegate_to: localhost
- name: Transfer Scripts and Configuration Files to Proxmox Host
ansible.builtin.copy:
src: '{{ item.src }}'
dest: '{{ item.dest }}'
mode: '0755'
loop:
- {
src: '/opt/Wolflith/PCSMenu/src/Functions/Docker/Scripts/updateDockerComposeEnv.sh',
dest: '/tmp/updateDockerComposeEnv.sh',
}
- {
src: '/opt/Wolflith/Temp/provisioning_docker_service_vars.yml',
dest: '/tmp/provisioning_docker_service_vars.yml',
}
- {
src: '{{ selected_service_path }}/docker-compose.yml',
dest: '/tmp/docker/{{ selected_service }}/docker-compose.yml',
}
- name: Transfer .env file to Proxmox Host if it exists
ansible.builtin.copy:
src: '{{ selected_service_path }}/.env'
dest: '/tmp/docker/{{ selected_service }}/.env'
mode: '0755'
when: env_file_exists
- name: Transfer Config Folder to Proxmox Host
ansible.builtin.copy:
src: '{{ selected_service_path }}/config/'
dest: '/tmp/docker/{{ selected_service }}/config/'
remote_src: yes
mode: '0755'
recursive: yes
ignore_errors: yes
- name: Push Files into the LXC Container
ansible.builtin.shell: |
pct push {{ vmid }} {{ item.src }} {{ item.dest }}
loop:
- { src: '/tmp/updateDockerComposeEnv.sh', dest: '/tmp/updateDockerComposeEnv.sh' }
- {
src: '/tmp/provisioning_docker_service_vars.yml',
dest: '/tmp/provisioning_docker_service_vars.yml',
}
- {
src: '/tmp/docker/{{ selected_service }}/docker-compose.yml',
dest: '/home/docker/{{ selected_service }}/docker-compose.yml',
}
- name: Push .env file into the LXC Container if it exists
ansible.builtin.shell:
cmd: "pct push {{ vmid }} '/tmp/docker/{{ selected_service }}/.env' '/home/docker/{{ selected_service }}/.env'"
when: env_file_exists
- name: Find config files and directories to push
ansible.builtin.find:
paths: '/tmp/docker/{{ selected_service }}/config'
recurse: yes
register: config_files_and_dirs
ignore_errors: yes
- name: Ensure corresponding directories exist in the LXC Container
ansible.builtin.shell: |
pct exec {{ vmid }} -- mkdir -p /home/docker/{{ selected_service }}/config/{{ item.path | regex_replace('^/tmp/docker/{{ selected_service }}/config/','') | dirname }}
loop: '{{ config_files_and_dirs.files }}'
when: item.isdir
ignore_errors: yes
- name: Push Config files into the LXC Container
ansible.builtin.shell: |
pct push {{ vmid }} '{{ item.path }}' '/home/docker/{{ selected_service }}/config/{{ item.path | regex_replace('^/tmp/docker/{{ selected_service }}/config/','') }}'
loop: '{{ config_files_and_dirs.files }}'
when: item.isdir == false
ignore_errors: yes
- name: Script Execution and Docker Service Deployment
block:
- name: Set Execute Permission for the Script Inside LXC Container
ansible.builtin.shell: |
pct exec {{ vmid }} -- chmod +x /tmp/updateDockerComposeEnv.sh
- name: Execute Script Inside LXC to update Docker Compose file
ansible.builtin.shell: |
pct exec {{ vmid }} -- bash -c "cd /tmp && ./updateDockerComposeEnv.sh {{ selected_service }}"
- name: Deploy Docker Compose Service Inside LXC Container
ansible.builtin.shell: |
pct exec {{ vmid }} -- bash -c "cd /home/docker/{{ selected_service }} && docker compose up -d"
- name: Cleanup Temporary Files and Directories
block:
- name: Remove Temporary Files from Proxmox Host
ansible.builtin.file:
path: '{{ item }}'
state: absent
loop:
- '/tmp/updateDockerComposeEnv.sh'
- '/tmp/docker'
- name: Remove Temporary Files from LXC Container
ansible.builtin.shell: |
pct exec {{ vmid }} -- rm -f /tmp/{{ item }}
loop:
- 'updateDockerComposeEnv.sh'
- 'provisioning_docker_service_vars.yml'