If you have been around the VMware ecosystem for any length of time, you will be aware that PowerCLI, a set of modules for Microsoft PowerShell, has long been the tool of choice to control VMware products over the command line.
If you are a little more Linux minded or remember the ESX service console, you might also be familiar with the Perl-based vCLI commands, many of which also exist in some form on the esxcli on every ESX host.
For an overview of all the (general) current command-line options, see this page.
In this post, I will show some examples of a relatively new addition to the command-line family; Datacenter CLI, or simply DCLI.
With Powershell and vCLI already existing, what is the point of yet another command-line tool?
Kyle Ruddy, senior technical marketing architect at VMware, and leader of the VMware {code} community, explained it on the VMware Code slack recently:
The strategic thinking basically boils down to choice. Being able to provide tools so that the folks consuming our products can do it in the manner that works best for them, whether that be through an API, an SDK, or a CLI.
There’s a whole portion of people, living in the VMware eco-system, that want a CLI but won’t touch PowerShell.
We’re just trying to offer up a path of least resistance.
Beyond that, DCLI is quite a bit different from the other tools. It sits closer to the API itself, requiring you to navigate the product API namespaces directly. It is also designed to be used heavily in scripted or automated contexts.
Currently it supports talking to the vSPhere, and NSX-T API
For recent information about the last DCLI releases and the products they support, have a look at these blog posts.
https://blogs.vmware.com/vsphere/2018/06/new-release-dcli-2-9-1.html
https://blogs.vmware.com/code/2020/02/04/new-release-dcli-3/
Below are several examples I have collected using DCLI, focusing on some NSX and vSphere use cases. This post is complementary to this awesome post by Aruna Lakmal at techcrumble.net that also has some great examples.
Installing and Running DCLI in a Python Virtual Enviroment
DCLI is Python-based, and comes as a PIP package. Best-practice is often to install Python based tools in their own virtual environment.
Make virtual environment:
python3 -m venv .venv
For ease of use, you can make these 2 aliases, add them to you .bashrc
alias dvenv=’deactivate
alias venv=’source ~/.venv/bin/activate
Activate your new aliases
source .bashrc
enter your virtual enviroment
venv
Install the pip python package manager
pip install –upgrade pip
Install DCLI
pip install dcli –upgrade
Test it works
dcli –help
De-activate your virtual env
dvenv
NSX-T Examples
Logging in, in INTERACTIVE mode.
dcli +interactive +nsx-server mynsxmanager.mylab.local +skip-server-verification
In interactive mode, you get tab autocomplete with a useful menu that can be scrolled
Get overall NSX Health (interactive mode)
com vmware nsx cluster status get
Get all Load-Balencer objects, including their ID’s, size Display name, and Attached Tier-1 Router
dcli +nsx-server mynsxmanager.mylab.local +skip com vmware nsx loadbalancer services list +filter ‘results[].{“display_name”: display_name, “description”: description, “Attached Router”: attachment.target_display_name, “id”: id, “size”: size}’ +formatter table
This one-liner checks all the relevant statuses for a Load-balencer, given its ID
dcli +nsx-server mynsxmanager.mylab.local +skip com vmware nsx loadbalancer services status get –service-id 6638c1b5-ca0f-4f19-9bd1-9c6f2c4e80c1 +filter ‘{“service_status”: service_status, “virtual_server_id”: virtual_servers[].virtual_server_id, “virtual server status”: virtual_servers[].status, “pool members”: pools[].members[].ip_address, “pool member status”: pools[].members[].status }’ +formatter table
vSphere Examples
Get all VMs (up to 1000)
vcenter vm list
Get information about a single VM
vcenter vm get –vm vm-39177
This will return information in YAML format
Same command, but in scripting mode, and forcing output as JSON (this can be useful for parcing information out in a different tool, for example you might run this command from within a pipeline and use the outputs as variables elsewhere)
dcli +server https:// mynsxmanager.mylab.local +skip com vmware vcenter vm get –vm vm-39177 +formatter json
Formatting
CLI supports various format outputs and inputs
Formatting JSON output inline using JMESPath queries
The +filter option uses JMESPath json query formatting. ( http://jmespath.org/?)
There is a command-line version of this, to dynamically figure out JMESpath queries that work. You install it as follows:
pip install jmespath-terminal
Now you can pipe the JSON output of DCLI commands straight into the jpterm tool, and use it to experiment with queries on the data. Example:
dcli +nsx-server mynsxmanager.mylab.local +skip com vmware nsx loadbalancer services list +formatter json | jpterm
Official Documentation
Check out these links for the official documentation and command-line referencehttps://code.vmware.com/docs/4676/data-center-cli-reference
Latest release blog:
https://blogs.vmware.com/code/2020/02/04/new-release-dcli-3/
User Guide:
https://code.vmware.com/docs/11307/dcli-3-0-0-user-s-guide/GUID-9E9E02F5-ACF6-4796-8251-22A18D73CA81.html
DCLI Command Reference:
https://code.vmware.com/docs/4676/data-center-cli-reference
useful blogs:
https://www.techcrumble.net/2019/01/how-to-use-vmware-datacenter-cli-dcli/
http://esx-guy.blogspot.com/2019/01/the-dcli-datacenter-cli.html
The original article was posted on: thefluffyadmin.net