I have a task that happens every two weeks where we clone a back end volume (NetApp) and export it back into vCenter and re-register the VM's (think restoring vm's from a "gold" copy). The problem is I have several tags assigned to these "gold" vm's that are lost during the restore/clone process. I've even tried taking a snapshot in hopes of persisting the tags but no luck. Does anyone have any experience with this and know of a way to persist a tag until I explicitly remove it?
Correct me if I my understating is wrong
1. You have a Gold VM (base VM) with several tags attached.
2. Now when you clone that Gold VM, you do not see any tags assigned to cloned VM.
Based on your response, I will try in my environment.
You're sort of correct.
We have several gold/base vm's that reside on a gold datastore. The datastore is seen as a gold volume in NetApp. Likewise, we have exact replicas of the gold vms that we call live vm's where active work takes place. We like to refresh these vm's every couple of weeks by doing the following:
What I am finding is that searching for VM's using Tags is MUCH faster and would very much like to capitalize on that. But I'm noticing that when I register these new live vm's, any tags that exist(ed) on the gold vm are now missing.
Does that clear things up?
Tags only do exist in vCenter inventory / DB. When you apply a tag to a VM, it does not get written to vmx file etc. Only a record is made in VC database that associates that object (VM in our case) with a specific tag.
When you register a VM from files on datastore, from vCenter's perspective it is a new object that so far has no references to any tags in VC DB.
If your environment does not change a lot you could probably write up a script in PowerCLI or your favorite programming language to assign specific tags to specific VMs and run that script as part of step 6.
Hope this helps.
Hi TravisRoberts,
As you have discovered, this is the default behavior when re-registering a VM (tags will be lost). You will need to backup the tags before removing the VMs from inventory.
Example - How to save your tags
Get-VM myVM | Select Name,@{N="Tags";E={((Get-TagAssignment -Entity $_ | select -ExpandProperty Tag).Name -join ",")}} | Export-Csv -NoTypeInformation c:\temp\myTags.csv
Example - Assign Tags from the CSV you saved previously:
## AssignVMTags.ps1
Write-Host "`nThis script assigns vSphere tags to one or more VMs" -ForegroundColor Yellow
$inputfile = Read-Host "Enter path to your Tag Assignment CSV"
foreach ($row in (Import-Csv $inputfile) | Where-Object { $_ -ne "Name" }) {
$vmName = $row.Name
$vmTags = $row.Tags
If (Get-View -ViewType virtualmachine -Filter @{ "Name" = "$($vmName)"; "Config.Template" = "True" }) {
$vm = Get-Template -Name $vmName
}
Else {
$vm = Get-VM -Name $vmName
}
foreach ($vmTagName in ($vmTags.Split(","))) {
Write-Host "..Assigning the $($vmTagName) tag to $($vm.name)" -ForegroundColor Green
$vmTag = Get-Tag -Name $vmTagName
New-TagAssignment -Tag $vmTag -Entity $vm | Out-Null
}
}
