Skip to content

Commit

Permalink
chore: add basic code for generating proxmox VE VM with cloudinit (#1)
Browse files Browse the repository at this point in the history
This adds the working code for generating proxmox VE VM with cloudinit.
  • Loading branch information
hazmei authored Jul 15, 2024
1 parent d5078c1 commit 9d26950
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,51 @@
# terraform-proxmox-vm-nocloud
Terraform module for provisioning proxmox VM with nocloud

<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5.0 |
| <a name="requirement_proxmox"></a> [proxmox](#requirement\_proxmox) | 3.0.1-rc3 |
| <a name="requirement_time"></a> [time](#requirement\_time) | >= 0.11.2 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_proxmox"></a> [proxmox](#provider\_proxmox) | 3.0.1-rc3 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [proxmox_cloud_init_disk.this](https://registry.terraform.io/providers/Telmate/proxmox/3.0.1-rc3/docs/resources/cloud_init_disk) | resource |
| [proxmox_vm_qemu.this](https://registry.terraform.io/providers/Telmate/proxmox/3.0.1-rc3/docs/resources/vm_qemu) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_bootable_iso"></a> [bootable\_iso](#input\_bootable\_iso) | The ISO file name to boot from | `string` | `"ssd:iso/talos-nocloud-amd64.iso"` | no |
| <a name="input_name"></a> [name](#input\_name) | The suffix to use for the cloud-init and VM | `string` | n/a | yes |
| <a name="input_network"></a> [network](#input\_network) | The network configuration for ip cidr, hostnum, gateway, and dns nameserver | <pre>object({<br> ip_subnet = optional(string, "10.255.0.0")<br> ip_hostnum = optional(number, 200)<br> ip_prefix = optional(number, 24)<br> ip_gateway = string<br> dns_nameservers = optional(list(string), ["1.1.1.1", "8.8.8.8"])<br> })</pre> | n/a | yes |
| <a name="input_resource_allocation"></a> [resource\_allocation](#input\_resource\_allocation) | The resource amount to allocation for CPU, Memory, and Storage | <pre>object({<br> cores = optional(number, 1)<br> vcpus = optional(number, 1)<br> sockets = optional(number, 1)<br> memory = optional(number, 256)<br> storage = optional(string, 128)<br> })</pre> | <pre>{<br> "cores": 1,<br> "memory": 256,<br> "sockets": 1,<br> "storage": 128,<br> "vcpus": 1<br>}</pre> | no |
| <a name="input_skip_vm_id"></a> [skip\_vm\_id](#input\_skip\_vm\_id) | The number to increment for VM ids | `number` | `0` | no |
| <a name="input_storage_name"></a> [storage\_name](#input\_storage\_name) | The storage to deploy to | `string` | `"ssd"` | no |
| <a name="input_target_node"></a> [target\_node](#input\_target\_node) | The target node to deploy to | `string` | `"pve"` | no |
| <a name="input_template_to_clone"></a> [template\_to\_clone](#input\_template\_to\_clone) | Template name to clone for the VM | `string` | `null` | no |
| <a name="input_user_data"></a> [user\_data](#input\_user\_data) | n/a | `any` | n/a | yes |
| <a name="input_vm_count"></a> [vm\_count](#input\_vm\_count) | Number of VMs to create | `number` | `1` | no |
| <a name="input_vm_onboot"></a> [vm\_onboot](#input\_vm\_onboot) | Whether to have the VM startup after PVE node starts | `bool` | `false` | no |
| <a name="input_vm_protection"></a> [vm\_protection](#input\_vm\_protection) | Enable/disable the VM protection from being removed | `bool` | `false` | no |
| <a name="input_vm_state"></a> [vm\_state](#input\_vm\_state) | The state of the VM | `string` | `"running"` | no |

## Outputs

No outputs.
<!-- END_TF_DOCS -->
91 changes: 91 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
resource "proxmox_cloud_init_disk" "this" {
count = var.vm_count

name = "${var.name}-${count.index}"
pve_node = var.target_node
storage = var.storage_name

meta_data = yamlencode({
instance_id = sha1(var.name)
local-hostname = "${var.name}-${count.index}"
})

user_data = var.user_data

network_config = yamlencode({
version = 1
config = [{
type = "physical"
name = "eth0"
subnets = [{
type = "static"
address = "${cidrhost("${var.network.ip_subnet}/${var.network.ip_prefix}", 200 + var.skip_vm_id + count.index)}/${var.network.ip_prefix}" # "${cidrhost(var.network.ip_cidr, 200 + count.index)}/24"
gateway = var.network.ip_gateway
dns_nameservers = var.network.dns_nameservers
}]
}]
})
}

resource "proxmox_vm_qemu" "this" {
count = var.vm_count

name = "${var.name}-${count.index}"
desc = <<-EOF
${var.name} VM
Talos Linux Kubernetes Cluster
EOF

vmid = 5000 + var.skip_vm_id + count.index
target_node = var.target_node

# template to clone
clone = var.template_to_clone
full_clone = true

onboot = var.vm_onboot
vm_state = var.vm_state
protection = var.vm_protection

cpu = "x86-64-v2-AES"

cores = 2
vcpus = 2
sockets = 1
memory = 2560

scsihw = "virtio-scsi-single"
os_type = "cloud-init" # cloud-init, # ubuntu, # centos

disks {
ide {
ide2 {
cdrom {
iso = var.bootable_iso
}
}
}
scsi {
scsi0 {
cdrom {
iso = proxmox_cloud_init_disk.this[count.index].id
}
}
scsi1 {
disk {
emulatessd = false
format = "qcow2"
iothread = true
replicate = true
size = "${var.resource_allocation.storage}G"
storage = var.storage_name
}
}
}
}

network {
model = "virtio"
bridge = "vmbr0"
}
}
Empty file added outputs.tf
Empty file.
99 changes: 99 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
variable "name" {
description = "The suffix to use for the cloud-init and VM"
type = string
}

variable "target_node" {
description = "The target node to deploy to"
type = string
default = "pve"
}

variable "storage_name" {
description = "The storage to deploy to"
type = string
default = "ssd" # or local-lvm

validation {
condition = contains(["local-lvm", "ssd"], var.storage_name)
error_message = "Valid values for var: storage_name are (local-lvm, ssd)."
}
}

variable "user_data" {}

variable "template_to_clone" {
description = "Template name to clone for the VM"
type = string
default = null
}

variable "vm_onboot" {
description = "Whether to have the VM startup after PVE node starts"
type = bool
default = false
}

variable "vm_state" {
description = "The state of the VM"
type = string
default = "running"

validation {
condition = contains(["running", "stopped", "started"], var.vm_state)
error_message = "Valid values for var: vm_state are (running, stopped, started)."
}
}

variable "vm_protection" {
description = "Enable/disable the VM protection from being removed"
type = bool
default = false
}

variable "vm_count" {
description = "Number of VMs to create"
type = number
default = 1
}

variable "resource_allocation" {
description = "The resource amount to allocation for CPU, Memory, and Storage"
type = object({
cores = optional(number, 1)
vcpus = optional(number, 1)
sockets = optional(number, 1)
memory = optional(number, 256)
storage = optional(string, 128)
})
default = {
cores = 1
vcpus = 1
sockets = 1
memory = 256
storage = 128
}
}

variable "network" {
description = "The network configuration for ip cidr, hostnum, gateway, and dns nameserver"
type = object({
ip_subnet = optional(string, "10.255.0.0")
ip_hostnum = optional(number, 200)
ip_prefix = optional(number, 24)
ip_gateway = string
dns_nameservers = optional(list(string), ["1.1.1.1", "8.8.8.8"])
})
}

variable "bootable_iso" {
description = "The ISO file name to boot from"
type = string
default = "ssd:iso/talos-nocloud-amd64.iso"
}

variable "skip_vm_id" {
description = "The number to increment for VM ids"
type = number
default = 0
}
14 changes: 14 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_version = ">= 1.5.0"

required_providers {
proxmox = {
source = "Telmate/proxmox"
version = "3.0.1-rc3"
}
time = {
source = "hashicorp/time"
version = ">= 0.11.2"
}
}
}

0 comments on commit 9d26950

Please sign in to comment.