Search code examples
terraformvsphere

Configure optional DHCP usage in Terraform for vSphere


Using Terraform to configure vSphere vms, I'd like to be able to provide an IP address (and gateway and netmask) in the tfvars file, but have the vm default to using DHCP if the values are not provided. I know it will use DHCP if the 'vsphere_virtual_machine' resources' 'customize' block contains an empty 'network_interface' block. I was hoping that be giving a default value of "" to the settings in the variables.tf file I could set values if present and use DHCP if not, but I get an error stating:

Error: module.vm.vsphere_virtual_machine.node: clone.0.customize.0.network_interface.0.ipv4_netmask: cannot parse '' as int: strconv.ParseInt: parsing "": invalid syntax

So putting in a blank string won't parse, and it won't just leave the whole network_interface blank if the values are blank.

I can't use COUNT on a subresource, so the only thing I've come up with so far is to put two entire, nearly identical, 'vsphere_virtual_machine' resources into my module and then put COUNT statements on both so only one gets created, depending on whether the network settings are provided or not, but man, does that seem ugly...?


Solution

  • I think you are in luck. I've been waiting for this exact same problem to be solved since almost a year now.

    Lo and behold, Terraform v0.12.0-alpha1: They now support dynamic block definitions instead of just static ones

    Enjoy, while I'm gonna throw away a couple of hundreds of lines worth of hacks just like the one you mentioned...