gpt4 book ai didi

azure - Terraform - Azure 上的静态 IP 地址

转载 作者:行者123 更新时间:2023-12-04 07:56:33 27 4
gpt4 key购买 nike

我们需要为通过 terraform 部署在 Azure 中的虚拟机配置静态专用 IP。原因是我们需要通过 ansible 管道在 Ansible 中使用这些。

我在这里找到的一个解决方案是首先创建一个具有“动态”地址的网卡,然后在 Terraform 的下一步中将其转换为“静态”IP。

# Create network interfaces with Private IP's
resource "azurerm_network_interface" "nic" {
for_each = { for vm in var.vms : vm.hostname => vm }
name = "${each.value.hostname}-NIC"
location = var.network_location
resource_group_name = var.vm_resource_group
ip_configuration {
name = "monitoringConfg"
subnet_id = data.azurerm_subnet.vm_subnet.id
private_ip_address_allocation = "dynamic"
}
tags = each.value.extra_tag
}

#Convert Dynamic Private IP's to Static
resource "azurerm_network_interface" "staticnic" {
for_each = { for vm in var.vms : vm.hostname => vm }
name = "${each.value.hostname}-NIC"
location = var.network_location
resource_group_name = var.vm_resource_group
ip_configuration {
name = "monitoringConfg"
subnet_id = data.azurerm_subnet.vm_subnet.id
private_ip_address_allocation = "static"
private_ip_address = azurerm_network_interface.nic[each.key].private_ip_address
}
tags = each.value.extra_tag

但是当我运行这个时,我收到以下错误:

ID 为“/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxxxxxxxxxxxxx/providers/Microsoft.Network/networkInterfaces/xxxxxxxxxxxxxxxxxx-NIC”的资源已存在 - 将通过 Terraform 进行管理资源需要进口到国家。有关详细信息,请参阅“azurerm_network_interface”的资源文档。在 ../../modules/main.tf 第 58 行,资源“azurerm_network_interface”“staticnic”中:58:资源“azurerm_network_interface”“staticnic”{

有人知道我做错了什么或者有更好的方法来处理这个问题吗?

亲切的问候,RB

最佳答案

在网络接口(interface)连接到正在运行的虚拟机(或其他资源)之前,Azure 不会分配动态 IP 地址,请参阅 this 。所以我认为我们不能在虚拟机创建之前将动态IP转换为静态IP,因为该IP地址暂时不存在。

相反,我们可以通过分配该子网范围内的某些 IP 地址来直接将某些静态 IP 地址关联到 Azure VM。阅读 private IP分配方式。

Azure reserves the first four addresses in each subnet address range.The addresses can't be assigned to resources. For example, if thesubnet's address range is 10.0.0.0/16, addresses 10.0.0.0-10.0.0.3 and10.0.255.255 are unavailable.

例如,您可以引用此模板为虚拟机配置静态私有(private) IP:

variable "vmlist" {
type = map(object({
hostname = string
IP_address = string
}))
default = {
vm1 ={
hostname = "vma"
IP_address = "10.0.2.4"
},
vm2 = {
hostname = "vmb"
IP_address = "10.0.2.5"
}
}
}

#...

resource "azurerm_network_interface" "staticnic" {
for_each = var.vmlist
name = "${each.value.hostname}-nic"
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 = "Static"
private_ip_address = each.value.IP_address
}
}

#...

resource "azurerm_virtual_machine" "main" {
for_each = var.vmlist
name = each.value.hostname
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
network_interface_ids = [azurerm_network_interface.staticnic[each.key].id]
vm_size = "Standard_DS1_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 = "2016-Datacenter"
version = "latest"
}

storage_os_disk {
name = "${each.value.hostname}-osdisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = each.value.hostname
admin_username = "testadmin"
admin_password = "Password1234!"
}

os_profile_windows_config {
provision_vm_agent = "true"
}

}

我正在使用

Terraform v0.14.7
+ provider registry.terraform.io/hashicorp/azurerm v2.52.0

enter image description here

更新

如果您想让Azure分配动态IP,然后将其转换为静态IP,您可以使用local-exec Provisioner创建资源后调用本地可执行文件。

resource "null_resource" "example" {

for_each = var.vmlist
provisioner "local-exec" {

command = <<EOT

$Nic = Get-AzNetworkInterface -ResourceGroupName ${azurerm_resource_group.main.name} -Name ${azurerm_network_interface.nic[each.key].name}
$Nic.IpConfigurations[0].PrivateIpAllocationMethod = "Static"
Set-AzNetworkInterface -NetworkInterface $Nic
EOT

interpreter = ["PowerShell", "-Command"]

}
}

关于azure - Terraform - Azure 上的静态 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66678399/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com