Azure 输出过时的公共 IP 地址

Azure 输出过时的公共 IP 地址

我有一个名为 azure.tf 的 Terraform 文件,该文件用于将虚拟机部署到 Azure,然后通过 SSH 连接到该机器以运行 Ansible playbook。问题是每次我运行 terraform apply 时,都会使用之前虚拟机的旧公共 IP 地址。

我有一个名为outputs.tf的文件,它输出这个IP地址

output "public_ip_address" {
  value = data.azurerm_public_ip.my_terraform_public_ip.ip_address
}

这将输出 IP 地址,但它似乎是已销毁资源的旧 IP 地址

resource "azurerm_resource_group" "rg" {
  location = var.resource_group_location
  name     = "ikwilgraaagdatditwerkt"
}

# Create virtual network
resource "azurerm_virtual_network" "my_terraform_network" {
  name                = "myVnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
}

# Create subnet
resource "azurerm_subnet" "my_terraform_subnet" {
  name                 = "mySubnet"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.my_terraform_network.name
  address_prefixes     = ["10.0.1.0/24"]
}

# Create public IPs
resource "azurerm_public_ip" "my_terraform_public_ip" {
  name                = "testpublicIP"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  allocation_method   = "Dynamic"
}


# Create Network Security Group and rule
resource "azurerm_network_security_group" "my_terraform_nsg" {
  name                = "myNetworkSecurityGroup"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  security_rule {
    name                       = "SSH"
    priority                   = 1001
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

# Create network interface
resource "azurerm_network_interface" "my_terraform_nic" {
  name                = "myNIC"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "my_nic_configuration"
    subnet_id                     = azurerm_subnet.my_terraform_subnet.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.my_terraform_public_ip.id
  }
}

# Connect the security group to the network interface
resource "azurerm_network_interface_security_group_association" "example" {
  network_interface_id      = azurerm_network_interface.my_terraform_nic.id
  network_security_group_id = azurerm_network_security_group.my_terraform_nsg.id
}

resource "azurerm_virtual_machine" "my_terraform_vm" {
  name = "myVM"  #abstract away
  location              = azurerm_resource_group.rg.location
  resource_group_name   = azurerm_resource_group.rg.name
  network_interface_ids = [azurerm_network_interface.my_terraform_nic.id]
  vm_size = "Standard_DS1_v2" #abstract away


  delete_data_disks_on_termination = true

  delete_os_disk_on_termination = true

  storage_image_reference {
    publisher = "canonical"         #abstract away
    offer     = "0001-com-ubuntu-server-focal"    #abstract away
    sku       = "20_04-lts"       #abstract away
    version   = "latest"          #abstract away

  }
    storage_os_disk {
    name              = "vm1-osdisk"   #abstract away
    caching           = "ReadWrite"   #abstract away
    create_option     = "FromImage"   #abstract away
    managed_disk_type = "Standard_LRS"#abstract away
  }
  os_profile {
    computer_name  = "hostname"       #abstract away
    admin_username = "testadmin"      #abstract away
    admin_password = "Password1234!"  #abstract away
  }
  os_profile_linux_config {
    disable_password_authentication = false
  }
   provisioner "remote-exec" {
    inline = ["echo 'Wait until SSH is ready'"]

    connection {
      type        = "ssh"
      user        = "ubuntu"
      private_key =  file(local.private_key_path_Azure)
      host        = azurerm_public_ip.my_terraform_public_ip.ip_address
    }
  }
  provisioner "local-exec" {
    command = "ansible-playbook  -i ${azurerm_public_ip.my_terraform_public_ip.ip_address}, --private-key ${local.private_key_path_Azure} docker.yaml"
  }
}

我查看了门户中的 NIC、公共 IP 和虚拟机,它们都分配了新的公共 IP 地址。那么为什么 remote-exec 使用旧的呢?

任何帮助将不胜感激。

相关内容