Self.ip_address does not seem to work in a module

I have a module I created to create an Ubuntu server on Esxi. Within the resource, I have a connection and a remote-exec provisioner. When I had these in the root module for testing, all worked well. Creating the same resource in a module, it fails with a message that the connection host cannot be empty.

Below is a small snippet of my code for the resourse.

resource "esxi_guest" "Default" {
  guest_name = var.guest_name

  ...

  connection {
    type     = "ssh"
    user     = "mark"
    password = var.vm_password
    host     = self.ip_address
  }

  provisioner "remote-exec" {
    inline = [
      "sudo growpart /dev/sda 1",
      "sudo resize2fs /dev/sda1"
    ]
  }
}

If self does not work, how can I obtain the assigned IP address?

Hi @MarkLFT,

Using self should not be affected by the resource’s location within a module. Can you show a more complete example of how you are using this resource in your configuration?

Many thanks for your reply. The problem seems to be related to setting the MAC address in the provider.

Without setting the MAC address, I receive an IP address from the DHCP server, but each time I create this VM, I receive a new/different IP address.

Hence the reason for trying to set a fixed MAC, so I would receive the same IP address each time.

But this doesn’t work either. So I think I just need to use a static IP address, and use cloud-init to set a fixed IP address each time.