Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env python
# Written by Juan Manuel Rey
# Github:
# Blog:
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# vSphere Python SDK script to force the HA reconfiguration in an ESXi host
# Tested with vSphere 6.0 U1
import argparse
import getpass
import atexit
import ssl
from pyVmomi import vim
from pyVim import connect
from tools import tasks
def get_args():
Retrieve script arguments
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--host',
required=True, action='store',
help='vCenter Server address')
parser.add_argument('-o', '--port',
type=int, default=443,
action='store', help='Port to connect to')
parser.add_argument('-u', '--user',
required=True, action='store',
help='User name to use for the connection')
parser.add_argument('-p', '--password',
required=False, action='store',
help='Password to use for the connection')
parser.add_argument('-e', '--esx_host',
required=True, action='store',
help='Host to reconfigure')
args = parser.parse_args()
if not args.password:
args.password = getpass.getpass(
prompt='Enter password for user %s: ' % args.user)
return args
def main():
args = get_args()
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_NONE
service_instance = connect.SmartConnect(,
if not service_instance:
print("Unable to connect with the vCenter Server "
"using the provided credentials")
return -1
atexit.register(connect.Disconnect, service_instance)
content = service_instance.RetrieveContent()
object_view = content.viewManager.CreateContainerView(content.rootFolder,
host_list = object_view.view
for host in host_list:
if == args.esx_host:
esx = host
print "Proceeding to execute operation 'Reconfigure for HA' in host %s" % \
reconf_ha = esx.ReconfigureHostForDAS_Task()
task = reconf_ha
tasks.wait_for_tasks(service_instance, [task])
print "Operation complete"
return 0
# Main execution
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment