다음을 통해 공유


Infrastructure as Code 메서드를 사용하여 VMware vCenter에서 가상 머신 만들기

이 문서에서는 Bicep 템플릿을 사용하여 vCenter 리소스로 VM을 프로비전하는 방법을 설명합니다.

Bicep 템플릿을 사용하여 Arc VMware 컴퓨터 만들기

다음 bicep 템플릿을 사용하여 Arc VMware 컴퓨터를 만들 수 있습니다. 다음은 Arc 사용 VMware 리소스에 사용할 수 있는 ARM(Azure Resource Manager), Bicep 및 Terraform 템플릿 목록입니다. 다른 Arc 작업을 트리거하려면 해당하는 ARM 템플릿을 Bicep 템플릿으로 변환합니다.

// Parameters
param vmName string = 'contoso-vm'
param vmAdminPassword string = 'examplepassword!#'
param vCenterId string = '/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso-rg/providers/Microsoft.ConnectedVMwarevSphere/vcenters/contoso-vcenter'
param templateId string = '/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso-rg/providers/Microsoft.ConnectedVMwarevSphere/VirtualMachineTemplates/contoso-template-win22'
param resourcePoolId string = '/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso-rg/providers/Microsoft.ConnectedVMwarevSphere/ResourcePools/contoso-respool'
param datastoreId string = '/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso-rg/providers/Microsoft.ConnectedVMwarevSphere/Datastores/contoso-datastore'
param networkId string = '/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso-rg/providers/Microsoft.ConnectedVMwarevSphere/VirtualNetworks/contoso-network'
param extendedLocation object = {
  type: 'customLocation'
  name: '/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/contoso-rg/providers/Microsoft.ExtendedLocation/customLocations/contoso-customlocation'
}
param ipSettings object = {
  allocationMethod: 'static'
  gateway: ['172.24.XXX.1']
  ipAddress: '172.24.XXX.105'
  subnetMask: '255.255.255.0'
  dnsServers: ['172.24.XXX.9']
}

resource contosoMachine 'Microsoft.HybridCompute/machines@2023-10-03-preview' = {
  name: vmName
  location:'westeurope'
  kind:'VMware'
  properties:{}
  tags: {
    foo: 'bar'
  }
}

resource vm 'Microsoft.ConnectedVMwarevSphere/virtualMachineInstances@2023-12-01' = {
  name: 'default'
  scope: contosoMachine
  extendedLocation: extendedLocation
  properties: {
    hardwareProfile: {
      memorySizeMB: 4096
      numCPUs: 2
    }
    osProfile: {
      computerName: vmName
      adminPassword: vmAdminPassword
    }
    placementProfile: {
      resourcePoolId: resourcePoolId
      datastoreId: datastoreId
    }
    infrastructureProfile: {
      templateId: templateId
      vCenterId: vCenterId
    }
    networkProfile: {
      networkInterfaces: [
        {
          nicType: 'vmxnet3'
          ipSettings: ipSettings
          networkId: networkId
          name: 'VLAN103NIC'
          powerOnBoot: 'enabled'
        }
      ]
    }
  }
}

// Outputs
output vmId string = vm.id

이 문서에서는 Terraform 템플릿을 사용하여 vCenter 리소스로 VM을 프로비전하는 방법을 설명합니다.

Terraform을 사용하여 Arc VMware 컴퓨터 만들기

필수 조건

  • Azure 구독: 활성 Azure 구독이 있는지 확인합니다.
  • Terraform: 컴퓨터에 Terraform을 설치합니다.
  • Azure CLI: Azure CLI를 설치하여 리소스를 인증하고 관리합니다.

다음 단계에 따라 Terraform을 사용하여 Arc VMware 컴퓨터를 만듭니다. 이 문서에서는 다음과 같은 두 가지 시나리오에 대해 설명합니다.

  1. vCenter 인벤토리에서 검색된 VM의 경우 Azure 작업에서 사용하도록 설정하고 Arc 에이전트를 설치합니다.
  2. 템플릿, 리소스 풀, 데이터 저장소를 사용하여 새 Arc VMware VM을 만들고 Arc 에이전트를 설치합니다.

시나리오 1

vCenter 인벤토리에서 검색된 VM의 경우 Azure 작업에서 사용하도록 설정하고 Arc 에이전트를 설치합니다.

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

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 "custom_location_id" {
  description = "The ID of the custom location."
  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 "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 "vm_username" {
  description = "The admin username for the VM."
  type        = string
}

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

variable "inventory_id" {
  description = "The Inventory ID for the VM."
  type        = string
}

variable "vcenter_id" {
  description = "The ID of the vCenter."
  type        = string
}

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

2단계: tfvars 파일 만들기

CreateVMwareVM.tfvars라는 파일을 만들고 변수에 대한 샘플 값을 제공합니다.

subscription_id      = "your-subscription-id"
resource_group_name  = "your-resource-group"
location             = "eastus"
machine_name         = "test_machine0001"
inventory_item_id    = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ConnectedVMwarevSphere/VCenters/your-vcenter-id/InventoryItems/your-inventory-item-id"
custom_location_id   = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ExtendedLocation/customLocations/your-custom-location-id"
vm_username          = "Administrator"
vm_password          = " The admin password for the VM "

3단계: 변수를 사용하도록 구성 수정

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 VMware machine resource in Azure
resource "azapi_resource" "test_machine0001" {
  schema_validation_enabled = false
  parent_id = data.azurerm_resource_group.example.id
  type = "Microsoft.HybridCompute/machines@2023-06-20-preview"
  name = var.machine_name
  location = data.azurerm_resource_group.example.location
  body = jsonencode({
      kind = "VMware"
      identity = {
        type = "SystemAssigned"
      }
  })
}

