We use custom images to deploy virtual machines in Terraform. Custom images have our standardized settings. However, if you use this custom image to deploy it in Terraform, your hostname will not change and it will unconditionally appear as "rockylinuximage01".
Our code is as below.
- var.tf
variable "vm_template" {
type = map(any)
default = {
vm1 = {
source_image_id = "/subscriptions/00000/resourceGroups/image/providers/Microsoft.Compute/images/new-image-rocky8"
vm_name = "test-vm"
hostname = "test-vm"
os_publisher = "resf"
os_offer = "rockylinux-x86_64"
os_sku = "8-base"
os_version = "8.9.20231119"
osdisk_type = "StandardSSD_LRS"
osdisk_size = "32"
vm_type = "Standard_D2s_v5"
nic = "nic01"
os_disk = "osdisk01"
}
}
}
- vm.tf
data "azurerm_key_vault" "kv" {
name = var.kv_name
resource_group_name = var.kv_rg
}
data "azurerm_key_vault_secret" "password" {
name = var.kv_secret_name
key_vault_id = data.azurerm_key_vault.kv.id
}
data "azurerm_resource_group" "system_rg_name" {
name = var.system_rg
}
data "azurerm_subnet" "network_subnet" {
name = var.network_subnet
virtual_network_name = var.network_vnet
resource_group_name = var.network_rg
}
resource "azurerm_network_interface" "nic" {
for_each = var.vm_template
name = "${each.value.vm_name}-${each.value.nic}"
location = var.location
resource_group_name = data.azurerm_resource_group.system_rg_name.name
enable_accelerated_networking = true
ip_configuration {
name = "internal"
subnet_id = data.azurerm_subnet.network_subnet.id
private_ip_address_allocation = var.private-ip-type
private_ip_address = each.value.private_ip
}
}
resource "azurerm_linux_virtual_machine" "vm" {
for_each = var.vm_template
name = each.value.vm_name
location = var.location
resource_group_name = data.azurerm_resource_group.system_rg_name.name
source_image_id = each.value.source_image_id
zone = each.value.zone
#### VM Instance Type
size = each.value.vm_type
network_interface_ids = [
azurerm_network_interface.nic[each.key].id,
]
identity {
type = "SystemAssigned"
}
os_disk {
name = "${each.value.vm_name}-${each.value.os_disk}"
caching = "ReadWrite" # ReadOnly, ReadWrite
storage_account_type = each.value.osdisk_type
disk_size_gb = each.value.osdisk_size
}
plan {
name = each.value.os_sku
publisher = each.value.os_publisher
product = each.value.os_offer
}
computer_name = each.value.hostname
admin_username = var.adminid
admin_password = data.azurerm_key_vault_secret.password.value
disable_password_authentication = false
lifecycle {
ignore_changes = [
admin_password,
]
}
}
If you create and distribute the code using the custom image as above, the "test-vm" you set in "computer_name" will be changed to "rockylinuximage01". I want the hostname I set to be applied to the server. Please help.
2
Answers
Thank you to everyone who helped. I'd like to put my opinions together and use the code modified as follows. I don't know if this is a better way or not, but I'm sure it meets the requirements anyway. Below is the modified code. If you have any better opinions, I would greatly appreciate it if you could let me know.
I have created a
Rocky Linux VM
using a custom image, and it’s taking the specifiedcomputer_name
from the Terraform code.In your case, it’s taking another
computer name
, which may indicate that it’s not using the current Terraform configuration. Please verify the plan before applying it, and also delete any previously created state files before running the code again.Terraform Apply
VM Hostname
If it is still taking the default name, you can change the hostname using the command
sudo hostnamectl set-hostname Linux-RockyVM
with a VM extension.After executing the
VM Extension
, the hostname was changed successfully.Reference: azurerm_virtual_machine_extension