skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.

    resource "azurerm_virtual_machine_extension" "run_command" {
      for_each             = var.vm_template
      name                 = "${each.value.vm_name}-run_command"
      virtual_machine_id   = azurerm_linux_virtual_machine.vm[each.key].id
      publisher            = "Microsoft.Azure.Extensions"
      type                 = "CustomScript"
      type_handler_version = "2.0"
      timeouts {
        create = "1h"
      }
      settings = <<SETTINGS
     {
      "commandToExecute": "bash -c 'hostnamectl set-hostname --static ${each.value.hostname}'"
     }
    SETTINGS
    }
    

  2. The Azure Virtual Machine hostname does not change in terraform

    I have created a Rocky Linux VM using a custom image, and it’s taking the specified computer_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.

    provider "azurerm" {
      features {}
    }
    
    data "azurerm_resource_group" "example" {
      name     = "RG-Name"
    }
    
    resource "azurerm_virtual_network" "example" {
      name                = "rockyvnet"
      address_space       = ["10.0.0.0/16"]
      location            = data.azurerm_resource_group.example.location
      resource_group_name = data.azurerm_resource_group.example.name
    lifecycle  {
        prevent_destroy = true
        }
    }
    
    resource "azurerm_subnet" "example" {
      name                 = "rocky-subnet"
      resource_group_name  = data.azurerm_resource_group.example.name
      virtual_network_name = azurerm_virtual_network.example.name
      address_prefixes     = ["10.0.1.0/24"]
    lifecycle  {
        prevent_destroy = true
        }
    }
    
    resource "azurerm_network_interface" "example" {
      name                = "rockyvm-nic"
      location            = data.azurerm_resource_group.example.location
      resource_group_name = data.azurerm_resource_group.example.name
    
      ip_configuration {
        name                          = "internal"
        subnet_id                     = azurerm_subnet.example.id
        private_ip_address_allocation = "Dynamic"
      }
    lifecycle  {
        prevent_destroy = true
        }
    }
    
    resource "azurerm_linux_virtual_machine" "example" {
      name                = "Linuxvm-Rocky"
      source_image_id     = "/subscriptions/11111b-b5ba-21111111/resourceGroups/RG-name/providers/Microsoft.Compute/galleries/Venkatgallery/images/Venkat-Image"
      resource_group_name = data.azurerm_resource_group.example.name
      location            = data.azurerm_resource_group.example.location
      size                = "Standard_D2s_v5"
      network_interface_ids = [
        azurerm_network_interface.example.id,
      ]
      identity {
        type = "SystemAssigned"
      }
    
      computer_name      = "Rockylinuxvm"
      admin_username      = "adminuser"
      admin_password      = "Welcome@123$"
      disable_password_authentication = false
    
      os_disk {
        name                 = "rockyosdisk"
        caching              = "ReadWrite"
        storage_account_type = "Standard_LRS"
        disk_size_gb         = "128"
      }
    
    
    
      plan {
        name      = "8-base"
        publisher = "resf"
        product   = "rockylinux-x86_64"
      }
      lifecycle  {
        prevent_destroy = true
        }
    }
    
    

    Terraform Apply

    enter image description here

    VM Hostname

    enter image description here

    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.

    resource "azurerm_virtual_machine_extension" "example" {
      name                 = "hostname"
      virtual_machine_id   = azurerm_linux_virtual_machine.example.id
      publisher            = "Microsoft.Azure.Extensions"
      type                 = "CustomScript"
      type_handler_version = "2.0"
    
      settings = <<SETTINGS
     {
      "commandToExecute": "sudo hostnamectl set-hostname Linux-RockyVM && sudo systemctl restart"
     }
    SETTINGS
    }
    

    After executing the VM Extension, the hostname was changed successfully.

    enter image description here

    Reference: azurerm_virtual_machine_extension

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search