Convert legacy Packer json template to hcl2 template (#247)

* convert legacy Packer json template to hcl2 template
This commit is contained in:
lonegunmanb
2023-09-19 13:37:17 +08:00
committed by GitHub
parent bb15543608
commit f2a6901f3f
12 changed files with 324 additions and 105 deletions

View File

@ -4,9 +4,25 @@ terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
source = "hashicorp/azurerm"
version = "~>2.0"
}
azapi = {
source = "Azure/azapi"
version = "~> 1.0"
}
local = {
source = "hashicorp/local"
version = "2.4.0"
}
random = {
source = "hashicorp/random"
version = "3.5.1"
}
tls = {
source = "hashicorp/tls"
version = "4.0.4"
}
}
}
@ -14,17 +30,19 @@ provider "azurerm" {
features {}
}
resource "random_pet" "id" {}
resource "azurerm_resource_group" "vmss" {
name = var.resource_group_name
name = coalesce(var.resource_group_name, "201-vmss-packer-jumpbox-${random_pet.id.id}")
location = var.location
tags = var.tags
tags = var.tags
}
resource "random_string" "fqdn" {
length = 6
special = false
upper = false
number = false
length = 6
special = false
upper = false
numeric = false
}
resource "azurerm_virtual_network" "vmss" {
@ -32,23 +50,23 @@ resource "azurerm_virtual_network" "vmss" {
address_space = ["10.0.0.0/16"]
location = var.location
resource_group_name = azurerm_resource_group.vmss.name
tags = var.tags
tags = var.tags
}
resource "azurerm_subnet" "vmss" {
name = "vmss-subnet"
resource_group_name = azurerm_resource_group.vmss.name
virtual_network_name = azurerm_virtual_network.vmss.name
address_prefixes = ["10.0.2.0/24"]
address_prefixes = ["10.0.2.0/24"]
}
resource "azurerm_public_ip" "vmss" {
name = "vmss-public-ip"
location = var.location
resource_group_name = azurerm_resource_group.vmss.name
allocation_method = "Static"
domain_name_label = random_string.fqdn.result
tags = var.tags
name = "vmss-public-ip"
location = var.location
resource_group_name = azurerm_resource_group.vmss.name
allocation_method = "Static"
domain_name_label = random_string.fqdn.result
tags = var.tags
}
resource "azurerm_lb" "vmss" {
@ -65,8 +83,8 @@ resource "azurerm_lb" "vmss" {
}
resource "azurerm_lb_backend_address_pool" "bpepool" {
loadbalancer_id = azurerm_lb.vmss.id
name = "BackEndAddressPool"
loadbalancer_id = azurerm_lb.vmss.id
name = "BackEndAddressPool"
}
resource "azurerm_lb_probe" "vmss" {
@ -89,7 +107,7 @@ resource "azurerm_lb_rule" "lbnatrule" {
}
data "azurerm_resource_group" "image" {
name = var.packer_resource_group_name
name = var.packer_resource_group_name
}
data "azurerm_image" "image" {
@ -97,6 +115,31 @@ data "azurerm_image" "image" {
resource_group_name = data.azurerm_resource_group.image.name
}
resource "azapi_resource" "ssh_public_key" {
type = "Microsoft.Compute/sshPublicKeys@2022-11-01"
name = random_pet.id.id
location = azurerm_resource_group.vmss.location
parent_id = azurerm_resource_group.vmss.id
}
resource "azapi_resource_action" "ssh_public_key_gen" {
type = "Microsoft.Compute/sshPublicKeys@2022-11-01"
resource_id = azapi_resource.ssh_public_key.id
action = "generateKeyPair"
method = "POST"
response_export_values = ["publicKey", "privateKey"]
}
resource "random_password" "password" {
count = var.admin_password == null ? 1 : 0
length = 20
}
locals {
admin_password = try(random_password.password[0].result, var.admin_password)
}
resource "azurerm_virtual_machine_scale_set" "vmss" {
name = "vmscaleset"
location = var.location
@ -110,7 +153,7 @@ resource "azurerm_virtual_machine_scale_set" "vmss" {
}
storage_profile_image_reference {
id=data.azurerm_image.image.id
id = data.azurerm_image.image.id
}
storage_profile_os_disk {
@ -121,16 +164,16 @@ resource "azurerm_virtual_machine_scale_set" "vmss" {
}
storage_profile_data_disk {
lun = 0
caching = "ReadWrite"
create_option = "Empty"
disk_size_gb = 10
lun = 0
caching = "ReadWrite"
create_option = "Empty"
disk_size_gb = 10
}
os_profile {
computer_name_prefix = "vmlab"
admin_username = var.admin_user
admin_password = var.admin_password
admin_password = local.admin_password
}
os_profile_linux_config {
@ -138,7 +181,7 @@ resource "azurerm_virtual_machine_scale_set" "vmss" {
ssh_keys {
path = "/home/azureuser/.ssh/authorized_keys"
key_data = file("~/.ssh/id_rsa.pub")
key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}
}
@ -150,20 +193,20 @@ resource "azurerm_virtual_machine_scale_set" "vmss" {
name = "IPConfiguration"
subnet_id = azurerm_subnet.vmss.id
load_balancer_backend_address_pool_ids = [azurerm_lb_backend_address_pool.bpepool.id]
primary = true
primary = true
}
}
tags = var.tags
}
resource "azurerm_public_ip" "jumpbox" {
name = "jumpbox-public-ip"
location = var.location
resource_group_name = azurerm_resource_group.vmss.name
allocation_method = "Static"
domain_name_label = "${random_string.fqdn.result}-ssh"
tags = var.tags
name = "jumpbox-public-ip"
location = var.location
resource_group_name = azurerm_resource_group.vmss.name
allocation_method = "Static"
domain_name_label = "${random_string.fqdn.result}-ssh"
tags = var.tags
}
resource "azurerm_network_interface" "jumpbox" {
@ -205,7 +248,7 @@ resource "azurerm_virtual_machine" "jumpbox" {
os_profile {
computer_name = "jumpbox"
admin_username = var.admin_user
admin_password = var.admin_password
admin_password = local.admin_password
}
os_profile_linux_config {
@ -213,7 +256,7 @@ resource "azurerm_virtual_machine" "jumpbox" {
ssh_keys {
path = "/home/azureuser/.ssh/authorized_keys"
key_data = file("~/.ssh/id_rsa.pub")
key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}
}