-
Notifications
You must be signed in to change notification settings - Fork 17
/
provision-template.yml
82 lines (68 loc) · 2.84 KB
/
provision-template.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
# provision-template.yml
# ansible-playbook -i inventory.yml --ask-vault-pass provision-template.yml
# Reference https://pve.proxmox.com/pve-docs/qm.1.html for qm commands & config
---
- hosts: proxmox
gather_facts: false
vars_files:
- vars.yml
tasks:
# Get next available Proxmox VE VMID, semi-random, idempotent, seed from hostname & template_name
- name: Get available VMID
vars:
var_concat_seed: "{{inventory_hostname}}-{{template_name}}"
rand_vmid: "{{9999 | random(seed=var_concat_seed)}}"
command: pvesh get /cluster/nextid -vmid {{rand_vmid}}
register: next_vmid
failed_when: next_vmid.rc != 0
# Create temp directory
- name: Create temp directory
file:
path: /tmp/{{template_name}}
state: directory
# Template out autounattend answer file
- name: Template out autounattend answer file
template:
src: ./autounattend.xml.tpl
dest: /tmp/{{template_name}}/autounattend.xml
# Copy drivers & scripts for ISO
- name: Copy files for ISO
copy:
src: iso-files/
dest: /tmp/{{template_name}}/
# Create ISO with drivers, scripts, & answer file
- name: Create ISO file
become: true
command: "genisoimage -J -r -o /var/lib/vz/template/iso/{{template_name}}Provision.iso /tmp/{{template_name}}/"
register: iso_created
# Create VM
- name: Create VM
command: "qm create {{next_vmid.stdout}} --name {{template_name}} --sockets {{vm_sockets}} --cores {{vm_cores}} --memory {{vm_memory_mb}} --ide2 file={{os_iso_location}},media=cdrom --ide3 file=local:iso/{{template_name}}Provision.iso,media=cdrom --net0 model=virtio,bridge=vmbr0,firewall=1 --scsihw virtio-scsi-pci --scsi0 {{pve_storage_id}}:{{drive_size_gb}},format={{format}} --ostype {{vm_os_type}} --agent {{agent}}"
# Start VM
- name: Start VM
command: "qm start {{next_vmid.stdout}}"
# Wait for VM build
- name: Wait for OS install, config, update, sysprep. Up to 120 min. Monitor status if possible.
command: "qm wait {{next_vmid.stdout}}"
async: 7200
poll: 30
# Remove CD drives
- name: Remove IDE CD Drives
command: "qm set {{next_vmid.stdout}} --delete ide2 --delete ide3"
# Create template from VM
- name: Create template
command: "qm template {{next_vmid.stdout}}"
# Remove ISO
- name: Remove Unattend ISO file
file:
path: /var/lib/vz/template/iso/{{template_name}}Provision.iso
state: absent
# Cleanup temp files
- name: Cleanup temp files used for ISO build
file:
path: /tmp/{{template_name}}
state: absent
when: iso_created.changed
- name: Done
debug:
msg: Template build {{template_name}}, with VMID {{next_vmid.stdout}} complete.