VMware Cloud Community
TravisRoberts
Contributor
Contributor

Tags disappear after cloning a vm?

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?

Tags (2)
0 Kudos
4 Replies
vThinkBeyondVM
VMware Employee
VMware Employee

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.


----------------------------------------------------------------
Thanks & Regards
Vikas, VCP70, MCTS on AD, SCJP6.0, VCF, vSphere with Tanzu specialist.
https://vThinkBeyondVM.com/about
-----------------------------------------------------------------
Disclaimer: Any views or opinions expressed here are strictly my own. I am solely responsible for all content published here. Content published here is not read, reviewed or approved in advance by VMware and does not necessarily represent or reflect the views or opinions of VMware.

0 Kudos
TravisRoberts
Contributor
Contributor

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:

  1. Remove Live vm's from inventory
  2. Unmount live datastore
  3. take a snapshot of the gold volume in netapp and export it to vCenter as the new live datastore
  4. Mount the new live datastore in vCenter
  5. Register the vm's as Live VM's
  6. Do a bunch of other modifications after they are registered.

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?

0 Kudos
kermic
Expert
Expert

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.

0 Kudos
grasshopper
Virtuoso
Virtuoso

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
    }
}
0 Kudos