2020-07-23 23:51:32 -04:00
TL-DR
-----
On a clean VM as root
```
apt install python3-pip git
git clone https://github.com/Kodomo/esxi-vm
pip3 install paramiko pyyaml
cd esxi-vm
make install
```
then, as a normal user you setup your credentials and create a VM
```
esxi-vm-create -u -H esxi.host.local -U root -P My!Secure#Password
esxi-vm-create -n TestDebian8VM
```
the VM is fully compatible with VMWare ESXi 6.0
2017-05-03 17:00:03 -03:00
Introduction
------------
2018-01-11 14:38:39 -03:00
This utility is a simple to use command line tool to create or destroy VMs on an ESXi host from from a system running python and SSH. vCenter is **not required** . This tool is an easy way to automate building VM's from command line or from other testing or automation tools such as Bamboo, Puppet, Saltstack, or Chef.
Requirements
------------
* You must enable SSH access on your ESXi server. The VMware VIX API tools are **not required** .
* It's **highly recommenced** to use password-less authentication by copying your SSH public keys to the ESXi host, otherwise your ESXi root password could be stored in clear-text in your home directory.
* Python **3** , [paramiko ](http://www.paramiko.org ) and [PyYAML ](https://github.com/yaml/pyyaml ):
```
2020-07-23 23:51:32 -04:00
apt install python3-pip
pip3 install paramiko pyyaml
2018-01-11 14:38:39 -03:00
```
2017-05-04 09:17:09 -03:00
Usage
-----
2018-01-07 18:51:37 -03:00
* Get help using `--help` option. The defaults will be displayed in the help output.
2017-05-04 09:21:10 -03:00
2018-01-11 14:38:39 -03:00
* The only command line required parameter is the VM name (`-n`), all other command line arguments are optional.
2017-05-04 09:21:10 -03:00
2018-01-07 18:51:37 -03:00
* Defaults are stored in your home directory in `~/.esxi-vm.yml` . You can edit this file directly, or you can use the tool to update most the defaults by specifying `--updateDefaults` .
2017-05-19 08:07:34 -04:00
2018-01-07 18:51:37 -03:00
* One of the first settings to set and save as defaults is the `--Host` (`-H`), `--User` (`-U`) and `--Password` (`-P`).
2017-05-19 08:07:34 -04:00
2018-01-11 14:38:39 -03:00
* Some basic sanity checks are done on the ESXi host before creating the VM. The `--verbose` (`-V`) option will give you a little more details in the creation process. If an invalid *Disk Stores* or *Network Interface* is specified, the available devices will be shown in the error message. The tool will not show the list of available ISO images, and Guest OS types. CPU, Memory, Virtual Disk sizes are based on ESXi 6.0 limitations.
2017-05-04 09:21:10 -03:00
2018-01-07 18:51:37 -03:00
* The `--dry` (`-d`) option will go through the sanity checks, but will not create the VM.
2017-05-04 09:21:10 -03:00
2018-01-11 14:38:39 -03:00
* By default the Disk Store is set to `LeastUsed` . This will use the *Disk Store* with the most free space (in bytes).
2017-05-04 09:21:10 -03:00
2018-01-07 18:51:37 -03:00
* By default the ISO is set to `None` . Specify the full path to the ISO image. If you specify just the ISO image filename (no path), the system will attempt to find the ISO image on your DataStores.
2017-05-04 09:17:09 -03:00
2018-01-07 18:51:37 -03:00
* By default the Network set set to `None` . A full or partial MAC address can be specified. A partial MAC address argument would be 3 Hex pairs which would then be prepended by VMware's OEM `00:50:56` .
2017-05-08 12:26:25 -03:00
2018-01-07 18:51:37 -03:00
* By default the VM is powered on. If an ISO was specified, then it will boot the ISO image. Otherwise, the VM will attempt a PXE boot if a Network Interface was specified. You could customize the ISO image to specify the kickstart file, or PXE boot using COBBLER, Foreman, Razor, or your favorite provisioning tool.
2017-05-04 09:17:09 -03:00
2018-01-07 18:51:37 -03:00
* To help with automated provisioning, the script will output the full MAC address and exit code 0 on success. You can specify `--summary` to get a more detailed summary of the VM that was created.
2017-05-19 08:07:34 -04:00
2017-05-03 17:00:03 -03:00
Command Line Args
-----------------
2018-01-11 14:38:39 -03:00
```bash
2017-05-19 08:07:34 -04:00
./esxi-vm-create --help
2017-10-23 22:44:46 -03:00
2018-01-07 18:51:37 -03:00
usage: esxi-vm-create [-h] [-d] [-H HOST] [-T PORT] [-U USER] [-P PASSWORD]
[-K KEY] [-n NAME] [-c CPU] [-m MEM] [-v HDISK] [-i ISO]
[-N NET] [-M MAC] [-S STORE] [-g GUESTOS] [-o VMXOPTS]
2020-07-23 23:51:32 -04:00
[-p POOL] [-V] [--summary] [-u]
2017-05-19 08:07:34 -04:00
ESXi Create VM utility.
optional arguments:
-h, --help show this help message and exit
-d, --dry Enable Dry Run mode (False)
-H HOST, --Host HOST ESXi Host/IP (esxi)
2018-01-07 18:51:37 -03:00
-T PORT, --Port PORT ESXi Port number (22)
2017-05-19 08:07:34 -04:00
-U USER, --User USER ESXi Host username (root)
-P PASSWORD, --Password PASSWORD
ESXi Host password (*****)
2018-01-11 14:38:39 -03:00
-K KEY, --Key KEY ESXi Host path to private key ()
-n NAME, --name NAME VM name ()
2020-07-23 23:51:32 -04:00
-p POOL, --pool NAME Resource pool to attach the VM (ha-root-pool)
2017-05-19 08:07:34 -04:00
-c CPU, --cpu CPU Number of vCPUS (2)
2020-07-23 23:51:32 -04:00
-m MEM, --mem MEM Memory in MB (4096)
2017-05-19 08:07:34 -04:00
-v HDISK, --vdisk HDISK
2020-07-23 23:51:32 -04:00
Size of virt hdisk (16)
2017-05-19 08:07:34 -04:00
-i ISO, --iso ISO CDROM ISO Path | None (None)
2017-10-23 22:44:46 -03:00
-N NET, --net NET Network Interface | None (None)
2017-05-19 08:07:34 -04:00
-M MAC, --mac MAC MAC address
-S STORE, --store STORE
2017-10-23 22:44:46 -03:00
vmfs Store | LeastUsed (LeastUsed)
2017-05-19 08:07:34 -04:00
-g GUESTOS, --guestos GUESTOS
2018-01-11 14:38:39 -03:00
Guest OS (centos-64)
2017-10-23 22:44:46 -03:00
-o VMXOPTS, --options VMXOPTS
2018-01-11 14:38:39 -03:00
Comma list of VMX options
2017-05-19 08:07:34 -04:00
-V, --verbose Enable Verbose mode (False)
2018-01-11 14:38:39 -03:00
-f LOG, --logfile LOG
Path to the log file (/Users/sebastien/esxi-vm.log)
-l, --log Write to log file (False)
2017-05-19 08:07:34 -04:00
--summary Display Summary (False)
-u, --updateDefaults Update Default VM settings stored in ~/.esxi-vm.yml
```
2018-01-11 14:38:39 -03:00
```nash
./esxi-vm-destroy --help
usage: esxi-vm-destroy [-h] [-H HOST] [-T PORT] [-U USER] [-P PASSWORD]
[-K KEY] [-n NAME] [-V] [--summary]
ESXi Destroy VM utility.
optional arguments:
-h, --help show this help message and exit
-H HOST, --Host HOST ESXi Host/IP (esxi)
-T PORT, --Port PORT ESXi Port number (22)
-U USER, --User USER ESXi Host username (root)
-P PASSWORD, --Password PASSWORD
ESXi Host password (*****)
-K KEY, --Key KEY ESXi Host path to private key ()
-n NAME, --name NAME VM name ()
-V, --verbose Enable Verbose mode (False)
-f LOG, --logfile LOG
Path to the log file (/Users/sebastien/esxi-vm.log)
-l, --log Write to log file (False)
--summary Display Summary (False)
```
2017-05-19 08:07:34 -04:00
Example Usage
-------------
2017-05-03 17:00:03 -03:00
2018-01-11 14:38:39 -03:00
* Running the script for the first time is recommended to specify your defaults (ESXi HOST, USER, PASSWORD):
2017-05-04 09:17:09 -03:00
2018-01-11 14:38:39 -03:00
```bash
2017-05-19 08:07:34 -04:00
./esxi-vm-create -H esxi -P MySecurePassword -u
Saving new Defaults to ~/.esxi-vm.yml
2017-05-04 09:17:09 -03:00
```
2018-01-11 14:38:39 -03:00
* Create a new VM named `testvm01` using all defaults from `~/.esxi-vm.yml` :
2017-05-03 17:00:03 -03:00
2018-01-11 14:38:39 -03:00
```bash
2017-05-19 08:21:38 -04:00
./esxi-vm-create -n testvm01 --summary
Create VM Success:
2020-07-23 23:51:32 -04:00
RESOURCE POOL: ha-root-pool
2017-05-19 08:21:38 -04:00
VM NAME: testvm01
vCPU: 2
Memory: 4GB
VM Disk: 20GB
DS Store: DS_4TB
Network: None
2017-05-03 17:00:03 -03:00
```
2018-01-11 14:38:39 -03:00
* Change default number of vCPUs to `4` , Memory to `8GB` and vDisk size to `40GB` :
2018-01-07 18:51:37 -03:00
2018-01-11 14:38:39 -03:00
```bash
2020-07-23 23:51:32 -04:00
./esxi-vm-create -c 4 -m 8096 -v 40 -u
2017-05-19 08:21:38 -04:00
Saving new Defaults to ~/.esxi-vm.yml
2017-05-03 17:00:03 -03:00
```
2018-01-11 14:38:39 -03:00
* Create a new VM named `testvm02` using new defaults from `~/.esxi-vm.yml` and specifying a Network interface and partial MAC:
2018-01-07 18:51:37 -03:00
2018-01-11 14:38:39 -03:00
```bash
2017-05-19 08:07:34 -04:00
./esxi-vm-create -n testvm02 -N 192.168.1 -M 01:02:03
00:50:56:01:02:03
2017-05-03 17:00:03 -03:00
```
2018-01-11 14:38:39 -03:00
* Available *Network Interfaces* and *Available Disk Storage* volumes are listed if an invalid option is specified:
2017-05-03 17:00:03 -03:00
2018-01-11 14:38:39 -03:00
```bash
2017-05-19 08:07:34 -04:00
./esxi-vm-create -n testvm03 -N BadNet -S BadDS
ERROR: Disk Storage BadDS doesn't exist.
Available Disk Stores: ['DS_SSD500s', 'DS_SSD500c', 'DS_SSD250', 'DS_4TB', 'DS_3TB_m']
LeastUsed Disk Store : DS_4TB
ERROR: Virtual NIC BadNet doesn't exist.
Available VM NICs: ['192.168.1', '192.168.0', 'VM Network test'] or 'None'
```
2018-01-11 14:38:39 -03:00
* Create a new VM named `testvm03` using a valid *Network Interface* , valid *Disk Storage* volume, summary and verbose enabled; save as default:
2018-01-07 18:51:37 -03:00
2018-01-11 14:38:39 -03:00
```bash
2017-05-19 08:07:34 -04:00
./esxi-vm-create -n testvm03 -N 192.168.1 -S DS_3TB_m --summary --verbose --updateDefaults
Saving new Defaults to ~/.esxi-vm.yml
Create testvm03.vmx file
Create testvm03.vmdk file
Register VM
Power ON VM
Create VM Success:
2020-07-23 23:51:32 -04:00
RESOURCE POOL: ha-root-pool
2017-05-19 08:07:34 -04:00
ESXi Host: esxi
VM NAME: testvm03
2017-05-19 08:21:38 -04:00
vCPU: 4
2020-07-23 23:51:32 -04:00
Memory: 8096MB
2017-05-19 08:21:38 -04:00
VM Disk: 40GB
2017-05-19 08:07:34 -04:00
Format: thin
DS Store: DS_3TB_m
Network: 192.168.1
Guest OS: centos-64
MAC: 00:0c:29:32:63:92
00:0c:29:32:63:92
2017-05-19 08:16:38 -04:00
```
2018-01-11 14:38:39 -03:00
* Create a new VM named `testvm04` specifying an ISO file to boot:
2018-01-07 18:51:37 -03:00
2018-01-11 14:38:39 -03:00
```bash
2017-05-19 08:16:38 -04:00
./esxi-vm-create -n testvm04 --summary --iso CentOS-7-x86_64-Minimal-1611.iso
FoundISOPath: /vmfs/volumes/5430094d-5a4fa180-4962-0017a45127e2/ISO/CentOS-7-x86_64-Minimal-1611.iso
Create testvm04.vmx file
Create testvm04.vmdk file
Register VM
Power ON VM
Create VM Success:
ESXi Host: esxi
VM NAME: testvm04
2017-05-19 08:21:38 -04:00
vCPU: 4
2020-07-23 23:51:32 -04:00
Memory: 8096MB
2017-05-19 08:21:38 -04:00
VM Disk: 40GB
2017-05-19 08:16:38 -04:00
Format: thin
DS Store: DS_3TB_m
Network: 192.168.1
ISO: /vmfs/volumes/5430094d-5a4fa180-4962-0017a45127e2/ISO/CentOS-7-x86_64-Minimal-1611.iso
Guest OS: centos-64
MAC: 00:0c:29:ea:a0:42
00:0c:29:ea:a0:42
2017-10-23 22:44:46 -03:00
```
2018-01-11 14:38:39 -03:00
* Merge or add extra VMX options, saved as default:
2018-01-07 18:51:37 -03:00
2018-01-11 14:38:39 -03:00
```bash
./esxi-vm-create -o 'floppy0.present = TRUE,svga.autodetect = TRUE,svga.present = TRUE' -u
2017-10-23 22:44:46 -03:00
Saving new Defaults to ~/.esxi-vm.yml
2017-05-03 17:00:03 -03:00
```
2018-01-11 14:38:39 -03:00
**IMPORTANT**: *Do not set double quotes around values (such as `TRUE`), they will be automatically added by the tool.*
* Set the *Virtual machine hardware version* to `11` (ESXI 6), be default it is `8` :
2018-01-07 18:51:37 -03:00
2018-01-11 14:38:39 -03:00
```bash
./esxi-vm-create -o "virtualHW.version = 11" -u
Saving new Defaults to ~/.esxi-vm.yml
2018-01-07 18:51:37 -03:00
```
2018-01-11 14:38:39 -03:00
**IMPORTANT**: *Do not set double quotes around values (such as `11`), they will be automatically added by the tool.*
* Create a new VM named `testvm04` on the host `esxi2` accessible on port `2222` (instead of the usual `22` ) and with an explicit private key `/home/user1/.ssh/id_rsa_esxi2` :
```bash
2018-01-07 18:51:37 -03:00
./esxi-vm-create -H esxi2 -T 2222 -K /home/user1/.ssh/id_rsa_esxi2 -n testvm04 --summary
Create VM Success:
VM NAME: testvm04
vCPU: 2
Memory: 4GB
VM Disk: 20GB
DS Store: DS_4TB
Network: None
```
2018-01-11 14:38:39 -03:00
* Destroy the VM named `testvm04` :
```bash
./esxi-vm-destroy -H esxi -P MySecurePassword -U root -n testvm04
```
This fork
--------
This is a fork. Summary of changes in this fork:
* Fix a bug in the `vmx` file generation (double quotes missing)
* Migrate to Python 3, as [Python 2 is soon dead ](https://pythonclock.org )
* Add parameters `--port` and `--key` (SSH port number and private key)
* Verbose is now more verbose (it displays also SSH commands)
* By default, does **not** write to log file
* `esxi-vm-destroy` is documented
* Move some common code in `esxi-vm-create` and `esxi-vm-destroy` to `esxi_vm_functions.py`
* Code cleaning (work in progress)
2020-07-23 23:51:32 -04:00
* VM Memory in MB **not GB** to be able to create low memory VM
* Default VM is now Debian 8-64bits
* Fixed a bug that didn't obtain the list of VMNIC from the esxi instance properly
* Upgraded virtualHW.version 11 (native ESXI 6.0 format)
* Added parameters `--pool` to attach the VM to a resource pool
2018-01-11 14:38:39 -03:00
Copyrights
----------
2020-07-23 23:51:32 -04:00
* **This fork** Copyright © 2020 Israel Figueroa
* **[fork](https://github.com/andrivet/esxi-vm)** Copyright © 2018 Sebastien Andrivet
* **[Original code](https://github.com/josenk/esxi-vm-create)** Copyright © 2017 Jonathan Senkerik
2018-01-11 14:38:39 -03:00
2017-05-03 17:00:03 -03:00
License
-------
2018-01-11 14:38:39 -03:00

2017-05-03 17:00:03 -03:00
2018-01-11 14:38:39 -03:00
> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
2017-05-03 17:00:03 -03:00
2018-01-11 14:38:39 -03:00
> This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
2017-05-03 17:00:03 -03:00
2018-01-11 14:38:39 -03:00
> You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
2017-05-03 17:00:03 -03:00