从托管磁盘映像创建虚拟机的Terraform [英] Terraform creating VM from managed disk image
问题描述
我希望将市场映像获取到托管磁盘,然后将此托管磁盘连接到具有Terraform的Azure虚拟机.
I wish to get a marketplace image to a managed disk and then have this managed disk attached to a Azure virtual machine with Terraform.
这可以更改虚拟机配置,在该配置中,销毁和重建操作可以使虚拟机保持完整.
This enables the change of the virtual machine configuration where a destroy and rebuild leaves the virtual machine intact.
我发现有类似问题的人,但问题已被解决,没有实现该目标的范例.
I have found people with similar problems but the issues get closed off with no example left of how to get this achieved.
对于平台图片
data "azurerm_platform_image" "2016-Datacenter" {
location = "West Europe"
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
}
使用平台映像创建托管磁盘
Create the managed disk with the platform image
resource "azurerm_managed_disk" "Server-osdisk" {
resource_group_name = "rgroup"
location = "West Europe"
create_option = "FromImage"
image_reference_id = "${data.azurerm_platform_image.server2016.id}"
disk_size_gb = "127"
name = "Server-osdisk"
storage_account_type = "Standard_LRS"
}
然后在 azurerm_virtual_machine
resource "azurerm_virtual_machine" "main" {
# ...
os_profile {
computer_name = "Server"
admin_username = ""
admin_password = ""
}
storage_os_disk {
managed_disk_id = "${azurerm_managed_disk.Server-osdisk.id}"
# os_type = "Windows"
managed_disk_type = "Premium_LRS"
caching = "ReadWrite"
create_option = "Attach"
name = "Server"
}
}
投掷
状态= 400代码="InvalidParameter"消息=必需参数缺少"osDisk.osType"(空)."Target =" osDisk.osType"
Status=400 Code="InvalidParameter" Message="Required parameter 'osDisk.osType' is missing (null)." Target="osDisk.osType"
如果在其中添加 os_type
,则会提示您不能具有计算机名称,用户名和密码所需的 os_profile
If you add os_type
in it throws up that you cannot have os_profile
which is needed for computer name, username and password
有同样问题的人
尝试过解决方案,但抛出了上面提到的错误
Tried solution but throws up the error mentioned above
我在这方面想念什么?
推荐答案
对于您的问题,我尝试解决.您将事情更改为自己的,这只是一个例子.此处的文件:
For your issue, I have a try and make it out. You change things to yours, it is just an example. The file here:
resource "azurerm_resource_group" "main" {
name = "acctestRG"
location = "West Europe"
}
data "azurerm_platform_image" "linux" {
location = "West Europe"
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
}
resource "azurerm_managed_disk" "source" {
name = "acctestmd1"
location = "West Europe"
resource_group_name = "${azurerm_resource_group.main.name}"
storage_account_type = "Standard_LRS"
create_option = "FromImage"
image_reference_id = "${data.azurerm_platform_image.linux.id}"
tags {
environment = "staging"
}
}
resource "azurerm_virtual_network" "main" {
name = "azuretestvnet"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
}
resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = "${azurerm_resource_group.main.name}"
virtual_network_name = "${azurerm_virtual_network.main.name}"
address_prefix = "10.0.2.0/24"
}
resource "azurerm_network_interface" "main" {
name = "azuretestnic"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.internal.id}"
private_ip_address_allocation = "dynamic"
}
}
resource "azurerm_virtual_machine" "main" {
name = "azurevm"
location = "West Europe"
resource_group_name = "${azurerm_resource_group.main.name}"
network_interface_ids = ["${azurerm_network_interface.main.id}"]
vm_size = "Standard_DS1_v2"
storage_os_disk {
os_type = "Linux"
name = "acctestmd1"
managed_disk_type = "Standard_LRS"
caching = "ReadWrite"
create_option = "Attach"
managed_disk_id = "${azurerm_managed_disk.source.id}"
}
os_profile_linux_config {
disable_password_authentication = false
}
}
我遇到了一些事情,我想您应该注意它们.
And there are some things I have met and I think you should pay attention to them.
- 虚拟机中的managed_disk_type和托管磁盘中的storage_account_type应该相同.
- 托管磁盘的名称在两者中应该相同.
希望这会对您有所帮助.
Hope this will help you.
这篇关于从托管磁盘映像创建虚拟机的Terraform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!