Ansible ESXi Module
Ansible provides a module for automating VMWare host and guest management using the VMWare API. The problem I ran into is that, with the free version of ESXi, most of the APIs are read-only. See Kevin Soltow’s VMWare Blog for the full scoop on restrictions of the various editions.
The good news is that if you enable SSH access to your ESXi host you can use the command line to accomplish the tasks related to guest creation and management using the available command line utilities.
The goal of this module is not to replace the Ansible vmware_guest module but to implement the functions not available on a standalone ESXi server in the simplest way possible. In order for the module to function, you will need to enable the SSH daemon on the ESXi server. The table below outlines the functions that are implemented in the ESXi module for Ansible.
Function | Command |
VMWare version: Returns the Product, Version, Build, Update and Patch information. | esxcli system version get |
List of guest VMs Returns info including vmid, datastore, vmx file path, guest O/S, hardware version and annotation. | vim-cmd vmsvc/getallvms |
Create a basic guest VM: Returns the vmid of the new guest. | vim-cmd vmsvc/createdummyvm {name} {datastore} |
Unregister a VM: Removes a VM from inventory without deleting the files. | vim-cmd vmsvc/unregister {vmid} |
Register a VM: Adds a VM to inventory given the path to the vmx file. | vim-cmd solo/registervm {path to vmx file} |
Power commands: on, off, shutdown, reboot, getstate | vim-cmd vmsvc/power.{state} {vmid} |
Delete a virtual disk: Deletes a virtual disk (does not modify the vmx file) | vmkfstools -U {path to vmdk file} |
Create a virtual disk: Creates a virtual disk file set of the specified size. | vmkfstools -c {size} {path to vmdk file} |
Clone a virtual disk: Clones a virtual disk and optionally changes the format. | vmkfstools -i {path to source vmdk file} {path to clone vmdk file} |
Get Snapshot information: Gets a list of snapshots | vim-cmd vmsvc/get.snapshotinfo {vmid} |
Create Snapshot: Create a new snapshot | vim-cmd vmsvc/snapshot.create {vmid} {name} |
Delete all Snapshots: Commit and delete all snapshops to the primary disk file. | vim-cmd vmsvc/snapshot.removeall {vmid} |
Creating a Virtual Machine
The ESXi module follows a simple process to create a virtual machine:
- Create a Dummy VM on the ESXi server
- Unregister the Dummy VM
- Delete and then create a new virtual disk
- Provision networking and CD/DVD Hardware by modifying the vmx configuration file.
- Register the updated VM configuration file.
Controlling the Virtual Machine
The ESXi module implements some of the VM power commands such as power on, power off, reboot and shutdown. The latter two commands implement a check to ensure that vmware tools are running on the VM.
Modifying the Virtual Machine
The ESXi module has a limited set of modifications that can be used to change the memory size, CD/DVD image, or change the network the VM interface is attached to.
You can find the Ansible ESXi module on my GitHub repository.
Installing the Module
The module is installed in the Ansible libary directory. I usually create this directory in the project directory and add the following line to ansible.cfg:
library = ./library
Using the Module
---
- name: Testing for ESXi module
hosts: esxi
gather_facts: false
tasks:
- name: Test
esxi:
action: "create_vm"
vm_name: "testvm"
vm_datastore: "datastore1"
vm_guest_os: "centos7-64"
vm_mem_size: 4096
vm_cpu_count: 2
vm_scsi_type: "lsilogic"
vm_hdd_size: 40
vm_networks:
- networkName: "VM Network"
virtualDev: "vmxnet3"
vm_iso_image: "/vmfs/volumes/datastore/iso/CentOS-7-x86_64-Minimal-1810.iso"
Leave Comments
You must be logged in to post a comment.