The new refreshed Terraform vCloud Director provider enables administrators and DevOps engineers to define vCloud Director “infrastructure as code” inside Terraform configuration files. This makes it an efficient automation and integration tool and this project is fully open-source and available on GitHub and also HashiCorp is hosting it in the “terraform-providers” namespace together with all the other official Terraform providers.
If you’d like to contribute with a feature request, report an issue or propose a code improvement please visit the project’s site below. There you can also see current activity and what’s in the works.
Terraform Installation & Configuration:
Terraform installation is very simple, it is just a single file. if you are running Linux OS system it is “terraform” and if it is windows based systems then it is terraform.exe.You can download the latest version of Terraform from the HashiCorp website using this direct link: https://www.terraform.io/downloads.html
I am using Windows in my Lab so for my Windows based system, I simply downloaded the Terraform Windows x64 file and put into a folder called “c:\tf” then added this folder into my PATHS variable so that I could run terraform.exe from anywhere. This can be done by going to System Properties –> Advanced –> Environment Variable
now go to “System variables” and add :terraform.exe” folder location.
- Select Path and Click on “Edit”
- at the end of the line put symbol “;” and add “terraform.exe” directory path.
Download Terraform vCloud Director Plugin and VS Code:
Download the latest vCloud director terraform plugin from here and put in to a directory and we will use this during terraform initialization.
Now a days i start using Microsoft Visual Studio Code to write my automation scripts. It has inbuilt terraform plugin to highlight/validate code which makes it way more simple, then working with different text editors and multiple windows. It’s a free download, check it out or you are free to use any other text editor of your choice.
Create Terraform files:
- Create a new folder
- Create two terraform files (terraform.tfvars & main.tf) as below and put the below content in to the files
- Save both the files in to the folder which we created in to step1
- terraform.tfvars:–
- This is where we give value to the variables. For example vcd_url = “https://vcd-01a.corp.local/api”, this means that anytime var.vcd_url is referenced in the “main.tf file” it will reference back to . Most variables below are self-explanatory.
-
# vCloud Director Connection Variables vcd_user = "administrator" vcd_pass = "VMware1!" vcd_url = "https://vcd-01a.corp.local/api" vcd_max_retry_timeout = "60" vcd_allow_unverified_ssl = "true"
- main.tf – This will have actual actionable code which will perform action on to vCloud Director.We do this by using a provider and multiple resources. The provider we are using in this demonstration is “vcd”. The resources are then responsible for different parts of vCloud Director. In this example “vcd_org” is going to create, modify or delete an Org. we are created new VCD Organisation.
-
variable "vcd_user" { description = "vCloud user" } variable "vcd_pass" { description = "vCloud pass" } variable "vcd_allow_unverified_ssl" { default = true } variable "vcd_url" {} variable "vcd_max_retry_timeout" { default = 60 } # Connect VMware vCloud Director Provider provider "vcd" { user = var.vcd_user password = var.vcd_pass org = "System" url = var.vcd_url max_retry_timeout = var.vcd_max_retry_timeout allow_unverified_ssl = var.vcd_allow_unverified_ssl } #Create a new org names "T3" resource "vcd_org" "org-name" { name = "T3" full_name = "My organization" description = "The pride of my work" is_enabled = "true" delete_recursive = "true" delete_force = "true" }
-
Initialize terraform plugin:
The terraform “init” command is used to initialize a working directory containing Terraform configuration files. This is the first command that you should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times.this command will never delete your existing configuration or state.
During init, Terraform searches the configuration for both direct and indirect references to providers and attempts to load the required plugins. For providers distributed by HashiCorp, init will automatically download and install plugins if necessary.
Since on my windows init didn’t downloaded plugin for some reason , so i have downloaded vCD plugin in above steps and during initialization i am pointing towards my directory and it says terraform will use vCloud Director Plug-in version 2.6.
Terraform Plan:
The terraform “plan” command is a convenient way to check whether the execution plan for a set of changes matches your expectations without making any changes to real resources or to the state.
“Terraform plan” command will check above created files and check what changes it needs to do on the environment and will give you summary of tasks.
Terraform Apply
The “terraform apply” command is used to apply the changes required to reach the desired state of the configuration, or the pre-determined set of actions generated by a terraform plan execution plan.
Org Created Successfully:
above step created an organisation in to my vCloud Director environment and it took only few minutes.
lots of development is happening on terraform provider for vCloud Director which will help VMware Cloud provider to automate repeated tasks. i will continue to add few more blog articles on this topic, stay tuned….
Pingback: Onboard Tenants on Cloud Director in less than 5 Minutes using vCD Terraform Provider | VMTECHIE
Pingback: Terraform vCloud Director Provider v2.7.0 and Remote Work
Pingback: Terraform vCloud Director Provider v2.7.0 et travail à distance – Actu générale
Pingback: Quick & Easy Tenant OnBoarding using Cloud Director Terraform Provider | VMTECHIE