1. Creating VMs on an ESXi host using vSphere API

    TypeScript

    To create virtual machines (VMs) on an ESXi host using the vSphere API, we will utilize Pulumi and the vsphere Pulumi provider. This provider interfaces with VMware vSphere resources which includes managing VMs and ESXi hosts. vsphere.VirtualMachine is the Pulumi resource that represents a VM within vSphere.

    Below is a Pulumi program written in TypeScript. It shows you how to create a simple virtual machine on an ESXi host. To use this program, you must have Pulumi installed, access to a vSphere instance, and the appropriate credentials to authenticate with the vSphere API. Here's a step-by-step explanation of what the code does:

    1. Imports - We are importing the necessary modules from Pulumi and the vsphere provider.
    2. Provider configuration - It sets up the vSphere provider with necessary configurations like the vSphere server, username, and password.
    3. Resource allocation - It allocates resources such as a VM folder, datastore, network, and a resource pool if needed.
    4. Virtual machine specification - It then defines the specifications for the virtual machine, including the guest operating system, CPU, memory, and disk configuration.

    Before you run the program, ensure that you've set your vSphere details in your environment or Pulumi configuration:

    pulumi config set vsphere:vsphereServer <vSphere-Server-IP> pulumi config set vsphere:username <Username> --secret pulumi config set vsphere:password <Password> --secret

    Here is the Pulumi TypeScript program to create a virtual machine on an ESXi host:

    import * as pulumi from "@pulumi/pulumi"; import * as vsphere from "@pulumi/vsphere"; // Step 1: Create a new instance of the vSphere Provider const provider = new vsphere.Provider("vsphereProvider", { vsphereServer: pulumi.config.require("vsphereServer"), allowUnverifiedSsl: true, // Only set to true in a development environment. }); // Step 2: Look up the VM folder, resource pool, and datastore const vmFolder = vsphere.getFolder({path: "vm"}); const resourcePool = vsphere.getResourcePool({name: "Resources"}); const datastore = vsphere.getDatastore({name: "datastore1"}); // Step 3: Define the VM properties const vmArgs: vsphere.VirtualMachineArgs = { // Name of the new VM name: "pulumi-vm", // Setting the guest ID to `otherGuest` as an example. Replace with appropriate value. guestId: "otherGuest", // Number of vCPUs for the VM numCpus: 2, // Amount of memory (in MB) for the VM memory: 4096, // Include at least one network interface networkInterfaces: [{ networkId: "VM Network", adapterType: "vmxnet3", }], // Include at least one disk disks: [{ size: 50, thinProvisioned: true, }], // Resource pool and datastore configurations resourcePoolId: resourcePool.then(rp => rp.id), datastoreId: datastore.then(ds => ds.id), }; // Step 4: Create the virtual machine const vm = new vsphere.VirtualMachine("pulumiVm", vmArgs, {provider}); // Output the VM ID export const vmId = vm.id;

    In this program:

    • Replace the placeholder <vSphere-Server-IP>, <Username>, and <Password> with the IP address of your vSphere server and the credentials to access it.
    • Modify the folder path, resource pool name, and datastore name per your setup.
    • Define the guestId to specify the OS type. You can find the complete list in vSphere documentation.
    • Adjust numCpus, memory, and disk size according to the requirements of your virtual machine.
    • Replace "VM Network" with the identifier of an existing network in your vSphere environment to attach the VM to. If you have VLANs configured, ensure you use the correct network label.

    To deploy the VM:

    1. Navigate to your project directory in the terminal.
    2. Run pulumi up to preview and deploy the changes.

    This Pulumi program will instruct vSphere to create a virtual machine according to the specified arguments. Once the pulumi up command finishes, the VM will be created on the ESXi host managed by your vSphere instance.

    Remember that managing VMs with Pulumi is a powerful way to automate your infrastructure, and it can be combined with other Pulumi resources for full-stack automation.



  2. I'd like to set this up as an API to programmatically setup virtual machines on an ESXi host using vSphere API

    TypeScript

    To set up virtual machines on an ESXi host using the vSphere API with Pulumi, you can use the vsphere.VirtualMachine resource from the @pulumi/vsphere package. Below is a Pulumi program that defines a function to create a virtual machine with specified properties.

    import * as pulumi from "@pulumi/pulumi"; import * as vsphere from "@pulumi/vsphere"; // Function to create a virtual machine on an ESXi host using vSphere API async function createVirtualMachine(vmName: string, guestId: string, datastoreName: string, resourcePoolName: string, networkName: string) { // Look up the VM folder, resource pool, datastore, and network const vmFolder = await vsphere.getFolder({path: "vm"}); const resourcePool = await vsphere.getResourcePool({name: resourcePoolName}); const datastore = await vsphere.getDatastore({name: datastoreName}); const network = await vsphere.getNetwork({name: networkName}); // Define the VM properties const vmArgs: vsphere.VirtualMachineArgs = { name: vmName, guestId: guestId, resourcePoolId: resourcePool.id, datastoreId: datastore.id, numCpus: 2, memory: 4096, networkInterfaces: [{ networkId: network.id, adapterType: "vmxnet3", }], disks: [{ size: 50, thinProvisioned: true, }], folder: vmFolder.path, }; // Create the virtual machine const vm = new vsphere.VirtualMachine(vmName, vmArgs); return vm; } // Example usage of the `createVirtualMachine` function const vmName = "pulumi-vm"; const guestId = "otherGuest"; // Replace with the appropriate guest ID for the OS const datastoreName = "datastore1"; const resourcePoolName = "Resources"; const networkName = "VM Network"; const vm = createVirtualMachine(vmName, guestId, datastoreName, resourcePoolName, networkName); // Export the VM ID export const vmId = vm.then(vm => vm.id);

    This program defines a function createVirtualMachine that takes in parameters for the VM name, guest ID, datastore name, resource pool name, and network name. It looks up the necessary resources like the folder, resource pool, datastore, and network, and then creates a VM with the specified properties.

    You can call this function with different parameters to set up multiple VMs. The export statement at the end allows you to retrieve the ID of the created VM, which can be useful for further automation or reference.

    Remember to replace placeholders like otherGuest with the actual guest ID that corresponds to the operating system you want to install on the VM.