다음을 통해 공유


Terraform 기반 SCVMM VM 관리

이 문서에서는 Terraform 템플릿을 사용하여 SCVMM 관리형 온-프레미스 가상 머신에서 수명 주기 작업을 프로그래밍 방식으로 수행하는 방법을 설명합니다.

Hashicorp Terraform 은 클라우드 인프라를 구성하고 배포하는 오픈 소스 IaC(코드로서의 인프라) 도구입니다. Terraform을 사용하면 AzAPI Terraform 공급자를 사용하여 SCVMM 기반 가상 인프라를 관리할 수 있습니다. Azure Arc 지원 SCVMM에서 사용할 수 있는 Terraform 템플릿의 전체 집합은 여기에서 액세스할 수 있습니다.

이 문서에서는 다음 시나리오를 다룹니다.

  • Azure에서 새 SCVMM 관리 온-프레미스 Virtual Machine 만들기

시나리오 1: 새 Virtual Machine 만들기

필수 조건

새 가상 머신을 만들기 전에 다음 필수 구성 요소가 있는지 확인합니다.

  • Arc SCVMM VM 기여자 역할이 있는 Azure 구독 및 리소스 그룹
  • Azure Arc 리소스 브리지가 실행 중 상태인 Arc 지원 SCVMM 서버입니다.
  • Terraform이 설치된 워크스테이션 머신.
  • Arc SCVMM 프라이빗 클라우드 리소스 사용자 역할이 있는 Azure 지원 클라우드 리소스입니다.
  • Arc SCVMM 프라이빗 클라우드 리소스 사용자 역할이 있는 Azure 지원 가상 머신 템플릿 리소스입니다.
  • Arc SCVMM 프라이빗 클라우드 리소스 사용자 역할이 있는 Azure 지원 가상 네트워크 리소스입니다.

1단계: variables.tf 파일에서 VM 변수 정의

자리 표시자가 있는 샘플 variables.tf 파일은 다음과 같습니다.

variable "subscription_id" {
  description = "The subscription ID for the Azure account."
  type        = string
}

variable "resource_group_name" {
  description = "The name of the resource group."
  type        = string
}

variable "___location" {
  description = "The ___location/region where the resources will be created."
  type        = string
}

variable "machine_name" {
  description = "The name of the machine."
  type        = string
}

variable "inventory_item_id" {
  description = "The ID of the Inventory Item for the VM."
  type        = string
}

variable "vm_username" {
  description = "The admin username for the VM."
  type        = string
}

variable "vm_password" {
  description = "The admin password for the VM."
  type        = string
}

variable "cloud_id" {
    description = "The ID of the cloud."
    type        = string
}

variable "template_id" {
  description = "The ID of the template."
  type        = string
}

variable "virtual_network_id" {
  description = "The ID of the virtual network."
  type        = string
}

variable "availability_set_name" {
  description = "The name of the availability set."
  type        = string
  
}

variable "vmmserver_id" {
  description = "The ID of the SCVMM server."
  type        = string
}

variable "custom_location_id" {
  description = "The ID of the custom ___location."
  type        = string
}

2단계: tfvars 파일에서 메타데이터 및 자격 증명 정의

자리 표시자가 있는 샘플 createscvmmVM.tfvars 파일은 다음과 같습니다.

subscription_id      = "your-subscription-id"
resource_group_name  = "your-resource-group"
___location             = "eastus"
machine_name         = "vm-name"
vm_username          = "Administrator"
vm_password          = "your_vm_password"
vmmserver_id         = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.SCVMM/vmmServers/your-vmmserver-name"
availability_set_name = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.SCVMM/AvailabilitySets/your-availabilityset-name" #this parameter is optional
cloud_id             = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.SCVMM/Clouds/your-vmmcloud-name"
template_id          = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.SCVMM/VirtualMachineTemplates/your-template-name"
virtual_network_id   = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.SCVMM/VirtualNetworks/your-vmnetwork-name"
custom_location_id   = "/subscriptions/your-subscription-id/resourcegroups/your-resource-group/providers/Microsoft.extendedlocation/customlocations/your-customlocation-name"

3단계: main.tf 파일에서 VM 구성 정의

자리 표시자가 있는 샘플 main.tf 파일은 다음과 같습니다.

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">= 3.0"
    }
    azapi = {
      source  = "azure/azapi"
      version = ">= 1.0.0"
    }
  }
}

