帮我用terraform在azure上面创建一个带vnet的虚拟机vnet地址:19216810024虚拟机ip19216810100nsg名称:vm-nsgos操作系统:centos
7,用户名:azureuser,密码:P@ssw0rd1234。
可以参考以下代码:
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = "example-resource-group"
location = "eastus"
}
resource "azurerm_virtual_network" "example" {
name = "example-vnet"
address_space = ["192.168.10.0/24"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_subnet" "example" {
name = "example-subnet"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["192.168.10.0/28"]
}
resource "azurerm_network_security_group" "example" {
name = "vm-nsg"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
security_rule {
name = "allow_ssh"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
resource "azurerm_public_ip" "example" {
name = "example-pip"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
allocation_method = "Static"
tags = {
environment = "example"
}
}
resource "azurerm_network_interface" "example" {
name = "example-nic"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
ip_configuration {
name = "example-ipc"
subnet_id = azurerm_subnet.example.id
private_ip_address_allocation = "Static"
private_ip_address = "192.168.10.100"
public_ip_address_id = azurerm_public_ip.example.id
}
}
resource "azurerm_linux_virtual_machine" "example" {
name = "example-vm"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
size = "Standard_B2ms"
admin_username = "azureuser"
admin_password = "P@ssw0rd1234"
network_interface_ids = [
azurerm_network_interface.example.id,
]
os_disk {
name = "example-osdisk"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "OpenLogic"
offer = "CentOS"
sku = "7.6"
version = "latest"
}
custom_data = <<EOF
#!/bin/bash
echo "Hello, World!" > /tmp/hello.txt
EOF
}
在执行完上述代码后,你将在Azure上创建一个名为“example-vm”的虚拟机,并使用CentOS 7.6操作系统。虚拟机将使用名为“example-nic”的网络接口,该接口将使用名为“example-subnet”的子网。虚拟机的IP地址为192.168.10.100,公共IP地址为动态分配。虚拟机将使用名为“vm-nsg”的网络安全组,该安全组将允许SSH连接。虚拟机将具有一个名为“example-osdisk”的OS磁盘,并在创建时将使用自定义数据来创建名为“/tmp/hello.txt”的文件。
原文地址: https://www.cveoy.top/t/topic/bh7u 著作权归作者所有。请勿转载和采集!