简体   繁体   English

Terraform Azure Provision VM 到现有子网/vnet/resourcegroup

[英]Terraform Azure Provision VM to existing subnet/vnet/resourcegroup

I have an existing Azure environment with resource groups, networks, vnets and subnets already created.我有一个现有的 Azure 环境,其中已经创建了资源组、网络、vnet 和子网。 I want to use terraform to automate the deployment of VMs into this existing Azure environment, but all the examples and documentation are oriented towards building entire nevironments/infrastructure from scratch.我想使用 terraform 将 VM 自动部署到这个现有的 Azure 环境中,但所有示例和文档都面向从头开始构建整个环境/基础设施。

Can anyone suggest documentation or examples on how to approach deploying a VM to an existing Azure environment using terraform?任何人都可以建议有关如何使用 terraform 将 VM 部署到现有 Azure 环境的文档或示例吗?

You can use Data Source: azurerm_subnet and azurerm_resource_group to access the properties of the existing subnet and resourcegroup.您可以使用 数据源:azurerm_subnetazurerm_resource_group来访问现有子网和资源组的属性。

A sample for creating a new Linux VM with an existing subnet.使用现有子网创建新 Linux VM 的示例。 Reference the output "subnet_id" of subnet data source in the network interface subnet_id = "${data.azurerm_subnet.test.id}"在网络接口子网subnet_id = "${data.azurerm_subnet.test.id}"引用子网数据源的输出"subnet_id subnet_id = "${data.azurerm_subnet.test.id}"

# refer to a resource group
data "azurerm_resource_group" "test" {
  name = "nancyResourceGroup"
}

#refer to a subnet
data "azurerm_subnet" "test" {
  name                 = "mySubnet"
  virtual_network_name = "myVnet"
  resource_group_name  = "nancyResourceGroup"
}

# Create public IPs
resource "azurerm_public_ip" "test" {
    name                         = "myPublicIP-test"
    location                     = "${data.azurerm_resource_group.test.location}"
    resource_group_name          = "${data.azurerm_resource_group.test.name}"
    public_ip_address_allocation = "dynamic"

}

# create a network interface
resource "azurerm_network_interface" "test" {
  name                = "nic-test"
  location            = "${data.azurerm_resource_group.test.location}"
  resource_group_name = "${data.azurerm_resource_group.test.name}"

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = "${data.azurerm_subnet.test.id}"
    private_ip_address_allocation = "dynamic"
    public_ip_address_id          = "${azurerm_public_ip.test.id}"
  }
}

# Create virtual machine
resource "azurerm_virtual_machine" "test" {
    name                  = "myVM-test"
    location              = "${azurerm_network_interface.test.location}"
    resource_group_name   = "${data.azurerm_resource_group.test.name}"
    network_interface_ids = ["${azurerm_network_interface.test.id}"]
    vm_size               = "Standard_DS1_v2"

# Uncomment this line to delete the OS disk automatically when deleting the VM
delete_os_disk_on_termination = true

# Uncomment this line to delete the data disks automatically when deleting the VM
delete_data_disks_on_termination = true

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"
  }
   storage_os_disk {
    name              = "myosdisk1"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }
   os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }
   os_profile_linux_config {
    disable_password_authentication = false
  }

}

For more reference, you can refer to this case .更多参考可以参考这个案例

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

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