This script is written using VMware Powercli module and its purpose is as similar as earlier written script Powershell vCenter server Rest API create and assign tag and tagcategory for creating and modifying Tag and TagCategory, only the difference is earlier script is written using vSphere API to connect vCenter server and doing changes.
To test this script, I will apply tags to few of my clusters in the virtual Datacenter, below is the screenshot from my environment, No tags are created on to them. All the information is in csv file.
After running the script, It applies the changes from the csv file and shows nice result, new changes are shown in yellow.
In this example, I have made few changes on the few tags on the clusters, deleted one entry and modified name of the tag. Then rerun the script to sync changes with red result is modified, Yellow is new created tag and Green results means already existing tag and no changes are done.
Download this vsphere powercli tagging, it is also available on github.com/kunaludapi.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
{ Import-Module VMware.VimAutomation.Core Connect-VIServer marvel.vcloud-lab.com -User administrator@vsphere.local -Password P@55word } $csv = Import-Csv C:\temp\data.csv $uniqueTagCategory = $csv | Sort-Object -Property @{Expression='TagCategory'; Descending=$true}, Cardinality, EntityType -Unique foreach ($data in $uniqueTagCategory) { try { $newTagCategory = New-TagCategory -Name $data.TagCategory -Cardinality $data.Cardinality -EntityType $data.EntityType -ErrorAction Stop Write-Host "New TagCategory created - '$($newTagCategory.Name)'" -BackgroundColor DarkGreen } catch { $tagCategory = Get-TagCategory -Name $data.TagCategory -ErrorAction Stop $entityType = $tagCategory.EntityType -join ',' Write-Warning " Already exist - TagCategory '$($tagCategory.Name)' with Cardinality '$($tagCategory.Cardinality)' and Entitytype '$entityType'" } } foreach ($data in $csv) { $cluster = Get-Cluster $data.Cluster $currentConf = Get-TagAssignment -Entity $cluster | Where-Object {$_.Tag.Category.Name -eq $data.TagCategory} if ($currentConf.Tag.Name -eq $null) { $tag = New-Tag -Name $data.Tag -Category $data.TagCategory $tagAssignment = $cluster | New-TagAssignment -Tag $tag $message = "Cluster '{0}' assigned New tag '{1}' under TagCategory '{2}'" -f $tagAssignment.Entity.Name, $tagAssignment.Tag.Name, $tagAssignment.Tag.Category.Name Write-Host $message -ForegroundColor Yellow } elseif ($currentConf.Tag.Name -ne $data.Tag) { $replaceTag = $currentConf.Tag | Set-Tag -Name $data.Tag $message = "Cluster '{0}' current tag '{1}' replaced with '{2}' under TagCategory '{3}'" -f $replaceTag.Entity.Name, $currentConf.tag.Name, $replaceTag.Name, $replaceTag.Category.Name Write-Host $message -ForegroundColor Red } else { $message = "Cluster '{0}' is already configured with Tag '{1}' under TagCategory '{2}'" -f $currentConf.Entity.Name, $currentConf.tag.Name, $currentConf.tag.Category.Name Write-Host $message -ForegroundColor Green } } Get-Cluster $csv.Cluster | Get-TagAssignment -Category Superhero | Select-Object Entity, Tag |
Useful Articles
Getting started Ansible AWX tower for IT automation run first playbook
Ansible for VMwary Using vmware_vm_inventory dynamic inventory plugin
Ansible selectattr The error was TemplateRuntimeError no test named 'equalto'
ansible create an array with set_fact
Ansible get information from esxi advanced settings nested dictionary with unique keynames
Powershell Using vRealize Log Insight Rest API