how to pass userdata via terraform

asked 2023-07-13 23:06:47 +0200

hal9000jw gravatar image

updated 2023-07-15 22:21:37 +0200

How do you pass the userdata to cloudbase-init via terraform?

Answer...

• Software
Platform: VMware vSphere 7.0
Cloudbase-init Version: 1.1.4 x64
VM Template: Windows Server 2022 OVF
Terraform Providers: hashicorp/vsphere, hashicorp/cloudinit

• cloudbase-init-unattend.conf
[DEFAULT]
username=Administrator
groups=Administrators
inject_user_password=true
config_drive_raw_hhd=false
config_drive_cdrom=false
config_drive_vfat=false
bsdtar_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\bsdtar.exe
mtools_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\
verbose=true
debug=true
log_dir=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\log\
log_file=cloudbase-init-unattend.log
default_log_levels=comtypes=INFO,suds=INFO,iso8601=WARN,requests=WARN
logging_serial_port_settings=
mtu_use_dhcp_config=false
ntp_use_dhcp_config=false
local_scripts_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\LocalScripts\
check_latest_version=false
metadata_services=cloudbaseinit.metadata.services.vmwareguestinfoservice.VMwareGuestInfoService
plugins=cloudbaseinit.plugins.common.userdata.UserDataPlugin
allow_reboot=false
stop_service_on_exit=false

• cloudbase-init.conf
[DEFAULT]
username=Administrator
groups=Administrators
first_logon_behaviour=always
inject_user_password=true
config_drive_raw_hhd=false
config_drive_cdrom=false
config_drive_vfat=false
bsdtar_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\bsdtar.exe
mtools_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\
verbose=true
debug=true
log_dir=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\log\
log_file=cloudbase-init.log
default_log_levels=comtypes=INFO,suds=INFO,iso8601=WARN,requests=WARN
logging_serial_port_settings=
mtu_use_dhcp_config=false
ntp_use_dhcp_config=false
local_scripts_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\LocalScripts\
check_latest_version=false
metadata_services=cloudbaseinit.metadata.services.vmwareguestinfoservice.VMwareGuestInfoService
netbios_host_name_compatibility=false

• cloudinit.tf (hashicorp/cloudinit)
data "cloudinit_config" "userdata" {
  gzip          = true
  base64_encode = true

  part {
    filename     = "userdata.yml"
    content_type = "text/cloud-config"
    content      = templatefile("${path.module}/cloudinit/userdata.yml", {})
  }
}

• vm.tf (hashicorp/vsphere, excerpt)
  clone {
    template_uuid = data.vsphere_content_library_item.content_library_item.id
    customize {
      windows_options {
        computer_name = local.vm.hostname
        workgroup = local.vm.workgroup
        admin_password = local.vm.admin_password
        time_zone = local.vm.time_zone
      }
      network_interface {
        ipv4_address = local.vm.ipv4_address
        ipv4_netmask = local.vm.ipv4_netmask
      }
      ipv4_gateway    = local.vm.ipv4_gateway
      dns_server_list = local.vm.dns_server_list
    }
  }

  cdrom {
    client_device = true
  }

  extra_config = {
    "guestinfo.metadata" = base64gzip(jsonencode({
    "instance-id": local.vm.hostname,
    })),
    "guestinfo.metadata.encoding" = "gzip+base64",
    "guestinfo.userdata" = data.cloudinit_config.userdata.rendered,
    "guestinfo.userdata.encoding" = "gzip+base64"
  }
edit retag flag offensive close merge delete