首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >azurerm_mssql_virtual_machine -已经存在

azurerm_mssql_virtual_machine -已经存在
EN

Stack Overflow用户
提问于 2022-10-10 15:42:47
回答 1查看 82关注 0票数 0

试图做一个AZ Terraform部署,但失败可怕-寻找一些想法,我错过了什么。基本上,我尝试用可变大小的磁盘部署2个VM-(可能稍后更多),将它们加入域并向其添加SQL服务器。(对我温柔点,我来自VMWare-Tf背景,这是我第一次在AZ上部署SQL!)

我的模块:

代码语言:javascript
运行
复制
## main.tf:

# ----------- NIC --------------------------------
resource "azurerm_network_interface" "nic" {
  name                = "${var.vm_name}-nic"
  resource_group_name = var.rg.name
  location            = var.location
  ip_configuration {
    name                          = "${var.vm_name}-internal"
    subnet_id                     = var.subnet_id
    private_ip_address_allocation = "Static"
    private_ip_address            = var.private_ip
  }
  dns_servers = var.dns_servers
}
# ----------- VM --------------------------------
resource "azurerm_windows_virtual_machine" "vm" {
  /* count   = length(var.instances) */
  name                     = var.vm_name
  location                 = var.location
  resource_group_name      = var.rg.name
  network_interface_ids    = [azurerm_network_interface.nic.id]
  size                     = var.size
  zone                     = var.zone
  admin_username           = var.win_admin_user
  admin_password           = var.win_admin_pw # data.azurerm_key_vault_secret.vmadminpwd.value
  enable_automatic_updates = "false"
  patch_mode               = "Manual"
  provision_vm_agent       = "true"
  tags                     = var.vm_tags

  source_image_reference {
    publisher = "MicrosoftSQLServer"
    offer     = "sql2019-ws2019"
    sku       = "enterprise"
    version   = "latest"
  }

  os_disk {
    name                 = "${var.vm_name}-osdisk"
    caching              = "ReadWrite"
    storage_account_type = "StandardSSD_LRS"
    disk_size_gb         = 250
  }
}

# ----------- DOMAIN JOIN --------------------------------
// Waits for up to 1 hour for the Domain to become available. Will return an error 1 if unsuccessful preventing the member attempting to join.
resource "azurerm_virtual_machine_extension" "wait-for-domain-to-provision" {
  name                 = "TestConnectionDomain"
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.9"
  virtual_machine_id   = azurerm_windows_virtual_machine.vm.id
  settings             = <<SETTINGS
  {
    "commandToExecute": "powershell.exe -Command \"while (!(Test-Connection -ComputerName ${var.active_directory_domain_name} -Count 1 -Quiet) -and ($retryCount++ -le 360)) { Start-Sleep 10 } \""
  }
SETTINGS
}
resource "azurerm_virtual_machine_extension" "join-domain" {
  name                 = azurerm_windows_virtual_machine.vm.name
  publisher            = "Microsoft.Compute"
  type                 = "JsonADDomainExtension"
  type_handler_version = "1.3"
  virtual_machine_id   = azurerm_windows_virtual_machine.vm.id
  settings             = <<SETTINGS
    {
        "Name": "${var.active_directory_domain_name}",
        "OUPath": "",
        "User": "${var.active_directory_username}@${var.active_directory_domain_name}",
        "Restart": "true",
        "Options": "3"
    }
SETTINGS
  protected_settings   = <<SETTINGS
    {
        "Password": "${var.active_directory_password}"
    }
SETTINGS
  depends_on           = [azurerm_virtual_machine_extension.wait-for-domain-to-provision]
}
# ----------- DISKS --------------------------------
resource "azurerm_managed_disk" "data" {
  for_each             = var.disks
  name                 = "${var.vm_name}-${each.value.name}"
  location             = var.location
  resource_group_name  = var.rg.name
  storage_account_type = each.value.sa
  create_option        = each.value.create
  disk_size_gb         = each.value.size
  zone                 = var.zone
}
resource "azurerm_virtual_machine_data_disk_attachment" "disk-attachment" {
  for_each           = var.disks
  managed_disk_id    = azurerm_managed_disk.data[each.key].id
  virtual_machine_id = azurerm_windows_virtual_machine.vm.id
  lun                = each.value.lun
  caching            = "ReadWrite"
  depends_on         = [azurerm_windows_virtual_machine.vm]
}
# ----------- SQL --------------------------------
# configure the SQL side of the deployment
resource "azurerm_mssql_virtual_machine" "sqlvm" {
  /* count = length(var.instances) */
  virtual_machine_id    = azurerm_windows_virtual_machine.vm.id
  sql_license_type      = "PAYG"
  r_services_enabled    = true
  sql_connectivity_port = 1433
  sql_connectivity_type = "PRIVATE"
  /* sql_connectivity_update_username = var.sqladmin
    sql_connectivity_update_password = data.azurerm_key_vault_secret.sqladminpwd.value */

  #The storage_configuration block supports the following:
  storage_configuration {
    disk_type             = "NEW"  # (Required) The type of disk configuration to apply to the SQL Server. Valid values include NEW, EXTEND, or ADD.
    storage_workload_type = "OLTP" # (Required) The type of storage workload. Valid values include GENERAL, OLTP, or DW.

    data_settings {
      default_file_path = "F:\\Data"
      luns              = [1]
    }

    log_settings {
      default_file_path = "G:\\Log"
      luns              = [2]
    }

    temp_db_settings {
      default_file_path = "D:\\TempDb"
      luns              = [0]
    }

  }
}

