繁体   English   中英

使用 terraform 从图像创建 azure vm

[英]create azure vm from image using terraform

我参考了github代码。请在下面找到网址

https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples/vm-from-managed-image

我修改了脚本并执行了 terraform init。 我收到以下错误。

读取 azurerm_network_interface[main] 的配置时出错:在 1:18 解析错误:预期为“)”但发现“.”[0m

我的脚本:

    # Configure the Microsoft Azure Provider
provider "azurerm" {
    subscription_id = "xxxxxxxx"
    client_id       = "xxxxxxxx"
    client_secret   = "xxxxxxxx"
    tenant_id       = "xxxxxxxx"
}

# Locate the existing custom/golden image
data "azurerm_image" "search" {
  name                = "AZLXSPTDEVOPS01_Image"
  resource_group_name = "RG-PLATFORM"
}

output "image_id" {
  value = "/subscriptions/4f5c9f2a-3584-4bbd-a26e-bbf69ffbfbe6/resourceGroups/RG-EASTUS-SPT-PLATFORM/providers/Microsoft.Compute/images/AZLXSPTDEVOPS01_Image"
}

# Create a Resource Group for the new Virtual Machine.
resource "azurerm_resource_group" "main" {
  name     = "RG-TEST"
  location = "eastus"
}

# Create a Virtual Network within the Resource Group
resource "azurerm_virtual_network" "main" {
  name                = "RG-Vnet"
  address_space       = ["10.100.0.0/16"]
  resource_group_name = "${azurerm_resource_group.main.name}"
  location            = "${azurerm_resource_group.main.location}"
}

# Create a Subnet within the Virtual Network
resource "azurerm_subnet" "internal" {
  name                 = "RG-Terraform-snet-in"
  virtual_network_name = "${azurerm_virtual_network.main.name}"
  resource_group_name  = "${azurerm_resource_group.main.name}"
  address_prefix       = "10.100.2.0/24"
}

# Create a Network Security Group with some rules
resource "azurerm_network_security_group" "main" {
  name                = "RG-QA-Test-Web-NSG"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"

  security_rule {
    name                       = "allow_SSH"
    description                = "Allow SSH access"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

# Create a network interface for VMs and attach the PIP and the NSG
resource "azurerm_network_interface" "main" {
  name                      = "myNIC"
  location                  = "${azurerm_resource_group.main.location}"
  resource_group_name       = "${azurerm_resource_group.main.name}"
  network_security_group_id = "${azurerm_network_security_group.main.id}"

  ip_configuration {
    name                          = "primary"
    subnet_id                     = "${azurerm_subnet.internal.id}"
    private_ip_address_allocation = "static"
    private_ip_address            = "${cidrhost("10.100.1.8/24", 4)}"
  }
}

# Create a new Virtual Machine based on the Golden Image
resource "azurerm_virtual_machine" "vm" {
  name                             = "AZLXSPTDEVOPS01"
  location                         = "${azurerm_resource_group.main.location}"
  resource_group_name              = "${azurerm_resource_group.main.name}"
  network_interface_ids            = ["${azurerm_network_interface.main.id}"]
  vm_size                          = "Standard_DS12_v2"
  delete_os_disk_on_termination    = true
  delete_data_disks_on_termination = true

  storage_image_reference {
    id = "${data.azurerm_image.search.id}"
  }

  storage_os_disk {
    name              = "AZLXSPTDEVOPS01-OS"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
}

  os_profile {
    computer_name  = "APPVM"
    admin_username = "admin"
    admin_password = "admin#2019"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}

下面的脚本工作正常

# Configure the Microsoft Azure Provider
provider "azurerm" {
    subscription_id = "xxxx"
    client_id       = "xxxx"
    client_secret   = "xxxx"
    tenant_id       = "xxxx"
}

# Locate the existing custom/golden image
data "azurerm_image" "search" {
  name                = "AZDEVOPS01_Image"
  resource_group_name = "RG-PLATFORM"
}

output "image_id" {
  value = "/subscriptions/xxxxxx/resourceGroups/RG-EASTUS-SPT-PLATFORM/providers/Microsoft.Compute/images/AZLXDEVOPS01_Image"
}

# Create a Resource Group for the new Virtual Machine.
resource "azurerm_resource_group" "main" {
  name     = "RG-OPT-QA-TEST"
  location = "eastus"
}

# Create a Subnet within the Virtual Network
resource "azurerm_subnet" "internal" {
  name                 = "RG-Terraform-snet-in"
  virtual_network_name = "RG-OPT-QA-Vnet"
  resource_group_name  = "${azurerm_resource_group.main.name}"
  address_prefix       = "10.100.2.0/24"
}

# Create a Network Security Group with some rules
resource "azurerm_network_security_group" "main" {
  name                = "RG-QA-Test-Dev-NSG"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"

  security_rule {
    name                       = "allow_SSH"
    description                = "Allow SSH access"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

# Create a network interface for VMs and attach the PIP and the NSG
resource "azurerm_network_interface" "main" {
  name                      = "NIC"
  location                  = "${azurerm_resource_group.main.location}"
  resource_group_name       = "${azurerm_resource_group.main.name}"
  network_security_group_id = "${azurerm_network_security_group.main.id}"

  ip_configuration {
    name                          = "nicconfig"
    subnet_id                     = "${azurerm_subnet.internal.id}"
    private_ip_address_allocation = "static"
    private_ip_address            = "${cidrhost("10.100.2.16/24", 4)}"
  }
}

# Create a new Virtual Machine based on the Golden Image
resource "azurerm_virtual_machine" "vm" {
  name                             = "AZLXDEVOPS01"
  location                         = "${azurerm_resource_group.main.location}"
  resource_group_name              = "${azurerm_resource_group.main.name}"
  network_interface_ids            = ["${azurerm_network_interface.main.id}"]
  vm_size                          = "Standard_DS12_v2"
  delete_os_disk_on_termination    = true
  delete_data_disks_on_termination = true

  storage_image_reference {
    id = "${data.azurerm_image.search.id}"
  }

  storage_os_disk {
    name              = "AZLXDEVOPS01-OS"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
}

  os_profile {
    computer_name  = "APPVM"
    admin_username = "devopsadmin"
    admin_password = "Cssladmin#2019"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}

好吧,对于您评论中的错误,我认为您应该像这样设置子网:

resource "azurerm_subnet" "internal" {
  name                 = "RG-Terraform-snet-in"
  virtual_network_name = "${azurerm_virtual_network.main.name}"
  resource_group_name  = "${azurerm_resource_group.main.name}"
  address_prefix       = "10.100.1.0/24"
}

和虚拟网络的错误,我没有在代码中看到名称为“RG-Vnet”的虚拟网络,如错误所述。 因此,您应该根据需要检查代码中是否一切正常。

要从 Azure 市场中的映像创建 Azure VM,可以按照教程使用 Terraform 在 Azure 中创建完整的 Linux 虚拟机基础结构 您不需要在 Terraform 代码中创建图像资源。 只需在资源azurerm_virtual_machine像这样设置:

storage_os_disk {
        name              = "myOsDisk"
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Premium_LRS"
    } 

此外,当您在同一代码中引用其他资源时,您应该这样做:

virtual_network_name = "${azurerm_virtual_network.main.name}"

不仅仅是字符串名称为“RG-Vnet”,这不是正确的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM