Automate Migrating ESX Host Interfaces to Nexus 1000V
“We’ve tried, it can’t be automated!” I’ve heard this more times than I can keep track of and if you read my previous blog you will know that I just do not agree. I have written about automation with Linux utilities, UCS PowerTool, AutoHotKey, Excel, etc… 99.999% of operations can be automated. So when a customer tells me that something cannot be automated I usually respond with “Have you tried …?”
Here is the scenario; the customer has an automated build process for ESX hosts. At the point where the host is ready to be connected to the Nexus 1000V the process becomes manual. The customer would like to use VMWare PowerCLI to migrate the host interface but the Cmdlet to retrieve Distributed Virtual Switches, Get-VirtualSwitch, just returns the DVS objects, there isn’t a Cmdlet to migrate the ESX vmnic interface.
Hold on a second, I know that VCenter knows about the Nexus 1000V because I see it in the interface. I know that VCenter can manipulate the Nexus 1000V because VCenter is where interface migration is done. I am fairly certain at this point that ESX interface migration from the VCenter vSwitch to the Nexus 1000V can be automated. But what to use to do it, there is no PowerCLI command like Set-ESXHostInterfaceToN1kv. This is typically where automation ends for many, sometimes you have to dive deep into the objects that the system manages and figure out what to do. Sometimes someone has already done a deep dive into something like what you are trying to do and maybe you can build off of their work.
In this instance I used the work shown here, http://www.lucd.info/2009/10/09/dvswitch-scripting-part-1-creation, it’s a blog about migrating from the VMWare vswitch to the VMWare Distributed Virtual Switch. I thought as I read it, that there could be something here that I can work with. I poked around a bit and tested some ideas and this is what I came up with, Set-EsxVmNic.ps1
Lines 1-23 Metadata
Lines 25-43 Parameters – this script uses user and password parameters for connecting to vCenter, however a credential object can be used, up to you.
Line 45 – Connect to vCenter, since I was running the script from the PowerCLI command window I didn’t have to load the snapin but if I did I would use this code
Additionally, depending upon your environment and other PowerCLI cmdlets you are running within your script, for example Invoke-VMScript you may need to make sure the script is running in 32 bit mode. If that is the case add this code to your script to relaunch in 32 bit environment.
Back to the script.
Line 48 Get the Nexus 1000V Object for the 1000V that the ESX host vmnic is going to be migrated to.
Line 51 Get the ESX host object for the host whose vmnic is going to be migrated to the Nexus 1000V.
Line 54 Create a DVS configuration specification object.
Lines 57-58 Create a DVS Host member configuration specification and set the operation to add.
Line 61 Create a Pnic Backing object and add the to the member host configuration object.
Lines 64-66 Get the object that represents the vmnic in the ESX host and add that as a Pnic object to the Pnic Backing object. At this point there is an object for the Host that contains an object for the Pnic Backing that contains an object for the Pnic specification that has a member that specifies the Pnic itself, got it?
Line 69 Set the target host’s host member to the object reference from the ESX host object retrieved at line 51.
Line 72 Set the DVS configuration specification object host member to the target host object.
Lines 75-77 Get the current state of the Nexus 1000V and set the DVS configuration specification version to that configuration Version of the Nexus 1000V.
Line 80 Reconfigure the Nexus 1000V with the DVS configuration specification object that has been created. The task reference that is returned can be used to monitor the status of the task, refer to lucd’s blog for that bit of code.
Line 82 Disconnect from vCenter.
I should give a reference to the code formatter I used for this blog entry, it is the Source Code Beautifier at http://hilite.me/
As always I hope that helps your automation needs.