Ok,以下是Terraform代码:

provider "azurerm" {
  version = "=1.34.0"
}

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

resource "azurerm_virtual_network" "vnet" {
  name                = "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" "subnet" {
  name                 = "subnet"
  address_prefix       = "192.168.10.0/24"
  virtual_network_name = azurerm_virtual_network.vnet.name
  resource_group_name  = azurerm_resource_group.test.name
}

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

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

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

resource "azurerm_virtual_machine" "vm" {
  name                  = "vm"
  location              = azurerm_resource_group.test.location
  resource_group_name   = azurerm_resource_group.test.name
  network_interface_ids = [azurerm_network_interface.nic.id]

  storage_image_reference {
    publisher = "OpenLogic"
    offer     = "CentOS"
    sku       = "8_0"
    version   = "latest"
  }

  storage_os_disk {
    name              = "osdisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  os_profile {
    computer_name  = "vm"
    admin_username = "adminuser"
    admin_password = "Password1234!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }

  depends_on = [
    azurerm_network_security_group.nsg
  ]
}

resource "azurerm_network_interface_security_group_association" "nic_nsg_assoc" {
  network_interface_id      = azurerm_network_interface.nic.id
  network_security_group_id = azurerm_network_security_group.nsg.id
}

请注意更改以下值:

  • resource "azurerm_resource_group" "test":更改资源组名称和位置。
  • resource "azurerm_virtual_network" "vnet":更改虚拟网络名称和地址空间。
  • resource "azurerm_subnet" "subnet":更改子网名称和地址前缀。
  • resource "azurerm_network_security_group" "nsg":更改网络安全组名称。
  • resource "azurerm_network_interface" "nic":更改网络接口名称。
  • ip_configuration块:更改IP地址和分配方式。
  • storage_image_reference块:更改操作系统和版本。
  • os_profile块:更改管理员用户名和密码。

运行terraform init初始化Terraform,然后运行terraform apply创建资源。

帮我用terraform版本123在azure上面创建一个带vnet的虚拟机vnet地址:19216810024虚拟机ip19216810100nsg名称:vm-nsgos操作系统:centos80资源组:test写完请回复Ok

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

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