Example 1 - Osirium/vcdriver GitHub Wiki

Setting up vcdriver

Managing Vsphere programatically is a delicate task. It involves dealing with critical credentials and permissions, both for humans and bots. With this in mind, there is a configuration engine based on four layers, which we will describe ordered by priority (Lowest priority first):

  1. No configuration: When no configuration is provided, user input will be required when necessary. This the default fallback behaviour.
>>> from vcdriver.session import connection
>>> connection()
vcdriver_host: my.vsphere.host
vcdriver_username: admin
vcdriver_password: 
Vcenter session opened with ID 52d75977-360d-0923-39ff-b914fd323d61
'vim.ServiceInstance:ServiceInstance'
  1. Environment: You can provide configuration values using environment variables, which might be convenient for secrets. You then need to call the load function explicitly.
>>> import os
>>> os.environ['vcdriver_host'] = 'my.vsphere.host'
>>> os.environ['vcdriver_username'] = 'admin'
>>> os.environ['vcdriver_password'] = 'pass'
>>> from vcdriver.session import connection
>>> from vcdriver.config import load
>>> load()
>>> connection()
Vcenter session opened with ID 52d75977-360d-0923-39ff-b914fd323d61
'vim.ServiceInstance:ServiceInstance'
  1. INI file: An INI file with the following format can also be used.
[Vsphere Session]
vcdriver_host = my.vsphere.host
vcdriver_port = 443
vcdriver_username = admin
vcdriver_password = 

[Virtual Machine Deployment]
vcdriver_resource_pool = MyPool
vcdriver_data_store = MyDisk
vcdriver_data_store_threshold = 20
vcdriver_folder = MyFolder

[Virtual Machine Remote Management]
vcdriver_vm_ssh_username = ssh_user
vcdriver_vm_ssh_password = ssh_password
vcdriver_vm_winrm_username = winrm_username
vcdriver_vm_winrm_password = winrm_password

Some of the configuration values will be explained in the following sections. It's worth mentioning that if any value is empty in this INI file, the configuration load function will check the environment before falling back to the user input.

>>> import os
>>> os.environ['vcdriver_password'] = 'pass'
>>> from vcdriver.session import connection
>>> from vcdriver.config import load
>>> load('path/to/vcdriver.ini')
>>> connection()
Vcenter session opened with ID 52d75977-360d-0923-39ff-b914fd323d61
'vim.ServiceInstance:ServiceInstance'
  1. Argument injection: You can also pass the configuration values as function parameters directly.
>>> from vcdriver.session import connection
>>> connection(vcdriver_host='my.vsphere.host', vcdriver_username='admin', vcdriver_password='pass')
Vcenter session opened with ID 52d75977-360d-0923-39ff-b914fd323d61
'vim.ServiceInstance:ServiceInstance'