0

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?

  • you want to use a module then – Mike Sep 4 '18 at 11:43
  • Care to elaborate? – shearn89 Sep 6 '18 at 20:07
1

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.

  • Thanks for chipping in! I'll have another read through the module docs, at first read they didn't seem to quite fit but that could just be my understanding. Thank you! – shearn89 Oct 12 '18 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.