Add comments to describe the code
This commit is contained in:
parent
a7491df751
commit
22f6403417
@ -1,88 +1,103 @@
|
|||||||
|
# Retrieve VM templates available in Proxmox that match the specified name
|
||||||
data "proxmox_virtual_environment_vms" "template" {
|
data "proxmox_virtual_environment_vms" "template" {
|
||||||
filter {
|
filter {
|
||||||
name = "name"
|
name = "name"
|
||||||
values = ["${var.vm_template}"]
|
values = ["${var.vm_template}"] # The name of the template to clone from
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Create a cloud-init configuration file as a Proxmox snippet
|
||||||
resource "proxmox_virtual_environment_file" "cloud_config" {
|
resource "proxmox_virtual_environment_file" "cloud_config" {
|
||||||
content_type = "snippets"
|
content_type = "snippets" # Cloud-init files are stored as snippets in Proxmox
|
||||||
datastore_id = "local"
|
datastore_id = "local" # Local datastore used to store the snippet
|
||||||
node_name = var.node_name
|
node_name = var.node_name # The Proxmox node where the file will be uploaded
|
||||||
|
|
||||||
source_raw {
|
source_raw {
|
||||||
file_name = "vm.cloud-config.yaml"
|
file_name = "vm.cloud-config.yaml" # The name of the snippet file
|
||||||
data = <<-EOF
|
data = <<-EOF
|
||||||
#cloud-config
|
#cloud-config
|
||||||
hostname: ${var.vm_name}
|
hostname: ${var.vm_name}
|
||||||
package_update: true
|
package_update: true
|
||||||
package_upgrade: true
|
package_upgrade: true
|
||||||
packages:
|
packages:
|
||||||
- qemu-guest-agent
|
- qemu-guest-agent # Ensures the guest agent is installed
|
||||||
users:
|
users:
|
||||||
- default
|
- default
|
||||||
- name: ${var.vm_user}
|
- name: ${var.vm_user}
|
||||||
groups: sudo
|
groups: sudo
|
||||||
shell: /bin/bash
|
shell: /bin/bash
|
||||||
ssh-authorized-keys:
|
ssh-authorized-keys:
|
||||||
- "${var.vm_user_sshkey}"
|
- "${var.vm_user_sshkey}" # Inject user's SSH key
|
||||||
sudo: ALL=(ALL) NOPASSWD:ALL
|
sudo: ALL=(ALL) NOPASSWD:ALL
|
||||||
runcmd:
|
runcmd:
|
||||||
- systemctl enable qemu-guest-agent
|
- systemctl enable qemu-guest-agent
|
||||||
- reboot
|
- reboot # Reboot the VM after provisioning
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Define and provision a new VM by cloning the template and applying initialization
|
||||||
resource "proxmox_virtual_environment_vm" "vm" {
|
resource "proxmox_virtual_environment_vm" "vm" {
|
||||||
name = var.vm_name
|
name = var.vm_name # VM name
|
||||||
node_name = var.node_name
|
node_name = var.node_name # Proxmox node to deploy the VM
|
||||||
tags = var.vm_tags
|
tags = var.vm_tags # Optional VM tags for categorization
|
||||||
|
|
||||||
agent {
|
agent {
|
||||||
enabled = true
|
enabled = true # Enable the QEMU guest agent
|
||||||
}
|
}
|
||||||
stop_on_destroy = true
|
|
||||||
|
stop_on_destroy = true # Ensure VM is stopped gracefully when destroyed
|
||||||
|
|
||||||
clone {
|
clone {
|
||||||
vm_id = data.proxmox_virtual_environment_vms.template.vms[0].vm_id
|
vm_id = data.proxmox_virtual_environment_vms.template.vms[0].vm_id # ID of the source template
|
||||||
node_name = data.proxmox_virtual_environment_vms.template.vms[0].node_name
|
node_name = data.proxmox_virtual_environment_vms.template.vms[0].node_name # Node of the source template
|
||||||
}
|
}
|
||||||
bios = var.vm_bios
|
|
||||||
machine = var.vm_machine
|
bios = var.vm_bios # BIOS type (e.g., seabios or ovmf)
|
||||||
|
machine = var.vm_machine # Machine type (e.g., q35)
|
||||||
|
|
||||||
cpu {
|
cpu {
|
||||||
cores = var.vm_cpu
|
cores = var.vm_cpu # Number of CPU cores
|
||||||
type = "host"
|
type = "host" # Use host CPU type for best compatibility/performance
|
||||||
}
|
}
|
||||||
|
|
||||||
memory {
|
memory {
|
||||||
dedicated = var.vm_ram
|
dedicated = var.vm_ram # RAM in MB
|
||||||
}
|
}
|
||||||
|
|
||||||
disk {
|
disk {
|
||||||
datastore_id = var.node_datastore
|
datastore_id = var.node_datastore # Datastore to hold the disk
|
||||||
interface = "scsi0"
|
interface = "scsi0" # Primary disk interface
|
||||||
size = 4
|
size = 4 # Disk size in GB
|
||||||
}
|
}
|
||||||
|
|
||||||
initialization {
|
initialization {
|
||||||
user_data_file_id = proxmox_virtual_environment_file.cloud_config.id
|
user_data_file_id = proxmox_virtual_environment_file.cloud_config.id # Link the cloud-init file
|
||||||
datastore_id = var.node_datastore
|
datastore_id = var.node_datastore
|
||||||
interface = "scsi1"
|
interface = "scsi1" # Separate interface for cloud-init
|
||||||
ip_config {
|
ip_config {
|
||||||
ipv4 {
|
ipv4 {
|
||||||
address = "dhcp"
|
address = "dhcp" # Get IP via DHCP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
network_device {
|
network_device {
|
||||||
bridge = "vmbr0"
|
bridge = "vmbr0" # Use the default bridge
|
||||||
vlan_id = var.vm_vlan
|
vlan_id = var.vm_vlan # VLAN tagging if used
|
||||||
}
|
}
|
||||||
|
|
||||||
operating_system {
|
operating_system {
|
||||||
type = "l26"
|
type = "l26" # Linux 2.6+ kernel
|
||||||
}
|
}
|
||||||
|
|
||||||
vga {
|
vga {
|
||||||
type = "std"
|
type = "std" # Standard VGA type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Output the assigned IP address of the VM after provisioning
|
||||||
output "vm_ip" {
|
output "vm_ip" {
|
||||||
value = proxmox_virtual_environment_vm.vm.ipv4_addresses[1][0]
|
value = proxmox_virtual_environment_vm.vm.ipv4_addresses[1][0] # Second network interface's first IP
|
||||||
description = "VM IP"
|
description = "VM IP"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,18 +1,22 @@
|
|||||||
|
# Define the required Terraform provider block
|
||||||
terraform {
|
terraform {
|
||||||
required_providers {
|
required_providers {
|
||||||
proxmox = {
|
proxmox = {
|
||||||
source = "bpg/proxmox"
|
source = "bpg/proxmox" # Use the community Proxmox provider from the bpg namespace
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Configure the Proxmox provider with API and SSH access
|
||||||
provider "proxmox" {
|
provider "proxmox" {
|
||||||
endpoint = var.proxmox_endpoint
|
endpoint = var.proxmox_endpoint # Proxmox API URL (e.g., https://proxmox.local:8006/api2/json)
|
||||||
api_token = var.proxmox_api_token
|
api_token = var.proxmox_api_token # API token for authentication (should have appropriate permissions)
|
||||||
insecure = false
|
insecure = false # Reject self-signed or invalid TLS certificates (set to true only in trusted/test environments)
|
||||||
|
|
||||||
|
# Optional SSH settings used for VM customization via SSH
|
||||||
ssh {
|
ssh {
|
||||||
agent = false
|
agent = false # Do not use the local SSH agent; use key file instead
|
||||||
private_key = file("~/.ssh/id_ed25519")
|
private_key = file("~/.ssh/id_ed25519") # Load SSH private key from the local file system
|
||||||
username = "root"
|
username = "root" # SSH username for connecting to the Proxmox host
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
node_name = "zenith"
|
node_name = "zenith" # Name of the Proxmox node where the VM will be deployed
|
||||||
vm_name = "zenith-vm"
|
vm_name = "zenith-vm" # Desired name for the new virtual machine
|
||||||
vm_cpu = 2
|
vm_cpu = 2 # Number of CPU cores to allocate to the VM
|
||||||
vm_ram = 2048
|
vm_ram = 2048 # Amount of RAM in MB (2 GB)
|
||||||
vm_vlan = 66
|
vm_vlan = 66 # VLAN ID for network segmentation
|
||||||
|
Loading…
x
Reference in New Issue
Block a user