## provider.tf

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">=3.0.1"
      #configuration_aliases = [azurerm.corp]
    }
  }
}

variables.tf

# ----------- COMMON --------------------------------
variable "vm_name" {
  type = string
}
variable "rg" {
  /* type = string */
  description = "STACK - resource group"
}
variable "location" {
  type        = string
  description = "STACK - location"
}
# ----------- NIC --------------------------------
variable "subnet_id" {
  type        = string
  description = "STACK - subnet"
}
variable "private_ip" {
}
variable "dns_servers" {
}
# ----------- VM --------------------------------
variable "size" {
  description = "VM - size"
  type        = string
}
variable "win_admin_user" {
  sensitive = true
  type      = string
}
variable "win_admin_pw" {
  sensitive = true
  type      = string
}
variable "os_storage_type" {
  type = string
}
variable "vm_tags" {
  type = map(any)
}
variable "zone" {
  #type = list
  description = "VM AZ"
}
# ----------- DOMAIN JOIN --------------------------------
variable "active_directory_domain_name" {
  type = string
}
variable "active_directory_username" {
  sensitive = true
}
variable "active_directory_password" {
  sensitive = true
}
# ----------- SQL --------------------------------
variable "sql_maint_day" {
  type        = string
  description = "SQL -  maintenance day"
}
variable "sql_maint_length_min" {
  type        = number
  description = "SQL - maintenance duration (min)"
}
variable "sql_maint_start_hour" {
  type        = number
  description = "SQL-  maintenance start (hour of the day)"
}
# ----------- DISKS --------------------------------
/* variable "disk_storage_account" {
  type        = string
  default     = "Standard_LRS"
  description = "DATA DISKS - storage account type"
}
variable "disk_create_method" {
  type        = string
  default     = "Empty"
  description = "DATA DISKS - creation method"
}
variable "disk_size0" {
  type = number
}
variable "disk_size1" {
  type = number
}
variable "disk_size2" {
  type = number
}
variable "lun0" {
  type    = number
  default = 0
}
variable "lun1" {
  type    = number
  default = 1
}
variable "lun2" {
  default = 2
  type    = number
} */

/* variable "disks" {
  description = "List of disks to create"
  type        = map(any)
  default = {
    disk0 = {
      name   = "data0"
      size   = 200
      create = "Empty"
      sa     = "Standard_LRS"
      lun    = 0
    }
    disk1 = {
      name   = "data1"
      size   = 500
      create = "Empty"
      sa     = "Standard_LRS"
      lun    = 1
    }
  }
} */

