Skip to content

Commit ee6e38b

Browse files
committedAug 1, 2024
working packer template with terraform clones
1 parent 81c3941 commit ee6e38b

File tree

3 files changed

+83
-16
lines changed

3 files changed

+83
-16
lines changed
 

‎packer/ubuntu-noble/ubuntu-jammy.pkr.hcl

+12-12
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ source "proxmox-clone" "ubuntu-server-jammy" {
3030
insecure_skip_tls_verify = true
3131

3232
# VM General Settings
33-
node = "prox"
33+
node = "pve2"
3434
clone_vm_id = "8101"
3535
vm_id = "9636"
3636
vm_name = "ubuntu-server-jammy"
@@ -45,7 +45,7 @@ source "proxmox-clone" "ubuntu-server-jammy" {
4545
disks {
4646
disk_size = "5G"
4747
format = "raw"
48-
storage_pool = "Fast2Tb"
48+
storage_pool = "Fast500Gb"
4949
type = "virtio"
5050
}
5151

@@ -61,23 +61,20 @@ source "proxmox-clone" "ubuntu-server-jammy" {
6161
model = "virtio"
6262
bridge = "vmbr0"
6363
firewall = "false"
64-
}
65-
66-
# VM Cloud-Init Settings
67-
cloud_init = true
68-
cloud_init_storage_pool = "local-lvm"
64+
}
65+
6966

7067
ssh_username = "mafyuh"
7168
ssh_private_key_file = "~/.ssh/id_rsa"
7269
}
7370

74-
# Build Definition to create the VM Template
71+
7572
build {
7673

7774
name = "ubuntu-server-jammy"
7875
sources = ["source.proxmox-clone.ubuntu-server-jammy"]
7976

80-
# Provisioning the VM Template for Cloud-Init Integration in Proxmox #1
77+
8178
provisioner "shell" {
8279
inline = [
8380
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
@@ -93,13 +90,13 @@ build {
9390
]
9491
}
9592

96-
# Provisioning the VM Template for Cloud-Init Integration in Proxmox #2
93+
9794
provisioner "file" {
98-
source = "ubuntu-noble/files/pve.cfg"
95+
source = "files/pve.cfg"
9996
destination = "/tmp/pve.cfg"
10097
}
10198

102-
# Provisioning the VM Template for Cloud-Init Integration in Proxmox #3
99+
103100
provisioner "shell" {
104101
inline = [ "sudo cp /tmp/pve.cfg /etc/cloud/cloud.cfg.d/pve.cfg" ]
105102
}
@@ -110,6 +107,9 @@ build {
110107
"sudo apt-get install -y ca-certificates curl gnupg lsb-release nfs-common qemu-guest-agent net-tools",
111108
"curl -fsSL https://get.docker.com | sudo sh",
112109
"echo \"alias dcu='docker compose up -d'\" >> ~/.bashrc",
110+
"echo \"alias dcd='docker compose down'\" >> ~/.bashrc",
111+
"git config --global user.name \"Mafyuh\"",
112+
"git config --global user.email \"matt@mafyuh.com\"",
113113
"sudo apt-get -y update"
114114
]
115115
}

‎terraform/cloud-init.tf

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ data "local_file" "ssh_public_key" {
22
filename = "/home/mafyuh/.ssh/main_key.pub"
33
}
44

5+
data "local_file" "ssh_public_key_2" {
6+
filename = "/home/mafyuh/.ssh/id_rsa.pub"
7+
}
8+
59
resource "proxmox_virtual_environment_file" "cloud_config" {
610
content_type = "snippets"
711
datastore_id = "Slow4tb"
@@ -19,6 +23,7 @@ resource "proxmox_virtual_environment_file" "cloud_config" {
1923
shell: /bin/bash
2024
ssh_authorized_keys:
2125
- ${trimspace(data.local_file.ssh_public_key.content)}
26+
- ${trimspace(data.local_file.ssh_public_key_2.content)}
2227
sudo: ALL=(ALL) NOPASSWD:ALL
2328
runcmd:
2429
- apt update
@@ -54,17 +59,16 @@ resource "proxmox_virtual_environment_file" "cloud_config2" {
5459
shell: /bin/bash
5560
ssh_authorized_keys:
5661
- ${trimspace(data.local_file.ssh_public_key.content)}
62+
- ${trimspace(data.local_file.ssh_public_key_2.content)}
5763
sudo: ALL=(ALL) NOPASSWD:ALL
5864
runcmd:
5965
- apt update
60-
- apt install -y qemu-guest-agent net-tools nfs-common
66+
- apt install -y qemu-guest-agent
6167
- timedatectl set-timezone America/New_York
6268
- systemctl enable qemu-guest-agent
6369
- systemctl start qemu-guest-agent
64-
- curl -fsSL https://get.docker.com | sudo sh
70+
- apt upgrade -y
6571
- su - mafyuh -c 'git clone https://git.mafyuh.dev/mafyuh/iac.git /home/mafyuh/iac'
66-
- su - mafyuh -c 'git config --global user.name "Mafyuh"'
67-
- su - mafyuh -c 'git config --global user.email "matt@mafyuh.com"'
6872
- echo "done" > /tmp/cloud-config.done
6973
EOF
7074

‎terraform/test.tf

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
resource "proxmox_virtual_environment_vm" "test" {
2+
3+
# VM General Settings
4+
node_name = "pve2"
5+
vm_id = 335
6+
name = "test"
7+
description = "test"
8+
tags = ["tofu", "ubuntu-22", "iac-repo"]
9+
started = true
10+
11+
agent {
12+
enabled = true
13+
}
14+
15+
clone {
16+
vm_id = 9636
17+
}
18+
19+
# VM CPU Settings
20+
cpu {
21+
cores = 4
22+
type = "host"
23+
architecture = "x86_64"
24+
}
25+
26+
# VM Memory Settings
27+
memory {
28+
dedicated = 4096
29+
}
30+
31+
# VM Network Settings
32+
network_device {
33+
bridge = "vmbr0"
34+
vlan_id = 1
35+
}
36+
37+
# VM Disk Settings
38+
disk {
39+
datastore_id = "Fast500Gb"
40+
size = 10
41+
interface = "scsi0"
42+
}
43+
44+
initialization {
45+
ip_config {
46+
ipv4 {
47+
address = "dhcp"
48+
}
49+
}
50+
51+
user_data_file_id = proxmox_virtual_environment_file.cloud_config2.id
52+
}
53+
54+
lifecycle {
55+
ignore_changes = [
56+
initialization[0].user_account[0].keys,
57+
initialization[0].user_account[0].password,
58+
initialization[0].user_account[0].username,
59+
initialization[0].user_data_file_id
60+
]
61+
}
62+
63+
}

0 commit comments

Comments
 (0)
Please sign in to comment.