gpt4 book ai didi

azure - Terraform Azure 将 VM 配置到现有子网/vnet/资源组

转载 作者:行者123 更新时间:2023-12-04 16:04:05 27 4
gpt4 key购买 nike

我有一个现有的 Azure 环境,其中已创建资源组、网络、vnet 和子网。我想使用 terraform 将虚拟机自动部署到现有的 Azure 环境中,但所有示例和文档都面向从头开始构建整个网络/基础设施。

有人可以建议有关如何使用 terraform 将虚拟机部署到现有 Azure 环境的文档或示例吗?

最佳答案

您可以使用Data Source: azurerm_subnetazurerm_resource_group访问现有子网和资源组的属性。

使用现有子网创建新 Linux VM 的示例。在网络接口(interface)中引用子网数据源的输出“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
}

}

更多引用,可以引用这个case .

关于azure - Terraform Azure 将 VM 配置到现有子网/vnet/资源组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52504137/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com