variable "disks" {
  type = map(object({
    name   = string
    size   = number
    create = string
    sa     = string
    lun    = number
  }))
}

实际部署:

代码语言:javascript
运行
复制
main.tf
/*
PS /home/fabrice> Get-AzVMSize -Location northeurope | where-object {$_.Name -like "*ds13*"}
*/
module "uat_set" {
  source = "../modules/vm"
  providers = {
    azurerm = azurerm.cbank-test
  }
  for_each                     = var.uat_set
  active_directory_domain_name = local.uat_ad_domain
  active_directory_password    = var.domain_admin_password
  active_directory_username    = var.domain_admin_username
  disks                        = var.disk_allocation
  dns_servers                  = local.dns_servers
  location                     = local.uat_location
  os_storage_type              = local.uat_storage_type
  private_ip                   = each.value.private_ip
  rg                           = data.azurerm_resource_group.main
  size                         = each.value.vm_size
  sql_maint_day                = local.uat_sql_maintenance_day
  sql_maint_length_min         = local.uat_sql_maintenance_min
  sql_maint_start_hour         = local.uat_sql_maintenance_start_hour
  subnet_id                    = data.azurerm_subnet.main.id
  vm_name                      = each.key
  vm_tags                      = var.default_tags
  win_admin_pw                 = var.admin_password
  win_admin_user               = var.admin_username
  zone                         = each.value.zone[0]
}
variable "uat_set" {
  description = "List of VM-s to create"
  type        = map(any)
  default = {
    UAT-SQLDB-NE-01 = {
      private_ip = "192.168.32.8"
      vm_size    = "Standard_DS13-4_v2"
      zone       = ["1"]
    }
    UAT-SQLDB-NE-02 = {
      private_ip = "192.168.32.10"
      vm_size    = "Standard_DS13-4_v2"
      zone       = ["2"]
    }
  }
}
variable "disk_allocation" {
  type = map(object({
    name   = string
    size   = number
    create = string
    sa     = string
    lun    = number
  }))
  default = {
    "temp" = {
      name   = "temp"
      size   = 200
      create = "Empty"
      sa     = "Standard_LRS"
      lun    = 0
    },
    "disk1" = {
      name   = "data1"
      size   = 500
      create = "Empty"
      sa     = "Standard_LRS"
      lun    = 1
    },
    "disk2" = {
      name   = "data2"
      size   = 500
      create = "Empty"
      sa     = "Standard_LRS"
      lun    = 2
    }
  }
}
locals {
  dns_servers                    = ["192.168.34.5", "192.168.34.10"]
  uat_storage_type               = "Standard_LRS"
  uat_sql_maintenance_day        = "Saturday"
  uat_sql_maintenance_min        = 180
  uat_sql_maintenance_start_hour = 23
  uat_ad_domain                  = "civbdev.local"
  uat_location                   = "North Europe"
}

## variables.tf

# new build variables
variable "Environment" {
  default     = "DEV"
  description = "this is the environment variable used to intperpolate with others vars"
}
variable "default_tags" {
  type = map(any)
  default = {
    Environment = "DEV"
    Product     = "dev-XXXtemplateXXX"
    Terraformed = "https://AllicaBankLtd@dev.azure.com/XXXtemplateXXX/Terraform/DEV"
  }
}
variable "admin_username" {
  sensitive = true
}
variable "admin_password" {
  sensitive = true
}
variable "domain_admin_username" {
  sensitive = true
}
variable "domain_admin_password" {
  sensitive = true
}

资源创建OK,但SQL-部件除外。

