Automation

 View Only
  • 1.  Tags disappear after cloning a vm?

    Posted Feb 10, 2016 09:01 PM

    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?



  • 2.  RE: Tags disappear after cloning a vm?

    Broadcom Employee
    Posted Feb 11, 2016 04:21 AM

    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.



  • 3.  RE: Tags disappear after cloning a vm?

    Posted Feb 11, 2016 03:53 PM

    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?



  • 4.  RE: Tags disappear after cloning a vm?

    Posted Feb 11, 2016 04:32 PM

    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.



  • 5.  RE: Tags disappear after cloning a vm?

    Posted Feb 17, 2016 06:13 PM

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