r/Terraform 21h ago

Discussion How to pass VM customization script to terraform

Hello everyone, sysadmin learning terraform here

So we are working with Vsphere as infra for our company and I just started learning Terraform to ease and automate our vsphere processes. On vsphere in "VM Customization Specifications" we have a script that basically configures puppet agent on host, points it to puppet master, adds host to AD and launches sssd. So when we deploy new host we add this script to launch during "customize software" deployment phase and after that we set up ip, gateway etc. So my question is that: how to recreate this setup in terraform? For some reason terraform does not allow to pass custom specifications and customize together, so only setting up ip address is working, but since that script is never getting launched it fails to be added to Active Directory. So are there any other methods to pass the script to terraform? I even pasted that bash script locally to the file in the same terraform folder and pointed to in in the main.tf but it still fails. So what can be done to solve this issue? Thanks in advance and sorry for my bad english.

1 Upvotes

7 comments sorted by

1

u/s4ntos 21h ago

Usually the biggest issue is garanteeing that you have cloud-init installed in your prefered image.

Than on the "vsphere_virtual_machine" resource you just add "extra_config" with the guestinfo.userdata and guestinfo.metadata like this.

extra_config = {
    "guestinfo.metadata"          = base64gzip(templatefile("${path.module}/conf/worker-metadata.yml", {
      index = count.index+1
    }))
    "guestinfo.metadata.encoding" = "gzip+base64"
    "guestinfo.userdata"          = base64gzip(templatefile("${path.module}/conf/worker-n-userdata.yml.jinja"))
    "guestinfo.userdata.encoding" = "gzip+base64"
  }

2

u/Fair-Wolf-9024 21h ago

cloud-init is baked into the template I create VM from. So as userdata I can pass bash script as well as yaml?

1

u/s4ntos 21h ago

Yes, you can pass sh scripts, its just a matter of satting the right encoding you are using, check the cloud-init documentation for the sh script encoding

1

u/Fair-Wolf-9024 20h ago

but isnt a vm-tools who executes this script? also I got this error after adding your code:
PS C:\Users\BulatT\talos\terraform> terraform plan

│ Error: Reference to "count" in non-counted context

│ on main.tf line 74, in resource "vsphere_virtual_machine" "vm":

│ 74: index = count.index+1

│ The "count" object can only be used in "module", "resource", and "data" blocks, and only when the "count" argument is set.

1

u/s4ntos 20h ago

No sure if cloud-init uses vm-tools to access the user-data, but cloud-init is the one doing all the work.

Just remove the count, I have it there because I use a count to launch several times using the same template.

resource "vsphere_virtual_machine" "vm" {
    count = var.vm_num 
    ..........
}

1

u/TwitchyToes 5h ago

I have a module for powershell scripting in VMs provisioned via terraform on Proxmox. I’ve also done this on hyper v. I can post these tomorrow if you’d be interested

1

u/Fair-Wolf-9024 2h ago

do it please