Wednesday, December 21, 2016

vSphere's Datacenter Command-Line Interface: Getting Started with Datacenter CLI

This post was originally published on VMware vSphere Blog and I am just re-posting it here for future reference...


Datacenter Command-Line Interface (DCLI) is one of vSphere’s newest CLIs. DCLI works exclusively with the vSphere Automation API (REST API for vSphere) to provide both an interactive and scriptable mode to monitor and manage all features made available to the REST based API.
With the vSphere 6.5 release, DCLI has picked up a ton of new functionalities! DCLI can now interact with the vCenter Server Appliance (VCSA), perform VM tasks, receive environmental vSphere information, managing vSphere Tags, and work with the Content Library. This is definitely a great tool to have in the toolbox for anyone accessing a vSphere environment.


Accessing DCLI

DCLI is able to be installed on a multitude of Windows and Linux systems as part of the vSphere Command-Line Interface (vCLI). DCLI is also automatically included within the vCenter Server Appliance (VCSA) as well as being included as part of the installation process for vCenter Server on Windows.


Using DCLI

There are two ways to use DCLI, either in an interactive shell or via scripting mode. Both methods have their benefits, so it comes down to either preference or what fits the use case at the time.


Interactive Shell

DCLI’s interactive shell has several great features, such as tab complete and session history retention and the ability to find a namespace just by starting it off with the first few characters.


Accessing DCLI’s Interactive Shell

Here’s an example workflow of using DCLI’s interactive shell:
  • Start DCLI with the following options (Note: options start with a + in DCLI)
    • +server -> The vCenter name/IP to connect to
    • +skip-server-verification -> Instruct DCLI to ignore any certificate warnings
    • +interactive -> Start the interactive mode
Once in the interactive shell, you can either hit tab and see the namespaces available or start typing in the name of the object you’d like to begin viewing or working with.
The example below shows a tab complete example when working with VM objects:


Here’s an example on retrieving the VCSA’s system version and then how the session history works:  
As seen above, just by typing in the first letter of a prior command allows the user to recall that command without having to type the entire line. The history will persist through sessions as well, making this a very nice feature!


Scripting Mode

The scripting mode allows DCLI commands to be used within scripts. These scripts can then be turned into more extensive workflows and/or scheduled tasks.
Calling DCLI commands through the scripting mode is fairly similar to interactive mode, but a server has to be specified each time. The full namespace has to be referenced as well.
Here’s an example of retrieving the VCSA’s system version and then retrieving a list of VMs:


Here is an example of a bash script to create 10 VMs on a given datastore, resource-pool and folder:
 
#!/bin/bash

COUNTER=0

while [ $COUNTER -lt 10 ]; do

echo Creating LoopVM$COUNTER

dcli com vmware vcenter vm create --name LoopVM$COUNTER --guest "SLES_12_64" --resource-pool "resgroup-9" --folder "group-v7" --datastore "datastore-12"

let COUNTER=COUNTER+1

done

bold=$(tput bold)

normal=$(tput sgr0)

echo ${bold}--- VM List ---${normal}

dcli com vmware vcenter vm list

Output Formatting

DCLI also has the ability to change how the output from commands are handled. The formatter allows users to specify outputs of CSV, HTML, JSON, Simple, Table, or XML.
Here’s an example of using the formatter by showing a list of the hosts and then specifying the “+formatter json” parameter to receive the same output in JSON:


Credential Store


One of the other big capabilities of DCLI is storing credentials with the Credential Store. This streamlines the process of connecting to the same vCenter repeatedly, and even more so when working in scripting mode.
The first time a user connects to a vCenter server, they will be prompted to store the credentials to the credstore. After that initial save, the credstore will be referenced with each connection to that server. If needed, credentials can be specified at the command line to over-ride the values stored in the credstore.
Users can also work with the credstore to either populate credentials with the “+credstore-add +username user” parameter, remove credentials with the “+credstore-remove +user user +server servername.fqdn”, and list what credentials already exist with the “+credstore-list” parameter.
Here’s an example output from using the “+credstore-list” parameter within an interactive mode session:
Retrieving a list of credentials from DCLI's credstore


Demonstration

Here’s a demonstration video showing DCLI’s interactive mode in action! The video details connecting to a VCSA, entering DCLI’s interactive mode, displaying some general environmental information as well as pointing out some of the benefits when using interactive mode, and lastly it creates a VM and shows all the important information about that new VM.
Pay close attention to that VM creation command, there’s a sneak peak of an upcoming post in this series on DCLI.


Conclusion

DCLI has, relatively, flown under the radar up until this point. Interactive mode allows even brand new users to easily access and run commands within moments of connecting and without knowin the name of the name spaces or reading documentation. Scripting mode lets users have those same great commands available outside of the interactive shell and utilize them in a standalone basis or within a scheduled task or cron job. These commands can easily be used with the formatter option to produce a variety of outputs and allow quick integration into other products.

DCLI is a great and intuitive CLI that should be added to anyone’s toolbox!
For more information on getting started with DCLI, see the DCLI Reference.

That's it... :)



No comments:

Post a Comment