From e3e27cc9723c8bcdb9ff859c665d8ed9778022bd Mon Sep 17 00:00:00 2001 From: Ditmar Visser Date: Sun, 27 Apr 2025 15:06:28 +0000 Subject: [PATCH] feat: terraform to azure --- README.md | 5 ++ week-2/main.tf | 13 ---- week-2/{ => opdracht-1}/.terraform.lock.hcl | 20 ++++++ week-2/opdracht-1/main.tf | 71 +++++++++++++++++++ week-2/{ => opdracht-1}/providers.tf | 11 +++ week-2/opdracht-1/secrets.auto.tfvars.example | 5 ++ week-2/opdracht-1/variables.tf | 47 ++++++++++++ week-2/variables.tf | 22 ------ 8 files changed, 159 insertions(+), 35 deletions(-) delete mode 100644 week-2/main.tf rename week-2/{ => opdracht-1}/.terraform.lock.hcl (54%) create mode 100644 week-2/opdracht-1/main.tf rename week-2/{ => opdracht-1}/providers.tf (59%) create mode 100644 week-2/opdracht-1/secrets.auto.tfvars.example create mode 100644 week-2/opdracht-1/variables.tf delete mode 100644 week-2/variables.tf diff --git a/README.md b/README.md index 7a27321..46ceb51 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,11 @@ Now you can access the ESXi dashboard on `192.168.1.5` (or whatever the ip of th 2. `echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list` 3. `sudo apt-get update && sudo apt-get install terraform` +10. Install Azure CLI + 1. `curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash` + 2. Login with `az login` + 3. Choose the `2 - HBO ICT` subscription + ### Test Connection Run the following command and enter the password of the `root` user on the ESXi Hypervisor: `ansible -i '192.168.1.5,' -m ping all -u root -k` diff --git a/week-2/main.tf b/week-2/main.tf deleted file mode 100644 index 959a6bc..0000000 --- a/week-2/main.tf +++ /dev/null @@ -1,13 +0,0 @@ -resource "esxi_guest" "vmtest" { - guest_name = "vmtest" - disk_store = "datastore1" - - memsize = "1024" - 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 = "VM Network" - } -} \ No newline at end of file diff --git a/week-2/.terraform.lock.hcl b/week-2/opdracht-1/.terraform.lock.hcl similarity index 54% rename from week-2/.terraform.lock.hcl rename to week-2/opdracht-1/.terraform.lock.hcl index cbdbfec..74654c6 100644 --- a/week-2/.terraform.lock.hcl +++ b/week-2/opdracht-1/.terraform.lock.hcl @@ -1,6 +1,26 @@ # This file is maintained automatically by "terraform init". # Manual edits may be lost in future updates. +provider "registry.terraform.io/hashicorp/azurerm" { + version = "4.27.0" + constraints = "~> 4.27.0" + hashes = [ + "h1:2fs47aLDaEm93ANXXVRdTjlbUBmFBZRsFjyshKoPE3o=", + "zh:0c69edea1995bd3bd9e61980757169c35bf22281b660b5c755b6cb13d08d29d2", + "zh:25b86bf7b9678371d8573983954c571696f3e64a3967133be3b835da36307106", + "zh:49921cff4f26a49bafada60cd07dabb52c5eb35231059ed928a4f4722e269c82", + "zh:4b986166531f9fd1289f01d8220519443e74888a21da512c1b841b006dad6215", + "zh:53fb65b2ca4df637f03e4748a100a7d7fc77249e307c03e294d6259cec0310f6", + "zh:5c0d021a387ca4e2a5a01da009746a08c45f08e971c10d9bda54539d7264d671", + "zh:600043f2b20dc5a45275e43f175c19fe8b6e8e9557a0c884aef018f1f63de90e", + "zh:a0284f6f38912f67bb4cb7829fda3fa75be81fea6a9b21119965c2a839430092", + "zh:a7ac0576e2069ef77557042c6b5157ded364fbd355b2f9bf7f5441622424086e", + "zh:c5db0bcafe986868e28cc6225b68b2d1cf4bf631939d260ca845f17a9aa1677d", + "zh:ce620c0eb71b1fdd925828b30cf232a869abccf1c459180f2f991c4166315251", + "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", + ] +} + provider "registry.terraform.io/josenk/esxi" { version = "1.10.3" hashes = [ diff --git a/week-2/opdracht-1/main.tf b/week-2/opdracht-1/main.tf new file mode 100644 index 0000000..136cdbf --- /dev/null +++ b/week-2/opdracht-1/main.tf @@ -0,0 +1,71 @@ +# ESXi + +resource "esxi_guest" "main" { + guest_name = "${var.prefix}-vm" + disk_store = "datastore1" + + memsize = "1024" + 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 = "VM Network" + } +} + +# Azure + +resource "azurerm_virtual_network" "main" { + name = "${var.prefix}-network" + address_space = ["10.0.0.0/16"] + location = var.azure_location + resource_group_name = var.azure_resourcegroup +} + +resource "azurerm_subnet" "main" { + name = "internal" + resource_group_name = var.azure_resourcegroup + virtual_network_name = azurerm_virtual_network.main.name + address_prefixes = ["10.0.2.0/24"] +} + +resource "azurerm_network_interface" "main" { + name = "${var.prefix}-nic" + location = var.azure_location + resource_group_name = var.azure_resourcegroup + + ip_configuration { + name = "internal" + subnet_id = azurerm_subnet.main.id + private_ip_address_allocation = "Dynamic" + } +} + +resource "azurerm_linux_virtual_machine" "main" { + name = "${var.prefix}-vm" + location = var.azure_location + resource_group_name = var.azure_resourcegroup + network_interface_ids = [ + azurerm_network_interface.main.id, + ] + size = "Standard_B2ats_v2" + + admin_username = "adminuser" + admin_ssh_key { + username = "adminuser" + public_key = data.azurerm_ssh_public_key.azure.public_key + } + + os_disk { + caching = "ReadWrite" + storage_account_type = "Standard_LRS" + } + + source_image_reference { + publisher = "Canonical" + offer = "ubuntu-24_04-lts" + sku = "server" + version = "latest" + } +} diff --git a/week-2/providers.tf b/week-2/opdracht-1/providers.tf similarity index 59% rename from week-2/providers.tf rename to week-2/opdracht-1/providers.tf index 312570f..91f965a 100644 --- a/week-2/providers.tf +++ b/week-2/opdracht-1/providers.tf @@ -3,6 +3,10 @@ terraform { esxi = { source = "registry.terraform.io/josenk/esxi" } + azurerm = { + source = "hashicorp/azurerm" + version = "~> 4.27.0" + } } } @@ -13,3 +17,10 @@ provider "esxi" { esxi_username = var.esxi_username esxi_password = var.esxi_password } + + +provider "azurerm" { + resource_provider_registrations = "none" + subscription_id = var.azure_subscriptionid + features{} +} \ No newline at end of file diff --git a/week-2/opdracht-1/secrets.auto.tfvars.example b/week-2/opdracht-1/secrets.auto.tfvars.example new file mode 100644 index 0000000..121f1e9 --- /dev/null +++ b/week-2/opdracht-1/secrets.auto.tfvars.example @@ -0,0 +1,5 @@ +esxi_hostname = "" +esxi_username = "" +esxi_password = "" +azure_subscriptionid = "" +azure_resourcegroup = "" \ No newline at end of file diff --git a/week-2/opdracht-1/variables.tf b/week-2/opdracht-1/variables.tf new file mode 100644 index 0000000..eeadc0e --- /dev/null +++ b/week-2/opdracht-1/variables.tf @@ -0,0 +1,47 @@ +variable "esxi_hostname" { + description = "IP address of the ESXi host" +} + +variable "esxi_hostport" { + description = "SSH port of the ESXi host" + default = "22" +} + +variable "esxi_hostssl" { + description = "SSL port of the ESXi host" + default = "443" +} + +variable "esxi_username" { + description = "Username to connect to the ESXi host" +} + +variable "esxi_password" { + description = "Password to connect to the ESXi host" + sensitive = true +} + +variable "azure_subscriptionid" { + description = "ID of the Azure Subscription." + sensitive = true +} + +variable "azure_resourcegroup" { + description = "Name of the Azure Resource Group." +} + +variable "azure_location" { + description = "Location of the Azure resources" + default = "westeurope" +} + +variable "prefix" { + description = "The Prefix used for all resources" + default = "week-2-opdracht-1" +} + +# Pull the SSH public key from Azure Key Vault +data "azurerm_ssh_public_key" "azure" { + name = "azure" + resource_group_name = var.azure_resourcegroup +} \ No newline at end of file diff --git a/week-2/variables.tf b/week-2/variables.tf deleted file mode 100644 index 98d8887..0000000 --- a/week-2/variables.tf +++ /dev/null @@ -1,22 +0,0 @@ -variable "esxi_hostname" { - description = "IP address of the ESXi host" -} - -variable "esxi_hostport" { - description = "SSH port of the ESXi host" - default = "22" -} - -variable "esxi_hostssl" { - description = "SSL port of the ESXi host" - default = "443" -} - -variable "esxi_username" { - description = "Username to connect to the ESXi host" -} - -variable "esxi_password" { - description = "Password to connect to the ESXi host" - sensitive = true -}