-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxmox_create_vm_template_20_04.sls
110 lines (86 loc) · 3 KB
/
proxmox_create_vm_template_20_04.sls
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
# get ubuntu 20.04 cloud image
{% if opts.id == 'p20' %}
{% set vm_template_id = '9003' %}
{% elif opts.id == 'p21' %}
{% set vm_template_id = '9004' %}
{% endif %}
{% set release_name = "focal" %}
{% set image_name = "ubuntu-20.04-server-cloudimg-amd64.img" %}
{% set zfs_dataset = salt['pillar.get']('pve_vars:zfs:zfs1') %}
# Only run the following if the template doesn't already exist
# Check for the template by see if the zfs output contains "base"
# ONLY WORKS WITH CMD.SHELL (PIPES ONLY WORK IN PYTHON SHELL)
{% set template_exists = salt['cmd.shell']('zfs list | grep base-'+vm_template_id+'-disk-0')%}
{% if 'zpool1/proxmox_vm/base-'+vm_template_id+'-disk-0' not in template_exists %}
get_ubuntu_18_04:
cmd.run:
- name: |
curl -L https://cloud-images.ubuntu.com/releases/{{release_name}}/release/{{image_name}} -o /var/lib/vz/template/iso/{{image_name}}
- shell: /bin/bash
- timeout: 300
- creates: /var/lib/vz/template/iso/{{image_name}}
create_instance:
cmd.run:
- name: |
qm create {{ vm_template_id }} -name ubuntu-cloudinit-{{ vm_template_id }} -memory 1024 -net0 virtio,bridge=lxdbr0 -cores 1 -sockets 1
# Only run the import image state if the result of zfs list is empty, otherwise it will keep adding new images incrementing each time
# ONLY WORKS WITH CMD.SHELL (PIPES ONLY WORK IN PYTHON SHELL)
{% set virtio_exists = salt['cmd.shell']('zfs list | grep vm-'+vm_template_id+'-disk-0')%}
{% if 'zpool1/proxmox_vm/vm-'+vm_template_id+'-disk-0' not in virtio_exists %}
import_image:
cmd.run:
- name: |
qm importdisk {{ vm_template_id }} /var/lib/vz/template/iso/{{image_name}} {{zfs_dataset.name}}
{% endif %}
attach_disk:
cmd.run:
- name: |
qm set {{ vm_template_id }} -scsihw virtio-scsi-pci -virtio0 {{zfs_dataset.name}}:vm-{{ vm_template_id }}-disk-0
# set ci-custom for cloud init
ci_custom_tweaks:
cmd.run:
- name: |
qm set {{ vm_template_id }} --cicustom "user=local:snippets/user-data-cicustom.yaml,network=local:snippets/network-data-cicustom.yaml"
attach_serial:
cmd.run:
- name: |
qm set {{ vm_template_id }} -serial0 socket
set_bootdisk:
cmd.run:
- name: |
qm set {{ vm_template_id }} -boot c -bootdisk virtio0
set_vm_startup_delay_timer:
cmd.run:
- name: |
qm set {{ vm_template_id }} --startup up=300
set_qemu_agent:
cmd.run:
- name: |
qm set {{ vm_template_id }} -agent 1
set_hotplug:
cmd.run:
- name: |
qm set {{ vm_template_id }} -hotplug disk,network,usb
set_cpus:
cmd.run:
- name: |
qm set {{ vm_template_id }} -vcpus 1
add_vga:
cmd.run:
- name: |
qm set {{ vm_template_id }} -vga qxl
set_cloudinit_drive:
cmd.run:
- name: |
qm set {{ vm_template_id }} -ide2 {{zfs_dataset.name}}:cloudinit
# 8GB
resize_drive:
cmd.run:
- name: |
qm resize {{ vm_template_id }} virtio0 8G
# Convert the VM to the template
template_convert:
cmd.run:
- name: |
qm template {{ vm_template_id }}
{% endif %}