Milestone 4: pyVmomi
Repo for the code used in this milestone Link
Deliverable 1. Figure out how to clone your course git repository. Open the base directory in visual studio code(1), and cover in your video demo a view similar to the one below that also shows a terminal with your mgmt1 IP address(2)
Deliverable 2. Demo your own interactive session with vcenter via pyvmomi. Print out an element of the aboutInfo object. Similar to the one below.
import getpass
passw = getpass.getpass()
from pyVim.connect import SmartConnect
import sll
s=ssl.SSLContext(ssl.PROTOCOL_TLSv1_)
s.verify_mode=ssl.CERT_NONE
si=SmartConnect(host="vcenter.paul.local", user="paul.gleason-adm", pwd=passw, SSLContext=s)
aboutInfo=si.conntent.about
print(aboutInfo)
print(aboutInfo.fullName)
Deliverable 3. Provide a link to a short video hosted on googledrive or panopto that clearly demonstrates that you’ve met the following requirements. Ensure you share this video with your instructor. Your video must be captured at 1080p or better and must have voice. Talk through the demonstration of deliverables 1 and 2 and your python program. Provide a very brief walkthrough of the python code you wrote in the execution of this milestone.
Requirement 1: Read your vmware username and vcenter hostname from a file.
file = configparser.ConfigParser()
file.read('creds.ini')
Hostinfo = file['SERVERINFO']['host']
Userinfo = file['SERVERINFO']['user']
Passinfo = file['SERVERINFO']['password']
vCenterIP = file['SERVERINFO']['vcenter_ip']
Requirement 2: Provide data from your current pyvmomi session. This must include DOMAIN/username, vcenter server and your source IP address. Update: Pull the domain/username and source ip from the connection information and vcenter server can be the variable you read from your configuration file. See the following video for debugging advice.
si = SmartConnect(host=Hostinfo, user=Userinfo, pwd=Passinfo, SSLContext=s)
current_session = si.content.sessionManager.currentSession
print(f"Current Session: \nUser Name: {current_session.userName} \nSource IP: {current_sesession.ipAddress} \nvCenter IP: {vCenterIP} \n")
Requirement 3: Create a search function that filters all vms in vcenter by name. If no filter is added, return all VMs
vmfolder = si.content.rootFolder.childEntity[0].vmFolder.childEntity
for vcenter_object in vmfolder:
if type(vcenter_object) == vim.Folder:
for vm in vcenter_object.childEntity:
if vm_name:
if vm.name == vm_name:
print_vm(vm)
else:
print_vm(vm)
else:
if vm_name:
if vcenter_object.name == vm_name:
vm = vcenter_object
print_vm(vm)
else:
vm = vcenter_object
print_vm(vm)
How this works is the the first if statement under the for loop is looking for the type of the object if the type is a folder it drops into another for loop to check for vms in that folder.
Requirement 4: For each VM returned, provide the following meta-data VM Name Power State Number of CPUs Memory in GB IP Address (don’t forget to add vmware tools to pf and any other “on” system that does not have an IP address returned.
vmfolder = si.content.rootFolder.childEntity[0].vmFolder.childEntity
for vcenter_object in vmfolder:
if type(vcenter_object) == vim.Folder:
for vm in vcenter_object.childEntity:
if vm_name:
if vm.name == vm_name:
print_vm(vm)
else:
print_vm(vm)
else:
if vm_name:
if vcenter_object.name == vm_name:
vm = vcenter_object
print_vm(vm)
else:
vm = vcenter_object
print_vm(vm)
Both of the else statements are made to print out just all vms.