代码语言:javascript
运行
复制
│ Error: A resource with the ID "/subscriptions/<..redacted...>/providers/Microsoft.SqlVirtualMachine/sqlVirtualMachines/UAT-SQLDB-NE-02" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_mssql_virtual_machine" for more information.
│ 
│   with module.uat_set["UAT-SQLDB-NE-02"].azurerm_mssql_virtual_machine.sqlvm,
│   on ../modules/vm/main.tf line 115, in resource "azurerm_mssql_virtual_machine" "sqlvm":
│  115: resource "azurerm_mssql_virtual_machine" "sqlvm" {
│ 
╵
╷
│ Error: A resource with the ID "/subscriptions/<..redacted...>/providers/Microsoft.SqlVirtualMachine/sqlVirtualMachines/UAT-SQLDB-NE-01" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_mssql_virtual_machine" for more information.
│ 
│   with module.uat_set["UAT-SQLDB-NE-01"].azurerm_mssql_virtual_machine.sqlvm,
│   on ../modules/vm/main.tf line 115, in resource "azurerm_mssql_virtual_machine" "sqlvm":
│  115: resource "azurerm_mssql_virtual_machine" "sqlvm" {
│ 
╵

有什么想法吗?我可能错过了什么?

塔·法布里斯

更新:

谢谢你的答复。只是确认一下:它不是一个已经存在的资源。在创建这些VM-s时,我直接得到了这个错误。

例如,这些是Terraform运行后的vm-s (其中没有一个具有sql扩展)。

计划甚至声明它将创建以下内容:

代码语言:javascript
运行
复制
Terraform will perform the following actions:

  # module.uat_set["UAT-SQLDB-NE-01"].azurerm_mssql_virtual_machine.sqlvm will be created
  + resource "azurerm_mssql_virtual_machine" "sqlvm" {
      + id                    = (known after apply)
      + r_services_enabled    = true
      + sql_connectivity_port = 1433
      + sql_connectivity_type = "PRIVATE"
      + sql_license_type      = "PAYG"
      + virtual_machine_id    = "/subscriptions/..../providers/Microsoft.Compute/virtualMachines/UAT-SQLDB-NE-01"

      + storage_configuration {
          + disk_type             = "NEW"
          + storage_workload_type = "OLTP"

          + data_settings {
              + default_file_path = "F:\\Data"
              + luns              = [
                  + 1,
                ]
            }

          + log_settings {
              + default_file_path = "G:\\Log"
              + luns              = [
                  + 2,
                ]
            }

          + temp_db_settings {
              + default_file_path = "Z:\\TempDb"
              + luns              = [
                  + 0,
                ]
            }
        }
    }

  # module.uat_set["UAT-SQLDB-NE-02"].azurerm_mssql_virtual_machine.sqlvm will be created
  + resource "azurerm_mssql_virtual_machine" "sqlvm" {
      + id                    = (known after apply)
      + r_services_enabled    = true
      + sql_connectivity_port = 1433
      + sql_connectivity_type = "PRIVATE"
      + sql_license_type      = "PAYG"
      + virtual_machine_id    = "/subscriptions/..../providers/Microsoft.Compute/virtualMachines/UAT-SQLDB-NE-02"

      + storage_configuration {
          + disk_type             = "NEW"
          + storage_workload_type = "OLTP"

          + data_settings {
              + default_file_path = "F:\\Data"
              + luns              = [
                  + 1,
                ]
            }

          + log_settings {
              + default_file_path = "G:\\Log"
              + luns              = [
                  + 2,
                ]
            }

          + temp_db_settings {
              + default_file_path = "Z:\\TempDb"
              + luns              = [
                  + 0,
                ]
            }
        }
    }

Plan: 2 to add, 0 to change, 0 to destroy.

想必,如果这些资源以某种方式存在--这将是奇怪的,就像Tf刚刚创建的VM--那么它不会在计划中说它现在就会创建它,对吗?

因此,这个错误是我困惑的根源,因为如果刚刚创建了VM,扩展的创建就失败了--它怎么可能存在?

EN

回答 1

Stack Overflow用户

发布于 2022-10-11 13:58:50

在这种情况下,您可能应该导入模块,因为错误提示您的terraform状态。

例如,terraform import module.uat_set[\"UAT-SQLDB-NE-02\"].azurerm_mssql_virtual_machine.sqlvm "/subscriptions/<..redacted...>/providers/Microsoft.SqlVirtualMachine/sqlVirtualMachines/UAT-SQLDB-NE-02"

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74017412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档