I've just started playing with Terraform, and as a starting point I was importing resources from our vSphere cluster - just a simple test environment I have set up to play with some Ansible stuff.
I've found I'm repeating myself a huge amount, and wondering if there's a better way to do it. I'm not sure modules fit what I'm after, and the resources I'm repeating aren't identical so I'm not sure a straight loop works.
Effectively, I'm looking to set resource defaults which I can then extend/override if required. A short fictional example that demonstrates what I want to achieve:
resource_defaults "vsphere_virtual_machine" {
resource_pool_id = "${data.vsphere_resource_pool.pool.id}"
datastore_id = "${data.vsphere_datastore.datastore.id}"
num_cpus = 2
memory = 1024 guest_id = "other3xLinux64Guest"
network_interface {
network_id = "${data.vsphere_network.network.id}" }
disk {
label = "disk0"
size = 20
}
}
resource "vsphere_virtual_machine" "node1" {
name = "terraform-test"
}
resource "vsphere_virtual_machine" "doublenet" {
name = "terraform-test-2"
network_interface {
network_id = "${data.vsphere_network.other_network.id}"
}
}
resource "vsphere_virtual_machine" "bigdisk" {
name = "terraform-test-3"
disk {
label = "disk0"
size = 100
}
}
Does anyone know how I can achieve what I'm trying to do?
UPDATE: Now that I have a bit more experience with Terraform, modules are exactly what I wanted to do. I just wasn't aware I could pass in parameters! I'll try to elaborate when I get a chance. I did something similar recently and by passing data structures in to the module I could get the similar-but-different instances to build as intended.
Terraform modules are what you want in case of duplication. These encapsulated certain resources with Default values you commonly use, you a set of resources can be reused with minimal Input instead of writing them again.
The Terraform module registry covers a few premade modules for the larger Providers, where you can try out some existing ones or - if your Provider is not supported - develop a better Picture for how a module should be used and designed.