如何解决使用Terraform将NIC附加到Azure中正在运行的VM
我写了一个terraform脚本在Azure中部署VM。我想在不同时间将多个NIC附加/分离到该VM。
由于虚拟机在部署后开始运行,因此当我尝试向network_interface_ids
资源块内的azurerm_virtual_machine
添加另一个NIC ID时出现错误。
我认为terraform尚无法更改Azure中VM的状态。
没有在terraform中看到任何VM-NIC关联资源,该如何实现?
我是接触云和地形的初学者,所以这可能是一个基本问题,但是我在任何地方都找不到解决方案。任何帮助表示赞赏。
我当前的代码如下:
resource "azurerm_network_interface" "nic1" {
name = "nic1"
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
enable_accelerated_networking = true
ip_configuration {
name = "nic1-config1"
# public_ip_address_id = azurerm_public_ip.public_ip.id
private_ip_address_allocation = "dynamic"
subnet_id = data.azurerm_subnet.subnet.id
primary = true
}
ip_configuration {
name = "nic1-config2"
public_ip_address_id = azurerm_public_ip.public_ip.id
private_ip_address_allocation = "dynamic"
subnet_id = data.azurerm_subnet.subnet.id
}
tags = var.TAGS
}
resource "azurerm_network_interface" "nic2" {
name = "nic2"
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
enable_accelerated_networking = true
ip_configuration {
name = "nic2-config1"
# public_ip_address_id = azurerm_public_ip.public_ip.id
private_ip_address_allocation = "dynamic"
subnet_id = data.azurerm_subnet.subnet.id
primary = true
}
ip_configuration {
name = "nic2-config2"
# public_ip_address_id = azurerm_public_ip.public_ip.id
private_ip_address_allocation = "dynamic"
subnet_id = data.azurerm_subnet.subnet.id
}
tags = var.TAGS
}
resource "azurerm_virtual_machine" "vm" {
name = "vm"
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
network_interface_ids = [azurerm_network_interface.nic1.id]
vm_size = "Standard_D4S_v3"
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
storage_os_disk {
name = "os-disk"
create_option = "FromImage"
caching = "ReadWrite"
managed_disk_type = "Premium_lrs"
disk_size_gb = 32
}
primary_network_interface_id = azurerm_network_interface.nic.id
storage_image_reference {
id = lookup(var.VMI,data.azurerm_resource_group.rg.location)
}
os_profile {
admin_username = "test"
computer_name = "test"
admin_password = "test"
}
os_profile_linux_config {
disable_password_authentication = true
ssh_keys {
path = "/home/user/.ssh/authorized_keys"
key_data = "..."
}
}
tags = var.TAGS
}
我成功部署了上述基础架构。
现在,我要将 nic2 附加到该虚拟机,因此进行了以下更改
resource "azurerm_virtual_machine" "vm" {
....
network_interface_ids = [azurerm_network_interface.nic1.id,azurerm_network_interface.nic2.id]
...
}
https://azure.microsoft.com/en-us/blog/introducing-the-new-dv3-and-ev3-vm-sizes说我最多可以将两个NIC连接到Standard_D4s_v3。
错误:compute.VirtualMachinesClient#CreateOrUpdate代码=“ AddingOrDeletingNetworkInterfacesOnARunningVirtualMachineNotSupported” Message =“具有单个网络接口的虚拟机vm必须先停止释放,然后才能更新为具有多个网络接口,反之亦然。”
我想知道是否可以通过某种方式将NIC热连接到VM上吗?
我正在看这个https://github.com/Azure/azure-powershell/issues/4565
解决方法
将另一个NIC附加到现有VM时,VM必须处于已停止(已取消分配)状态。它是设计的。而且Terraform没有用于VM与NIC之间关联的单独资源。
因此,据我所知,无论如何,您首先需要停止虚拟机,然后将第二个nic id添加到虚拟机中。
resource "azurerm_virtual_machine" "vm" {
....
network_interface_ids = [azurerm_network_interface.nic1.id,azurerm_network_interface.nic2.id]
...
}
VM处于停止状态时,然后应用Terraform代码。要通过Terraform停止VM,可以使用null_resource with local-exec provisioner执行CLI命令:
resource "null_resource" "example2" {
provisioner "local-exec" {
command = "az vm stop --resource-group groupName --name vmName"
interpreter = ["/bin/bash","-c"]
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。