resource "azurerm_resource_group" "test" { name = "test" location = "eastus" }

resource "azurerm_virtual_network" "test" { name = "test-vnet" address_space = ["192.168.10.0/24"] location = azurerm_resource_group.test.location resource_group_name = azurerm_resource_group.test.name }

resource "azurerm_subnet" "test" { name = "test-subnet" resource_group_name = azurerm_resource_group.test.name virtual_network_name = azurerm_virtual_network.test.name address_prefixes = ["192.168.10.0/24"] }

resource "azurerm_network_security_group" "test" { name = "vm-nsg" location = azurerm_resource_group.test.location resource_group_name = azurerm_resource_group.test.name }

resource "azurerm_network_interface" "test" { name = "test-nic" location = azurerm_resource_group.test.location resource_group_name = azurerm_resource_group.test.name

ip_configuration { name = "test-ipconfig" subnet_id = azurerm_subnet.test.id private_ip_address_allocation = "Static" private_ip_address = "192.168.10.100" }

network_security_group_id = azurerm_network_security_group.test.id }

resource "azurerm_linux_virtual_machine" "test" { name = "test-vm" location = azurerm_resource_group.test.location resource_group_name = azurerm_resource_group.test.name network_interface_ids = [azurerm_network_interface.test.id] size = "Standard_B1s"

os_disk { name = "test-osdisk" caching = "ReadWrite" storage_account_type = "Standard_LRS" }

source_image_reference { publisher = "OpenLogic" offer = "CentOS" sku = "8.0" version = "latest" }

admin_username = "adminuser" admin_ssh_key { username = "adminuser" public_key = file("~/.ssh/id_rsa.pub") }

disable_password_authentication = true }

output "vm_ip_address" { value = azurerm_network_interface.test.private_ip_address }

output "vm_ssh_command" { value = "ssh adminuser@${azurerm_network_interface.test.private_ip_address} -i ~/.ssh/id_rsa" }

注意:上述代码中的ssh私钥路径和用户名需要根据实际情况修改。

使用 Terraform 在 Azure 上创建带 VNet 的 CentOS 8.0 虚拟机

原文地址: https://www.cveoy.top/t/topic/mKgg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录