josenk / terraform-provider-esxi

Terraform-provider-esxi plugin
GNU General Public License v3.0
538 stars 154 forks source link

VM's only get an IP address by using cloudinit #211

Closed gzeel closed 10 months ago

gzeel commented 10 months ago

When I deploy a simple VM, it does not receive an IP address.

terraform {
  required_version = ">= 0.13"
  required_providers {
    esxi = {
      source = "registry.terraform.io/josenk/esxi"
    }
  }
}

provider "esxi" {
  esxi_hostname      = "xxxx"
  esxi_hostport      = "22"
  esxi_hostssl       = "443"
  esxi_username      = "root"
  esxi_password      = "xxxxx"
}

resource "esxi_guest" "vmtest" {
  guest_name         = "vmtest"
  disk_store         = "datastore1"
  numvcpus           = 1
  memsize            = 2048

  ovf_source = "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.ova"
  network_interfaces {
    virtual_network = "VM Network"
  }
}
output "ip" {
  value = [esxi_guest.vmtest.ip_address]
}

The Network adapter (VM Network) is connected, and i see a mac address in ESXi. In the logging of the DHCP server I don't see any requests coming in either.

However, when I add cloudinit data to the Terraform file, the vm will get an IP address. example:

guestinfo = {
    "metadata"          = base64encode(templatefile("metadata.yaml", { 
      name = format("db%d", count.index + 1)
    }))
    "metadata.encoding" = "base64"
    "userdata"          = base64encode(templatefile("userdata.yaml", local.templatevars))
    "userdata.encoding" = "base64"
  }

with the following metadata and userdata.yaml content

#cloud-config
local-hostname: ${name}
instance-id: ubuntu-${name}
#cloud-config
users:
  - name: ${ssh_username}
    ssh-authorized-keys:
      - ssh-ed25519 ${public_key}
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    groups: sudo
    shell: /bin/bash
packages:
  - tree
  - ntpdate
  - curl
ntp:
  enabled: true
  ntp_client: chrony
runcmd:
  - date >/root/cloudinit.log
  - echo "Done cloud-init" >>/root/cloudinit.log

Does it have anything to do with the fact that a hostname is set with cloudinit? I've tried different Ubuntu versions (22.04 and 20.04) but with the same result.

OS: Debian 12 Terraform: 1.5.7 Ovftool: 4.4.3 Plugin: 1.10.3

gzeel commented 10 months ago

Nevermind, turns out that a DHCP request is only done when the hostname is set? Cloudinit does this in my example, but this snippet also works

  ovf_properties {
    key = "hostname"
    value = "firstboot"
  }