# Configure the AzureRM provider with the subscription ID
provider "azurerm" {
  features {}
  subscription_id = var.subscription_id
}

# Configure the AzAPI provider with the subscription ID
provider "azapi" {
  subscription_id = var.subscription_id
}

# Retrieve the resource group details
data "azurerm_resource_group" "example" {
  name = var.resource_group_name
}

# Create a Hybrid machine resource in Azure
resource "azapi_resource" "test_machine02" {
  schema_validation_enabled = false
  parent_id = data.azurerm_resource_group.example.id
  type = "Microsoft.HybridCompute/machines@2024-07-10"
  name = var.machine_name
  ___location = var.___location
  body = {
      kind = "SCVMM"
      identity = {
        type = "SystemAssigned"
      }
  }
}

# Create a Virtual Machine instance using the Hybrid machine and Inventory Item ID
resource "azapi_resource" "test_inventory_vm001" {
  schema_validation_enabled = false
  type = "Microsoft.SCVMM/VirtualMachineInstances@2024-06-01"
  name = "default"
  parent_id = azapi_resource.test_machine01.id
  body = {
    properties = {
      infrastructureProfile = {
        templateId   = var.template_id
        cloudId      = var.cloud_id
        vmName       = var.machine_name
      }
    # Availability sets, OS profile and hardware profile are optional
      availabilitySets = [
      {
      name = var.availability_set_name
      }
      ]
    # Override VM template to customize VM creation 
      osProfile = {
        computerName    = "myVM"
        adminPassword   = var.vm_password
        domainName      = "cdmlab"
        domainUsername  = "cdmlabuser"
        domainPassword  = "your_domain_password"
        workgroup       = "myWorkgroup"
        runOnceCommands = "echo Hello; echo World"
      }
      hardwareProfile = {
        cpuCount = 4
        memoryMB = 3072
        limitCpuForMigration = "true"
      }
      networkProfile = {
        networkInterfaces = [
          {
            name            = "nic1"
            macAddress      = "00:0A:95:9D:68:16"
            virtualNetworkId = var.virtual_network_id
            ipv4AddressType = "Dynamic"
          }
        ]
      }
      storageProfile = {
        disks = [
          {
            name       = "disk1"
            diskId     = "disk-unique-id-1"
            diskSizeGB = 30
            bus        = 0
            lun        = 0
            busType    = "SCSI"
            vhdType    = "Dynamic"
          }
        ]
      }
    }
    extendedLocation = {
      type = "CustomLocation"
      name = var.custom_location_id
    }
  }
  depends_on = [azapi_resource.test_machine01]
}

# Install Arc agent on the VM
resource "azapi_resource" "guestAgent" {
  type      = "Microsoft.SCVMM/virtualMachineInstances/guestAgents@2024-06-01"
  parent_id = azapi_resource.test_inventory_vm001.id
  name      = "default"
  body = {
    properties = {
      credentials = {
        username = var.vm_username
        password = var.vm_password
      }
      provisioningAction = "install"
    }
  }
  schema_validation_enabled = false
  ignore_missing_property   = false
  depends_on = [azapi_resource.test_inventory_vm001]
}

4단계: Terraform 명령 실행

-var-file 플래그를 사용하여 Terraform 명령을 실행하는 중에 .tfvars 파일을 전달합니다.

  • Terraform 초기화(아직 초기화되지 않은 경우): terraform init
  • 구성 유효성 검사: terraform validate -var-file="createscvmmVM.tfvars"
  • 변경 계획: terraform plan -var-file="createscvmmVM.tfvars"
  • 변경 내용 적용: terraform apply -var-file="createscvmmVM.tfvars"

변경 내용을 적용하려면 yes를 입력하여 프롬프트를 확인합니다.

모범 사례

  • 버전 제어 사용: 시간에 따른 변경 내용을 추적하려면 Terraform 구성 파일을 버전 제어(예: Git)에 유지합니다.
  • 상태 관리: 데이터 손실을 방지하기 위해 Terraform 상태 파일을 정기적으로 백업합니다.

다음 단계

  • Azure에서 SCVMM VM에 대한 작업을 수행합니다.
  • Arc 지원 SCVMM VM 확장을 관리 합니다.