This blog post will demonstrate how to apply one or more licenses to vCenter using Terraform. The tutorial will cover creating a module that can be used across multiple vSphere environments.
First, we will create a folder called “configure_licensing” and within it, we will create the following files:
* provider.tf
* provider_variables.tf
* main.tf
* outputs.tf
The provider.tf file defines the provider to use, while the provider_variables.tf file defines what needs to be passed in to enable the provider. The main.tf file is where we will define our resource and the outputs.tf file is where we will define the outputs we want to receive.
In the variables.tf file, we have declared a variable called “licenses” with a type of list(string). This means it expects to receive one or more license keys, wrapped in quotes and comma separated. We wrap the entire entry in ‘[]‘ brackets. For example, we would pass in something like the following:
[“12345-abcde-fghjk-67890-qwerf”, “67890-qwerf-12345-01CU4-987VC”]
In the main.tf file, we have declared a resource type of vsphere_license.licenseKey. According to the documentation, there are two arguments: one for the license key and one for the user who will be assigned the license. We will only specify the required argument, which is the license key.
To add multiple licenses, we can use a built-in method to find the length of the list, or think of it differently, the size of the list. If we passed in two values (two license keys) into this variable, then the length will be 2. Count will be set to the length.
Here is an example of how we can add multiple licenses:
license_key = var.licenses[count.index]
This is similar to my fruitbowl example (despite the very different syntax!). Terraform will set license_key equal to each of the items in our list(string).
We have also declared a number of outputs that we would like sent to the console when the application has been successful. These outputs are assigned names and can be referenced later on.
To run the application, we will pass in the definitions file with the license keys that we want to register. We will then run a terraform plan and check what changes will be made. Once we are happy with the changes, we can run terraform apply and pass in the plan. The outputs that we requested will be shown at the end of a successful execution.
In conclusion, this tutorial has demonstrated how to apply one or more licenses to vCenter using Terraform. We have also created a module that can be used across multiple vSphere environments. The code for this tutorial can be found in the configure_licensing folder.