govmomi is a “Go” library for interacting with VMware vSphere APIs (ESXi and/or vCenter) and it’s built using VMware vSphere SDK. govmomi project has more than 1000 GitHub stars and used by many opensource projects including terraform, Kubernetes, kops etc. govc is a vSphere CLI built on top of govmomi. It’s written in Go language and pre-compiled for Linux, OSX, and windows. govc vSphere CLI is very useful to carry out various vCenter/vSphere operation from the command line. It’s a very user-friendly CLI which can be used along with shell scripting for automation tasks. This article will walk you through how to download and use it for your vSphere environment.
It’s time to say bye for vSphere PowerCLI. Let’s give a try on “GOVC”
Download govc:
- Download the file relevant to your operating system (https://github.com/vmware/govmomi/releases)
2. I have downloaded govc_linux_amd64.gz for RHEL 7/CentOS server. Created a new directory and downloaded the Linux govc binary.
[root@uaweb1 ~]# mkdir vsphereCLI [root@uaweb1 ~]# cd vsphereCLI/ [root@uaweb1 vsphereCLI]# wget https://github.com/vmware/govmomi/releases/download/v0.20.0/govc_linux_amd64.gz --2019-06-20 04:11:41-- https://github.com/vmware/govmomi/releases/download/v0.20.0/govc_linux_amd64.gz Resolving github.com (github.com)... 140.82.118.3 Connecting to github.com (github.com)|140.82.118.3|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/Signature=dc74aa95e732c1ffc98e6e896142fe4103480Dgovc_linux_amd64.gz&response-content-type=application%2Foctet-stream [following] --2019-06-20 04:11:42-- https://github-production-release-asset-2e65be.s3.amazonaws.com/22883982/df677280-2a5b-11e9-8d82-06eea35b4145?X-Amz-AlgSignedHeaders=response-et-2e65be.s3.amazonaws.com)... 52.216.85.147 Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.85.147|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 7650188 (7.3M) [application/octet-stream] Saving to: ‘govc_linux_amd64.gz’ 100%[================================================================================>] 7,650,188 509KB/s in 20s 2019-06-20 04:12:03 (374 KB/s) - ‘govc_linux_amd64.gz’ saved [7650188/7650188] [root@uaweb1 vsphereCLI]#
3. Unarchive the compressed govc binary. Create a soft link in the command search path for easy execution.
[root@uaweb1 vsphereCLI]# ls -lrt total 7472 -rw-r--r-- 1 root root 7650188 Feb 6 22:09 govc_linux_amd64.gz [root@uaweb1 vsphereCLI]# gunzip govc_linux_amd64.gz [root@uaweb1 vsphereCLI]# ls -lrt total 31688 -rw-r--r-- 1 root root 32447221 Feb 6 22:09 govc_linux_amd64 [root@uaweb1 vsphereCLI]# pwd /root/vsphereCLI [root@uaweb1 vsphereCLI]# ln -s /root/vsphereCLI/govc_linux_amd64 /usr/local/bin/govc [root@uaweb1 vsphereCLI]# govc version govc 0.20.0 [root@uaweb1 vsphereCLI]#
4. Configure the user environment with GOVC environment variable. I have updated my user’s “.bash_profile” to load the GOVC environment variables.
- GOVC_USERNAME: vCenter Username/ ESXi Username
- GOVC_PASSWORD: vCenter Password/ ESXi Password
- GOVC_URL: vCenter/ESXi SDK URL
- GOVC_INSECURE: Disable/Enable certificate verification
[root@uaweb1 ~]# cat .bash_profile |grep GOVC export GOVC_URL=https://192.168.2.212 export GOVC_USERNAME=administrator@vsphere.local export GOVC_PASSWORD=password@123 export GOVC_INSECURE=true [root@uaweb1 ~]#
If you do not want to store the user’s credentials in plain text, you can execute the GOVC environment commands when you needed.
export GOVC_URL="https://192.168.2.212" export GOVC_USERNAME="administrator@vsphere.local" export GOVC_PASSWORD="password@123" export GOVC_INSECURE="true"
5. Let’s make the API call to vSphere environment using govc.
Listing all the datacenter vCenter objects.
[root@uaweb1 ~]# govc ls /SECUA/vm /SECUA/network /SECUA/host /SECUA/datastore [root@uaweb1 ~]#
List the host. If the Cluster is configured , it will just show the clustername.
[root@uaweb1 ~]# govc ls /SECUA/host /SECUA/host/SECCLS [root@uaweb1 ~]#
List all the cluster resources.
[root@uaweb1 ~]#govc ls /SECUA/host/SECCLS /SECUA/host/SECCLS/Resources /SECUA/host/SECCLS/192.168.2.65 /SECUA/host/SECCLS/192.168.2.66 [root@uaweb1 ~]#
6. We could also pass esxcli command using govc.
[root@uaweb1 ~]# govc host.esxcli -host=192.168.2.66 network vswitch standard portgroup list Name Virtual Switch Active Clients VLAN ID ---- -------------- -------------- ------- Management Network vSwitch0 1 0 VLAN-160 vSwitch0 0 160 VM Network vSwitch0 0 0 [root@uaweb1 ~]#
7. Here is the list of govc commands/usage.
[root@uaweb1 ~]# govc Usage of govc: about about.cert cluster.add cluster.change cluster.create cluster.group.change cluster.group.create cluster.group.ls cluster.group.remove cluster.override.change cluster.override.info cluster.override.remove cluster.rule.change cluster.rule.create cluster.rule.info cluster.rule.ls cluster.rule.remove datacenter.create datacenter.info datastore.cluster.change datastore.cluster.info datastore.cp datastore.create datastore.disk.create datastore.disk.inflate datastore.disk.info datastore.disk.shrink datastore.download datastore.info datastore.ls datastore.mkdir datastore.mv datastore.remove datastore.rm datastore.tail datastore.upload datastore.vsan.dom.ls datastore.vsan.dom.rm device.boot device.cdrom.add device.cdrom.eject device.cdrom.insert device.connect device.disconnect device.floppy.add device.floppy.eject device.floppy.insert device.info device.ls device.remove device.scsi.add device.serial.add device.serial.connect device.serial.disconnect device.usb.add disk.create disk.ls disk.register disk.rm disk.snapshot.create disk.snapshot.ls disk.snapshot.rm disk.tags.attach disk.tags.detach dvs.add dvs.create dvs.portgroup.add dvs.portgroup.change dvs.portgroup.info env events export.ovf extension.info extension.register extension.setcert extension.unregister fields.add fields.info fields.ls fields.rename fields.rm fields.set find firewall.ruleset.find folder.create folder.info guest.chmod guest.chown guest.download guest.getenv guest.kill guest.ls guest.mkdir guest.mktemp guest.mv guest.ps guest.rm guest.rmdir guest.run guest.start guest.touch guest.upload host.account.create host.account.remove host.account.update host.add host.autostart.add host.autostart.configure host.autostart.info host.autostart.remove host.cert.csr host.cert.import host.cert.info host.date.change host.date.info host.disconnect host.esxcli host.info host.maintenance.enter host.maintenance.exit host.option.ls host.option.set host.portgroup.add host.portgroup.change host.portgroup.info host.portgroup.remove host.reconnect host.remove host.service host.service.ls host.shutdown host.storage.info host.storage.mark host.storage.partition host.vnic.info host.vnic.service host.vswitch.add host.vswitch.info host.vswitch.remove import.ova import.ovf import.spec import.vmdk license.add license.assign license.assigned.ls license.decode license.label.set license.ls license.remove logs logs.download logs.ls ls metric.change metric.info metric.interval.change metric.interval.info metric.ls metric.reset metric.sample object.collect object.destroy object.method object.mv object.reload object.rename option.ls option.set permissions.ls permissions.remove permissions.set pool.change pool.create pool.destroy pool.info role.create role.ls role.remove role.update role.usage session.login session.logout session.ls session.rm snapshot.create snapshot.remove snapshot.revert snapshot.tree sso.service.ls sso.user.create sso.user.id sso.user.ls sso.user.rm sso.user.update tags.attach tags.attached.ls tags.category.create tags.category.info tags.category.ls tags.category.rm tags.category.update tags.create tags.detach tags.info tags.ls tags.rm tags.update task.cancel tasks vapp.destroy vapp.power version vm.change vm.clone vm.console vm.create vm.destroy vm.disk.attach vm.disk.change vm.disk.create vm.guest.tools vm.info vm.ip vm.keystrokes vm.markastemplate vm.markasvm vm.migrate vm.network.add vm.network.change vm.option.info vm.power vm.question vm.rdm.attach vm.rdm.ls vm.register vm.unregister vm.upgrade vm.vnc [root@uaweb1 ~]#
8. To know more about a specific command, use help. (# govc command -help)
[root@uaweb1 ~]# govc vm.info -help Usage: govc vm.info [OPTIONS] VM... Display info for VM. Examples: govc vm.info $vm govc vm.info -json $vm govc find . -type m -runtime.powerState poweredOn | xargs govc vm.info Options: -cert= Certificate [GOVC_CERTIFICATE] -dc= Datacenter [GOVC_DATACENTER] -debug=false Store debug logs [GOVC_DEBUG] -dump=false Enable Go output -e=false Show ExtraConfig -g=true Show general summary -json=false Enable JSON output -k=true Skip verification of server certificate [GOVC_INSECURE] -key= Private key [GOVC_PRIVATE_KEY] -persist-session=true Persist session to disk [GOVC_PERSIST_SESSION] -r=false Show resource summary -t=false Show ToolsConfigInfo -tls-ca-certs= TLS CA certificates file [GOVC_TLS_CA_CERTS] -tls-known-hosts= TLS known hosts file [GOVC_TLS_KNOWN_HOSTS] -u=https://@192.168.2.212/sdk ESX or vCenter URL [GOVC_URL] -vim-namespace=vim25 Vim namespace [GOVC_VIM_NAMESPACE] -vim-version=6.7 Vim version [GOVC_VIM_VERSION] -vm.dns= Find VM by FQDN -vm.ip= Find VM by IP address -vm.ipath= Find VM by inventory path -vm.path= Find VM by path to .vmx file -vm.uuid= Find VM by UUID -waitip=false Wait for VM to acquire IP address [root@uaweb1 ~]#
Hope govc will be useful for quick vSphere operations. I will try to post a few more articles with many more command examples with a different switch.
Share it! Comment it!! Be Sociable!!!
Leave a Reply