# Create a Virtual Machine instance using the VMware machine and Inventory Item ID
resource "azapi_resource" "test_inventory_vm0001" {
  schema_validation_enabled = false
  type = "Microsoft.ConnectedVMwarevSphere/VirtualMachineInstances@2023-10-01"
  name = "default"
  parent_id = azapi_resource.test_machine0001.id
  body = jsonencode({
      properties = {
          infrastructureProfile = {
            inventoryItemId = var.inventory_item_id
          }
      }
      extendedLocation = {
        type = "CustomLocation"
        name = var.custom_location_id
      }
  })
  depends_on = [azapi_resource.test_machine0001]
}

# Install Arc agent on the VM
resource "azapi_resource" "guestAgent" {
  type      = "Microsoft.ConnectedVMwarevSphere/virtualMachineInstances/guestAgents@2023-10-01"
  parent_id = azapi_resource.test_inventory_vm0001.id
  name      = "default"
  body = jsonencode({
    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_vm0001]
}

4단계: Terraform 명령 실행

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

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

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

모범 사례

  • 버전 제어 사용: 시간에 따른 변경 내용을 추적하려면 Terraform 구성 파일을 버전 제어(예: Git)에 유지합니다.
  • 신중하게 계획 검토: 변경 내용을 적용하기 전에 항상 Terraform 계획의 출력을 검토하여 어떤 변경 사항이 적용되는지 확인합니다.
  • 상태 관리: 데이터 손실을 방지하기 위해 Terraform 상태 파일을 정기적으로 백업합니다.

다음 단계를 수행하면 Terraform을 사용하여 Azure에서 HCRP 및 Arc VMware VM을 효과적으로 만들고 관리하는 것은 물론, 만들어진 VM에 게스트 에이전트를 설치할 수도 있습니다.

시나리오 2

템플릿, 리소스 풀, 데이터 저장소를 사용하여 새 Arc VMware VM을 만들고 Arc 에이전트를 설치합니다.

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

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 "vm_username" {
  description = "The admin username for the VM."
  type        = string
}

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

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

variable "vcenter_id" {
  description = "The ID of the vCenter."
  type        = string
}

variable "resource_pool_id" {
  description = "The ID of the resource pool."
  type        = string
}

variable "datastore_id" {
  description = "The ID of the datastore."
  type        = string
}

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

2단계: tfvars 파일 만들기

CreateVMwareVM.tfvars라는 파일을 만들고 변수에 대한 샘플 값을 제공합니다.

subscription_id      = "your-subscription-id"
resource_group_name  = "your-resource-group"
location             = "eastus"
machine_name         = "test_machine0002"
vm_username          = "Administrator"
vm_password          = "*********"
template_id          = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ConnectedVMwarevSphere/virtualmachinetemplates/your-template-id"
vcenter_id           = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ConnectedVMwarevSphere/VCenters/your-vcenter-id"
resource_pool_id     = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ConnectedVMwarevSphere/resourcepools/your-resource-pool-id"
datastore_id         = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ConnectedVMwarevSphere/datastores/your-datastore-id"
custom_location_id   = "/subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.ExtendedLocation/customLocations/your-custom-location-id"

3단계: 변수를 사용하도록 구성 수정

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 VMware machine resource in Azure
resource "azapi_resource" "test_machine0002" {
  schema_validation_enabled = false
  parent_id = data.azurerm_resource_group.example.id
  type = "Microsoft.HybridCompute/machines@2023-06-20-preview"
  name = var.machine_name
  location = data.azurerm_resource_group.example.location
  body = jsonencode({
      kind = "VMware"
      identity = {
        type = "SystemAssigned"
      }
  })
}

# Create a Virtual Machine instance using the VMware machine created above
resource "azapi_resource" "test_vm0002" {
  schema_validation_enabled = false
  type = "Microsoft.ConnectedVMwarevSphere/VirtualMachineInstances@2023-10-01"
  name = "default"
  parent_id = azapi_resource.test_machine0002.id
  body = jsonencode({
      properties = {
          infrastructureProfile = {
            templateId = var.template_id
            vCenterId = var.vcenter_id
          }

          placementProfile = {
            resourcePoolId = var.resource_pool_id
            datastoreId = var.datastore_id
          }

          osProfile = {
            adminPassword = var.vm_password
          }
      }
      extendedLocation = {
        type = "CustomLocation"
        name = var.custom_location_id
      }
  })
  depends_on = [azapi_resource.test_machine0002]
}

# Create a guest agent for the VM instance
resource "azapi_resource" "guestAgent" {
  type      = "Microsoft.ConnectedVMwarevSphere/virtualMachineInstances/guestAgents@2023-10-01"
  parent_id = azapi_resource.test_vm0002.id
  name      = "default"
  body = jsonencode({
    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_vm0002]
}

4단계: Terraform 명령 실행

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

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

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

모범 사례

  • 버전 제어 사용: 시간에 따른 변경 내용을 추적하려면 Terraform 구성 파일을 버전 제어(예: Git)에 유지합니다.
  • 신중하게 계획 검토: 변경 내용을 적용하기 전에 항상 Terraform 계획의 출력을 검토하여 어떤 변경 사항이 적용되는지 확인합니다.
  • 상태 관리: 데이터 손실을 방지하기 위해 Terraform 상태 파일을 정기적으로 백업합니다.

다음 단계

Azure에서 VMware VM에 대한 작업 수행