Score:0

Azure outputs outdated public IP-address

cl flag

I have a Terraform file called azure.tf which is supposed to deploy a Virtual machine to Azure and afterwards SSH to said machine to run an Ansible playbook. The problem is that everytime i run terraform apply, the old public IP-address of the previous vm gets used.

I've got a file called outputs.tf which outputs this IP-address

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

This Outputs IP address but it seems to be the old IP-adress of the already destroyed resource

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"
  }
}

I've looked at the NIC, public IP, and vm in the portal and they all get assigned the new public IP-adress. So why does the remote-exec use the old one?

Any help would be greatly appreciated.

ph flag
Duplicate of https://stackoverflow.com/questions/76375117/azure-outputs-outdated-public-ip-address
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.