Automation, CLIs, and developer interfaces
In this chapter, we take a look at automation, CLIs, and developer interfaces.
Welcome back to Runecast Academy. We hope you've enjoyed the series so far and maybe learned something new along the way. In this chapter, we take a look at automation, CLIs, and developer interfaces.
A few of the topics covered here include:
- If you have actions that you need to do multiple times or consistently across numerous environments, automation will make your life much easier.
- The power of tools such as ESXCLI, PowerCLI, and DCLI.
- Some of the tools available and some ways to get started.
Runecast Academy Series 1 – Part 11. Automation, CLIs, and developer interfaces
ESXCLI
So what is ESXCLI? As you might guess, based on the name, it is a command-line interface framework for vSphere, that provides a modular architecture for various components called namespaces running in the VMkernel. ESXCLI is usually used to run commands on the ESXi Shell in troubleshooting situations or when configuring things that the vSphere Client might not expose. Some common examples are esxcli system version get or esxcli network ip interface ipv4 get. Typically esxcli is executed over a remote terminal session via ssh, or locally at the host console. It is also possible to execute ESXCLI commands from PowerCLI (more on this in a moment). Try get-esxcli from within PowerCLI to get started!
PowerCLI
PowerCLI is a potent and flexible command-line tool that lets you automate many things within the aspects of vSphere Management. PowerCLI runs on Windows, macOS, and Linux using PowerShell (the latter two on Powershell Core). In addition, multiple modules allow you to extend what things you can manage, such as vSphere & VSAN, VMware Cloud Director, VMware Cloud on AWS, VMware Horizon, and NSX-T.
If you want to get started now and just try something simple, you can sample this with the below from a Powershell prompt.
Install-Module -Name VMware.PowerCLI
$credential = Get-Credential
$Username = $credential.GetNetworkCredential().username
$Password = $credential.GetNetworkCredential().password
$vCenterServer = Read-Host -Prompt 'Enter the FQDN of the vCenter Server you want to connect to. (eg. vcenter.domain.com)'
Connect-VIServer $vCenterServer -User $Username -Password $Password
Get-VM
Disconnect-VIServer $vCenterServer -Confirm:$false
You should now see a list of all the VMs in your environment. One of the most incredible things about Powershell (and, by extension, PowerCLI) is the ability to take the output of one set of commands and push it into another. This is known as “piping” because you use the pipe character “|” for this. For example, try connecting up to your vCenter Server, then run Get-VMhost. You’ll see a table with a list of all hosts connected and a little information about their status. Now try Get-VMHost | format-list. You now get more details about each host in a list format. You can even pipe this information to another command, like export-CSV. The possibilities are pretty much endless! PowerCLI is a tool that should be in the arsenal of every VMware administrator.
DCLI
DCLI (Datacenter CLI) is one of VMware’s newer command line offerings and exclusively works with the vSphere Automation API (REST API for vSphere). This allows for a scriptable way to monitor and manage everything you can do with the REST API. DCLI can be executed locally from a vCenter Server (either VCSA or Windows) or remotely from a machine with the vSphere CLI package installed. However, be aware that as of vSphere 7, the vSphere CLI is considered end-of-life.
An excellent place to get started here is this guide from Kyle Ruddy: https://blogs.vmware.com/code/2018/06/04/dcli-installation-walkthrough/
vRO
VMware vRealize® Orchestrator™ is a workflow automation tool that aids in simplifying the automation of tasks for system admins and technical users. The system is easy to use, with drag and drop building blocks that allow you to create or adjust flows initiated by a user or 3rd party systems. You can also add in scripting actions if you or a business unit requires further customization of the objects within the platform. It is another way to implement basic change management by having version history, roll back, and approvals. While historically, vRO required that workflows be built in Javascript, it now accepts Powershell, Python, and Node.js. Basic vRealize Orchestrator licensing is included when you license vCenter Server, so this is a tool that is free for most organizations.
CodeCapture
If you have got invested within PowerCLI, there may be some things that you cannot quite work out how to do straight away. Or you may want something in a particular state each time it's refreshed, such as a test or demo lab. For me, this is where CodeCapture starts to shine and also enhances your learning. You can enable this by going into the Developer Center within vSphere and turning it on under CodeCapture. Within vSphere, you should now see a nice red record button, click on this and perform the tasks you want to do, and head back here when you are done. Here is my example of setting a name on my cluster and then reviewing the code to do it:
API Explorer
The API explorer does precisely what it says on the tin. It’s an excellent graphical representation of the REST API, which you can poke around. Depending on which version of vCenter Server you are using, you can find this either by browsing to https://vcenter_fqdn/apiexplorer or in newer versions, find this within the Developer Center (alongside CodeCapture) in the vSphere Client. The bonus of this latter approach is that you’re already authenticated by virtue of being logged into the vSphere Client. The API Explorer is in Swagger format, in much the same way as the API documentation in Runecast Analyzer.
I love this because it's mainly just clicking through what you think you want, and then you click “execute” to run the code. Then, it shows you the results on-screen. As a bonus, it will even give you a curl command on how to do this if you want to embed this into other scripts.