feat: provision vm's
Some checks failed
Provision & Configure / Terraform Apply (push) Has been cancelled
Some checks failed
Provision & Configure / Terraform Apply (push) Has been cancelled
This commit is contained in:
156
terraform/main.tf
Normal file
156
terraform/main.tf
Normal file
@ -0,0 +1,156 @@
|
||||
# ESXi
|
||||
# Render userdata template with skylab SSH key
|
||||
data "template_file" "esxi_userdata" {
|
||||
template = file("${path.module}/userdata.tftpl")
|
||||
vars = {
|
||||
skylab-ssh-public-key = trimspace(var.skylab_ssh_public_key)
|
||||
azure-ssh-private-key = indent(6, trimspace(var.azure_ssh_private_key))
|
||||
azure-vm-ip = azurerm_linux_virtual_machine.week-6.public_ip_address
|
||||
}
|
||||
}
|
||||
|
||||
resource "esxi_portgroup" "week-6" {
|
||||
name = "${var.prefix}-portgoup"
|
||||
vswitch = "vSwitch0"
|
||||
}
|
||||
|
||||
resource "esxi_guest" "week-6" {
|
||||
guest_name = var.prefix
|
||||
disk_store = "datadisk1"
|
||||
|
||||
memsize = "2048"
|
||||
numvcpus = "1"
|
||||
power = "on"
|
||||
|
||||
ovf_source = "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.ova"
|
||||
|
||||
network_interfaces {
|
||||
virtual_network = esxi_portgroup.week-6.name
|
||||
}
|
||||
|
||||
guestinfo = {
|
||||
"metadata" = base64encode(templatefile("${path.module}/metadata.yaml", {
|
||||
hostname = var.prefix
|
||||
}))
|
||||
"metadata.encoding" = "base64"
|
||||
"userdata" = base64encode(data.template_file.esxi_userdata.rendered)
|
||||
"userdata.encoding" = "base64"
|
||||
}
|
||||
}
|
||||
|
||||
# Azure
|
||||
|
||||
resource "azurerm_virtual_network" "week-6" {
|
||||
name = "${var.prefix}-network"
|
||||
address_space = ["10.0.0.0/16"]
|
||||
location = var.azure_location
|
||||
resource_group_name = var.azure_resourcegroup
|
||||
}
|
||||
|
||||
resource "azurerm_subnet" "week-6" {
|
||||
name = "internal"
|
||||
resource_group_name = var.azure_resourcegroup
|
||||
virtual_network_name = azurerm_virtual_network.week-6.name
|
||||
address_prefixes = ["10.0.2.0/24"]
|
||||
}
|
||||
|
||||
resource "azurerm_public_ip" "week-6" {
|
||||
name = "${var.prefix}-pip"
|
||||
resource_group_name = var.azure_resourcegroup
|
||||
location = var.azure_location
|
||||
allocation_method = "Static"
|
||||
}
|
||||
|
||||
resource "azurerm_network_interface" "week-6" {
|
||||
name = "${var.prefix}-nic"
|
||||
location = var.azure_location
|
||||
resource_group_name = var.azure_resourcegroup
|
||||
|
||||
ip_configuration {
|
||||
name = "internal"
|
||||
subnet_id = azurerm_subnet.week-6.id
|
||||
private_ip_address_allocation = "Dynamic"
|
||||
public_ip_address_id = azurerm_public_ip.week-6.id
|
||||
}
|
||||
}
|
||||
|
||||
resource "azurerm_network_security_group" "week-6" {
|
||||
name = "${var.prefix}-nsg"
|
||||
location = var.azure_location
|
||||
resource_group_name = var.azure_resourcegroup
|
||||
security_rule {
|
||||
access = "Allow"
|
||||
direction = "Inbound"
|
||||
name = "tls"
|
||||
priority = 100
|
||||
protocol = "Tcp"
|
||||
source_port_range = "*"
|
||||
source_address_prefix = "*"
|
||||
destination_port_range = "22"
|
||||
destination_address_prefix = azurerm_network_interface.week-6.private_ip_address
|
||||
}
|
||||
}
|
||||
|
||||
resource "azurerm_network_interface_security_group_association" "week-6" {
|
||||
network_interface_id = azurerm_network_interface.week-6.id
|
||||
network_security_group_id = azurerm_network_security_group.week-6.id
|
||||
}
|
||||
|
||||
# Render userdata template with skylab SSH key
|
||||
data "template_file" "azure_cloudinit" {
|
||||
template = file("${path.module}/cloudinit-azure.yaml")
|
||||
vars = {
|
||||
hostname = var.prefix
|
||||
azure_ssh_public_key = trimspace(var.azure_ssh_public_key)
|
||||
}
|
||||
}
|
||||
|
||||
resource "azurerm_linux_virtual_machine" "week-6" {
|
||||
name = var.prefix
|
||||
location = var.azure_location
|
||||
resource_group_name = var.azure_resourcegroup
|
||||
network_interface_ids = [
|
||||
azurerm_network_interface.week-6.id,
|
||||
]
|
||||
size = "Standard_B2ats_v2"
|
||||
|
||||
admin_username = "adminuser"
|
||||
admin_ssh_key {
|
||||
username = "adminuser"
|
||||
public_key = trimspace(var.azure_ssh_public_key)
|
||||
}
|
||||
|
||||
custom_data = base64encode(data.template_file.azure_cloudinit.rendered)
|
||||
|
||||
os_disk {
|
||||
caching = "ReadWrite"
|
||||
storage_account_type = "Standard_LRS"
|
||||
}
|
||||
|
||||
source_image_reference {
|
||||
publisher = "Canonical"
|
||||
offer = "ubuntu-24_04-lts"
|
||||
sku = "server"
|
||||
version = "latest"
|
||||
}
|
||||
}
|
||||
|
||||
locals {
|
||||
inventory = templatefile("${path.module}/ansible-inventory.tmpl", {
|
||||
esxi_name = esxi_guest.week-6.guest_name
|
||||
esxi_ip = esxi_guest.week-6.ip_address
|
||||
esxi_private_key_file = "~/.ssh/skylab"
|
||||
azure_name = azurerm_linux_virtual_machine.week-6.name
|
||||
azure_ip = azurerm_linux_virtual_machine.week-6.public_ip_address
|
||||
azure_private_key_file = "~/.ssh/azure"
|
||||
})
|
||||
}
|
||||
|
||||
resource "local_file" "ansible_inventory" {
|
||||
content = local.inventory
|
||||
filename = "${path.module}/../ansible/inventory.ini"
|
||||
}
|
||||
|
||||
output "ip_addresses" {
|
||||
value = local_file.ansible_inventory.content
|
||||
}
|
Reference in New Issue
Block a user