项目作者: mohsinali-eng

项目描述 :
Tools to automate virtual machine hosted in VMware vSphere
高级语言: Python
项目地址: git://github.com/mohsinali-eng/vmautomation.git
创建时间: 2017-06-24T19:27:18Z
项目社区:https://github.com/mohsinali-eng/vmautomation

开源协议:MIT License

下载


VM Automation

Overview

This tool will help you to automate different operations in virtual machine
located in VMware vSphere.

Supported Features

Currently support different actions like

  • Create Virtual Machine
  • Clone Virtual Machine from a template
  • Delete an existing virtual machine
  • Power on a virtual machine
  • Power off a virtual machine
  • Reset a virtual machine

Demo

vmautomation demo

Install required packages

To start installation you need pip

  1. wget -qO- https://bootstrap.pypa.io/get-pip.py | python

To Install required python packages based on requirements.txt

  1. python -m pip install -r requirements.txt

Documentation

Python Program

Let’s perform different operations based on .json file
>
Program will ask for password if not specified
>

Create:

Let’s create a new virtual machine in VMware vSphere using create.json.
Define virtual machine’s configuration into sample_json/create.json file then run this command

  1. python vmautomation --host=<hostname> --username=<username> --password=<password> create --json-file sample_json/create.json

Clone:

Let’s clone a virtual machine from a template in VMware vSphere using clone.json.
Define virtual machine’s configuration into sample_json/clone.json file then run this command

  1. python vmautomation --host=<hostname> --username=<username> --password=<password> clone --json-file sample_json/clone.json

Power on:

Let’s power on an existing virtual machine

  1. python vmautomation --host=<hostname> --username=<username> --password=<password> power-on --vm-name=<virtual_machine_name>

Power off:

Let’s power off an existing virtual machine

  1. python vmautomation --host=<hostname> --username=<username> --password=<password> power-off --vm-name=<virtual_machine_name>

Reset:

Let’s reset an existing virtual machine

  1. python vmautomation --host=<hostname> --username=<username> --password=<password> reset --vm-name=<virtual_machine_name>

Delete:

Let’s delete an existing virtual machine

  1. python vmautomation --host=<hostname> --username=<username> --password=<password> delete --vm-name=<virtual_machine_name>

Using python APIs

>
You can also add vmautomation into your python site-packages.
So that you can easily import it into your python script.
>

  1. #!/usr/bin/env python
  2. from vmautomation import virtual_machine
  3. # Make sure you are passing logging object based on python logging module
  4. ########################
  5. ### Create operation ###
  6. ########################
  7. # To create virtual machine object
  8. virtual_machine_obj = VirtualMachine(host=<hostname>, username=<username>,
  9. password=<password>, port=<port>,
  10. logger=<logging_obj>,ssl_check=<ssl_check>,
  11. vm_name=<vm_name>)
  12. # To set datacenter object
  13. virtual_machine_obj.set_datacenter_obj(datacenter=<datacenter_name>)
  14. # To set datastore object
  15. virtual_machine_obj.set_datastore_obj(datastore=<datastore_name>)
  16. # To set resource pool object
  17. virtual_machine_obj.set_resource_pool_obj(resource_pool=<resource_pool_name>)
  18. # To set folder object
  19. virtual_machine_obj.set_folder_obj(folder=<folder_name>)
  20. # Virtual Machine Create operation
  21. virtual_machine_obj.create(memory_in_MB=<memory_in_megabytes>,
  22. num_of_CPUs=<num_of_cpus>,
  23. guest_OS_id=<guest_os>,
  24. version=<virtual_machine_version>)
  25. ### To add/reconfigure Virtual Machine ###
  26. # To add a new hard drive
  27. virtual_machine_obj.add_hard_disk(disk_label=<label_for_new_hard_drive>),
  28. capacityin_KB=<disk_capacity_in_kilobytes>)
  29. # To add a new CDROM
  30. # To add iso file you have to specify iso datastore and iso file name
  31. # Otherwise put it as None, then will use client CDROM
  32. iso_file_name = "[{0}] {1}".format(<iso_datastore_name>, <.iso_filename>)
  33. virtual_machine_obj.add_cdrom(iso_file_name,
  34. startConnected=<is_connected_from_startup>)
  35. # To add a new Network Card
  36. # Here if you specify mac address then put mac_address_type as manual
  37. virtual_machine_obj.add_network_card(mac_address=<mac_address>,
  38. network_label=<network_card_label>,
  39. mac_address_type=<manual_or_assigned>,
  40. connected=<is_connected_from_startup>,
  41. summary=<summary_of_network_card>)
  42. ##############################
  43. ### Clone VM from template ###
  44. ##############################
  45. # To create virtual machine object
  46. virtual_machine_obj = VirtualMachine(host=<hostname>, username=<username>,
  47. password=<password>, port=<port>,
  48. logger=<logging_obj>,ssl_check=<ssl_check>,
  49. vm_name=<vm_name>)
  50. # To set datacenter object
  51. virtual_machine_obj.set_datacenter_obj(datacenter=<datacenter_name>)
  52. # To set datastore object
  53. virtual_machine_obj.set_datastore_obj(datastore=<datastore_name>)
  54. # To set resource pool object
  55. virtual_machine_obj.set_resource_pool_obj(resource_pool=<resource_pool_name>)
  56. # To set folder object
  57. virtual_machine_obj.set_folder_obj(folder=<folder_name>)
  58. # To set template object
  59. virtual_machine_obj.clone_from_template(template_name=<template_name>)
  60. # To update mac address to a specific network card
  61. virtual_machine_obj.update_mac_address(nic_hdw_name=<network_card_name>,
  62. new_mac_address=<new_mac_address>)
  63. # To update network label for a specific network card
  64. virtual_machine_obj.update_network_label(nic_hdw_name=<network_card_name>,
  65. new_network_label=<new_network_label>)
  66. # To update network state for a specific network card
  67. virtual_machine_obj.update_nic_state(nic_hdw_name=<network_card_name>,
  68. is_connected=<network_card_is_connected>)
  69. ################################
  70. ### Power on virtual machine ###
  71. ################################
  72. # To create virtual machine object
  73. virtual_machine_obj = VirtualMachine(host=<hostname>, username=<username>,
  74. password=<password>, port=<port>,
  75. logger=<logging_obj>,ssl_check=<ssl_check>,
  76. vm_name=<vm_name>)
  77. virtual_machine.power_on()
  78. #################################
  79. ### Power off virtual machine ###
  80. #################################
  81. # To create virtual machine object
  82. virtual_machine_obj = VirtualMachine(host=<hostname>, username=<username>,
  83. password=<password>, port=<port>,
  84. logger=<logging_obj>,ssl_check=<ssl_check>,
  85. vm_name=<vm_name>)
  86. virtual_machine.power_off()
  87. #############################
  88. ### Reset virtual machine ###
  89. #############################
  90. # To create virtual machine object
  91. virtual_machine_obj = VirtualMachine(host=<hostname>, username=<username>,
  92. password=<password>, port=<port>,
  93. logger=<logging_obj>,ssl_check=<ssl_check>,
  94. vm_name=<vm_name>)
  95. virtual_machine.reset()
  96. ##############################
  97. ### Delete virtual machine ###
  98. ##############################
  99. # To create virtual machine object
  100. virtual_machine_obj = VirtualMachine(host=<hostname>, username=<username>,
  101. password=<password>, port=<port>,
  102. logger=<logging_obj>,ssl_check=<ssl_check>,
  103. vm_name=<vm_name>)
  104. virtual_machine.delete()