我正在使用Terraform在Azure Devops中创建Windows 2019 Server映像的过程。至此,我可以使用NIC和磁盘(c:驱动器)创建虚拟机了。我正在尝试添加/附加第二个驱动器(f:驱动器)。当我进入磁盘管理时,驱动器空间将作为未分配空间列出,并且在文件资源管理器中未分配驱动器号。以下是我在我的主要instance.tf文件中使用的代码。
我正在尝试运行我的assign-data-disk.ps1脚本,以使用remote-exec分配f:驱动器并打开适当的防火墙端口,不幸的是没有任何工作。甚至尝试过azurerm_virtual_machine_extension资源,但是没有运气。这有可能吗?我试图在所有文档中进行搜索,然后在google上搜索网络,但倒霉了。任何帮助或指导将不胜感激。
resource "azurerm_network_interface" "nic" {
name = "${var.server_name}-nic"
location = "${lookup(var.resource_group_location, var.resource_group_name)}"
resource_group_name = "${var.resource_group_name}"
ip_configuration {
name = "primary"
subnet_id = "${lookup(var.subnet_id, var.resource_group_name)}"
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_network_security_group" "winrm" {
name = "${var.server_name}-winrm"
location = "${lookup(var.resource_group_location, var.resource_group_name)}"
resource_group_name = "${var.resource_group_name}"
security_rule {
name = "WinRM-HTTP"
priority = 110
direction = "Inbound"
access = "Allow"
protocol = "TCP"
source_port_range = "*"
destination_port_range = "5985"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "WinRM-HTTPS"
priority = 115
direction = "Inbound"
access = "Allow"
protocol = "TCP"
source_port_range = "*"
destination_port_range = "5986"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags = {
environment = "test"
}
}
resource "azurerm_virtual_machine_extension" "vm" {
name = "${var.server_name}-extension"
location = "${lookup(var.resource_group_location, var.resource_group_name)}"
resource_group_name = "${var.resource_group_name}"
virtual_machine_name = "hostname"
publisher = "Microsoft.OSTCExtensions"
type = "CustomDataDriveScript"
type_handler_version = "1.2"
settings = <<SETTINGS
{
"commandToExecute": "/terraform/scripts/assign-data-disk.ps1"
}
SETTINGS
}
resource "azurerm_virtual_machine" "vm" {
name = "${var.server_name}"
location = "${lookup(var.resource_group_location, var.resource_group_name)}"
resource_group_name = "${var.resource_group_name}"
network_interface_ids = ["${azurerm_network_interface.nic.id}"]
vm_size = "Standard_DS3_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 = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2019-Datacenter"
version = "latest"
}
storage_os_disk {
name = "Mike-myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "${var.data_disk_type}"
}
storage_data_disk{
name = "Mike-mydatadisk1"
create_option = "Empty"
disk_size_gb = "${var.data_disk_size}"
lun = "0"
}
os_profile {
computer_name = "hostname"
admin_username = "${var.admin_username}"
admin_password = "${var.admin_password}"
}
os_profile_windows_config {
enable_automatic_upgrades = "false"
timezone = "Eastern Standard Time"
}
tags = {
environment = "test"
}
provisioner "file" {
source = "${var.SCRIPTS_SOURCE_FOLDER}/assign-data-disk.ps1"
destination = "C:/Software/scripts/assign-data-disk.ps1"
connection {
type = "winrm"
port = "5985"
https = "true"
insecure = "true"
use_ntlm = "true"
host = "${azurerm_network_interface.nic.private_ip_address}"
user = "${var.admin_username}"
password = "${var.admin_password}"
timeout = "2m"
}
}
provisioner "remote-exec" {
inline = [
"echo Running assign-data-disk.ps1 script...",
"powershell c:/Software/scripts/assign-data-disk.ps1",
"echo Finished assign-data-disk.ps1 script.",
]
connection {
type = "winrm"
port = "5985"
https = "true"
insecure = "true"
use_ntlm = "true"
host = "${azurerm_network_interface.nic.private_ip_address}"
user = "${var.admin_username}"
password = "${var.admin_password}"
timeout = "2m"
}
}
}
当我运行“ terraform.exe apply”时,我实际上没有收到任何错误。但是.ps1脚本文件未复制到Azure 2019 Server。直到我将WinRM范围从“本地子网”更改为“任何IP地址”后,端口才正式开放。而且f:驱动器未正确分配/分配。