Create VyOS machines

main
Dan Ankers 2023-10-03 22:42:03 +01:00
parent 0709b83b47
commit 5f69ab090c
2 changed files with 109 additions and 0 deletions

94
main.tf
View File

@ -0,0 +1,94 @@
terraform {
required_providers {
proxmox = {
source = "telmate/proxmox"
version = "2.9.8"
}
guacamole = {
source = "techBeck03/guacamole"
version = "1.4.1"
}
}
}
provider "guacamole" {
url = "https://guacamole.k8s.md1clv.im"
token = "9BAEA0A7A51128DEEEB2A0BF71EAB1755F5E8A5E094699C3FD85DFF8F35599E4"
data_source = "mysql"
}
provider "proxmox" {
# url is the hostname (FQDN if you have one) for the proxmox host you'd like to connect to to issue the commands. my proxmox host is 'prox-1u'. Add /api2/json at the end for the API
pm_api_url = "https://172.29.7.12:8006/api2/json"
# api token id is in the form of: <username>@pam!<tokenId>
pm_api_token_id = "dan@md1clv.im!terraform"
# this is the full secret wrapped in quotes. don't worry, I've already deleted this from my proxmox cluster by the time you read this post
pm_api_token_secret = "f6371ace-8997-4b2d-8c16-7567a0ccd849"
# leave tls_insecure set to true unless you have your proxmox SSL certificate situation fully sorted out (if you do, you will know)
pm_tls_insecure = true
}
# resource is formatted to be "[type]" "[entity_name]" so in this case
# we are looking to create a proxmox_vm_qemu entity named test_server
resource "proxmox_vm_qemu" "vyos_router" {
count = 3 # just want 1 for now, set to 0 and apply to destroy VM
name = "vyos-${count.index + 1}" #count.index starts at 0, so + 1 means this VM will be named test-vm-1 in proxmox
# this now reaches out to the vars file. I could've also used this var above in the pm_api_url setting but wanted to spell it out up there. target_node is different than api_url. target_node is which node hosts the template and thus also which node will host the new VM. it can be different than the host you use to communicate with the API. the variable contains the contents "prox-1u"
target_node = var.proxmox_host
# another variable with contents "ubuntu-2004-cloudinit-template"
clone = var.vyos_template_name
# basic VM settings here. agent refers to guest agent
agent = 1
os_type = "cloud-init"
cores = 2
sockets = 1
cpu = "host"
memory = 2048
scsihw = "virtio-scsi-single"
bootdisk = "scsi0"
disk {
slot = 0
# set disk size here. leave it small for testing because expanding the disk takes time.
size = "10G"
type = "scsi"
storage = "NFS-BIG"
iothread = 1
}
# if you want two NICs, just copy this whole network section and duplicate it
network {
model = "virtio"
bridge = "vmbr0"
}
network {
model = "virtio"
bridge = "TestNet1"
}
# not sure exactly what this is for. presumably something about MAC addresses and ignore network changes during the life of the VM
lifecycle {
ignore_changes = [
network,
]
}
# the ${count.index + 1} thing appends text to the end of the ip address
# in this case, since we are only adding a single VM, the IP will
# be 10.98.1.91 since count.index starts at 0. this is how you can create
# multiple VMs and have an IP assigned to each (.91, .92, .93, etc.)
ipconfig0 = "ip=172.29.7.6${count.index + 1}/24,gw=172.29.7.1"
# sshkeys set using variables. the variable contains the text of the key.
sshkeys = <<EOF
${var.ssh_key}
EOF
}

15
vars.tf
View File

@ -0,0 +1,15 @@
variable "ssh_key" {
default = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCxtXWfJ2r6vEQ481lOGSmnb7yJHyDS8qq0/8KRPr98Yri0Tn200RR/XHMAR3wR/mACoaYFoi9DOE1tR0lyUf/qYEcPnN7pRfnF5afOM51YBxlZ1n5osZ70/C23xpqML4Ru6KaYvziysZ2lWe4iNJqOJXjJcOVRnFIA2iD/UIDLZAe/6GNlam4FKn6y5qZm/bCuaQlJsc3SnZIcEQ8yuhSZVOgugZxSXFXvhOt/88HKcrgDm9XA7QXQ9GcgpJ2ZDdIlwy+Iz3cT4HcjTPpmX5yf95FfKN4EATyH2QqTarWRae2L5L5uJXTVKFllQXBw3KFaUK5oXQV35LpgtM94+77IpPFl/u6JTs9tlrnO/dIe9LoXjPphUj0GMioi9IFyOtpon5aksIuEkRLVc8JhNrTcGGsbkSSvlC6ejq7HkM6d+RnwILYqhcfXx+GevVF9a2gidRGupoLzDOYPlqMldGQKNh4ZyfMTF1cbAcDnxFNbYBMEG/mPAktJ4iwaTQqTeMs= dan@linux.fritz.box"
}
variable "proxmox_host" {
default = "px1"
}
variable "template_name" {
default = "ubuntu-2204-cloudinit-template"
}
variable "vyos_template_name" {
default = "vyos-1.5-rolling-202310010025-template"
}