When it comes to managing a VMware vSphere inventory, things can get crazy. Being able to determine what a virtual machine or other vSphere object actually does can be influence by any number of things from the naming convention in place, to who worked in the environment before you. The VMware tag feature built into vSphere an amazing asset to any vSphere administrator, and also often one of the most overlooked. vSphere tags can be implemented a number of ways, such managing vSphere tags by using the web client, or managing vSphere tags with PowerCLI. As we all know, there are unique challenges that come with managing a number of vSphere assets. VMware vSphere tags simplify this.
Ready to find out how VMware tags can make your life easier? Let’s start by taking a look at the two most important things you will need to be concerned with.
Introducing vSphere Tags and Categories
The two important things to think of when figuring out the best way to implement vSphere tags are the tags you are going to use along with the categories you are going to put them into. We are going to use the case of tagging virtual machines to illustrate this.
While there are a number of way to create tags in vSphere, we are going to start by right clicking the virtual machine we would like to tag, select Tags & Custom Attributes, then click Assign Tag.
When we are starting out we will not see much, so we are going to click the New tag button which is circled
Today we want to create tags based on applications. An application we work with is called Critical App, so we are going to name the tag Critical App. Then we will need to create a category, so we are going to select New Category from the drop down Category menu.
We will now be able to enter the information for our new category, which we are calling Applications since we are going to create unique tags for all of our applications.
As you can see, you can tag more than just virtual machines in your environment. You can associate a category with the following object types, or simply select All Objects.
- Content Library
- Datastore Cluster
- Distributed Port Group
- Distributed Switch
- Library Item
- Resource Pool
- Virtual Machine
Now, we have created a vSphere Tag for our application called Critical App, and created the new category of Applications. The only thing left to do is click OK to assign the VMware Tag and the category to our virtual machine.
After we have created a vSphere tag and category, and they have been assigned to a virtual machine, we will see the tag and category on the summary screen of the virtual machine. This makes it easy to tell us exactly what a virtual machine or other vSphere object does at a glance if we implement tags and categories that make sense of our environment.
If we want to view our tags and categories in the vCenter web client, we can select Tags & Custom Attributes from the Home menu. We can also add Tags and Categories from this screen.
However, we have just tagged a single virtual machine using this process. I need still to tag the rest of the virtual machines in my cluster with the Critical App tag, since they all belong to our most critical application.
vSphere Tags and PowerCLI
Since we first used the vSphere web client to create and assign vSphere tags and categories, let’s try something else. Now I am going to use PowerCLI to tag the rest of the virtual machine in my cluster with the Critical App tag. PowerCLI has a number of commands that allow us to manage vSphere Tags and categories from the command line.
First, we will take a look at the Tags we have in our vCenter. After connecting to our vCenter with the Connect-VIServer command, we are going to type
Get-Tag. This will show us the tags we have in vCenter, along with their associated categories.
Now, to apply the Critical App tag using PowerCLI to the rest of the virtual machines in my cluster named Site1-Cluster, I am going to use the command:
Get-Cluster Site1-Cluster | Get-VM | New-TagAssignment -Tag “Critical App”.
As you can see, the tag has been applied to my virtual machines with PowerCLI.
If I wanted to create new tags and categories from PowerCLI, I could also do that. First, I can use the command:
New-TagCategory -Name “Applications” -Cardinality multiple -EntityType Virtual Machine
This creates a category named Applications for Virtual Machines. You may have noticed the
-Cardinality switch. Cardinality determines how many tags you can place per object. For example, if I created a Category named Tier, and the tags Production, Development, and Test within it, I may only want to allow one tag at a time to be applied to an Virtual Machine, since a Virtual Machine cannot be in the Production and Development tier at the same time. In this case I would use the cardinality attribute of single. On the other hand, I may have want a server to be able to be tagged with multiple applications at the same time, which I do in this case, so I used the multiple attribute when creating the vSphere tag category with PowerCLI.
After creating a category, I want to create a tag within the category. I can use the command:
New-Tag -Name “Important App” -Category “Applications”
to create a new tag named Important App in the Applications category. The, I can apply the tags to my virtual machines as we did previously, once again using PowerCLI.
Assigning a vSphere Tag With PowerCLI to Any Inventory Item
Sometimes, you just quickly need to assign a tag to a single item, for example, If I just need to use PowerCLI to tag one virtual machine, I could use the following command:
New-TagAssignment -Tag "NameOfTag" -Entity VMName
The above syntax works for about anything like this:
New-TagAssignment -Tag "NameOfTag" -Entity "InventoryItem"
Your inventory item can be anything from a vSphere Cluster, to a Datastore, to a Port Group, to whatever you need. The trick is to make sure that when you set up your tag, you allow it to be assigned to all item types if that is how you decide to use it, this is especially helpful with a use case like assigning vSphere Tags to DR resources.
Why Everyone Should Use vSphere Tags
vSphere tags can be helpful in a number of cases. The use case we have mostly talked about tagging virtual machines by application. Using tags at the application level can help make day to day activities such as troubleshooting and backing up applications easier by giving us a quick overview of the components that make up an application. Another use case we briefly talked about is tagging components by Tier to quickly identify Production, Dev, and Test objects. We may want to use tags on Datastores to tag each datastore with the array name instead of giving datastores long and confusing names. The possibilities are endless with vSphere tags, and they are a great feature to help make vSphere environments easier to manage.
More vSphere Tag Resources
As you can see from these examples, vSphere tags are not difficult to use once you get the hang of it, and are a very powerful too. The best place to find more information on vSphere tags is the Official vSphere Documentation. In addition, it may make sense to start by making a list of the vSphere tags and categories you would like to implement in your environment so you can quickly and easily implement them like we just did with PowerCLI.