vSphere Tags are used to add attributes to VMs so that they can be used to help categorise VMs for further filtering or discovery. vSphere Tags have a number of use cases of which Melissa has a great blog post here on the power of vSphere Tags, their configuration and their application. Veeam fully supports the use of vSphere Tags when configuring Backup or Replication Jobs. The use of tags essentially transforms static jobs into dynamic policy based management for backup and replication.

Once a job is set to build its VM inventory from Tags there is almost no need to go back and amend the job settings to cater for VMs that are added or removed from vCenter. . Shown above, I have a Tag Category configured with two tags that are used to set a VM to be included or excluded in the backed job. Every time the job is run it will source the VM list based on these policy elements resulting in less management overheads and as a way to capture changes to the VM inventory.

vSphere Tags with Terraform:

I’ve been deploying a lot of lab VMs using Terraform of late. The nature of these deployments means that VMs are being created and destroyed often. I was finding that VMs that should be backed up where not being backed up, while VMs that shouldn’t be backed up where being backed up. This also leads to issues with the backup job…an example was this week, when I was working on my Kubernetes vSphere Terraform project.

The VMs where present at the start of the backup, but during the window the VMs had been destroyed leaving the job in an error state. These VMs being transient in nature should never have been part of the job. With the help of the tags I created above I was able to use Terraform to assign those tags to VMs created as part of the plan.

With Terraform you can create Tag Categories and Tags as part of the code. You can also leverage existing Tag Categories and Tags and feed that into the declarations as variables. For backup purposes, every VM that I create now has one of the two tags assigned to it. Outside of Terraform, I would apply this from the Web Client or via PowerShell, but the idea is to ensure a repeatable, declarative VM state where any VM created with Terraform has a tag applied.

Terraform vSphere Tag Configuration Example:

First step is to declare two data sources somewhere in the TF code. I typically place these into a main.tf file.

data "vsphere_tag_category" "category" { name = "${var.vsphere_tag_category}" } data "vsphere_tag" "tag" { name = "${var.vsphere_tag_name}" category_id = "${data.vsphere_tag_category.category.id}" } 1 2 3 4 5 6 7 8 data "vsphere_tag_category" "category" { name = "${var.vsphere_tag_category}" } data "vsphere_tag" "tag" { name = "${var.vsphere_tag_name}" category_id = "${data.vsphere_tag_category.category.id}" }

We have the option to hard code the names of the Tag and Tag Category in the data source, but a better way is to use variables for maximum portability.

The terraform.tfvars file is where we can set the variable

# VM specifications vsphere_datacenter = "VC03" vsphere_vm_folder = "TPM03-AS" ... ... vsphere_tag_category ="TPM03" vsphere_tag_name ="TPM03-NO-BACKUP" 1 2 3 4 5 6 7 # VM specifications vsphere_datacenter = "VC03" vsphere_vm_folder = "TPM03-AS" . . . . . . vsphere_tag_category = "TPM03" vsphere_tag_name = "TPM03-NO-BACKUP"

We also need to created a corresponding entry in the variables.tf

variable "vsphere_tag_category" { description = "vSphere Tag Catagory Details" } variable "vsphere_tag_name" { description = "vSphere Tag Details" } 1 2 3 4 5 6 variable "vsphere_tag_category" { description = "vSphere Tag Catagory Details" } variable "vsphere_tag_name" { description = "vSphere Tag Details" }

Finally we can set the tag information in the VM .tf file that references the data sources, that in turn reference the variables that have been configured.

# Create a vSphere VM in the folder # resource "vsphere_virtual_machine" "TPM03-K8-MASTER" { # VM placement # name = "${var.vsphere_vm_name}" resource_pool_id = "${data.vsphere_resource_pool.resource_pool.id}" datastore_id = "${data.vsphere_datastore.datastore.id}" folder = "${var.vsphere_vm_folder}" tags = ["${data.vsphere_tag.tag.id}"] 1 2 3 4 5 6 7 8 # Create a vSphere VM in the folder # resource "vsphere_virtual_machine" "TPM03-K8-MASTER" { # VM placement # name = "${var.vsphere_vm_name}" resource_pool_id = "${data.vsphere_resource_pool.resource_pool.id}" datastore_id = "${data.vsphere_datastore.datastore.id}" folder = "${var.vsphere_vm_folder}" tags = [ "${data.vsphere_tag.tag.id}" ]

The Result:

Once the Terraform plan has been applied and the VMs created the Terraform State file will contain references to the tags and the output from the running of the plan will show it assigned to the VM.

The Tag will be assigned to the VM and visible as an attribute in vCenter.

Any Veeam Backup Job that is configured to use Tags will now dynamically add or exclude VMs created by Terraform plan. In the case above, the VM has the TPM03-NO-BACKUP tag assigned which means it will be part of the exclusion list for the backup job.

vSphere Tags are an excellent way to configure policy based backup and replication jobs through Veeam. Terraform is great for deploying infrastructure in a repeatable, declarative way. By having Terraform assign Tags to VMs as they are deployed allows us to control whether a VM is included or excluded from a backup policy. If deploying VMs from Terraform, take advantage of vSphere Tags and have them as part of your deployments.

References:

https://www.terraform.io/docs/providers/vsphere/r/tag.html

Share this: Twitter

LinkedIn

Reddit

WhatsApp



Like this: Like Loading...