Provisioning VMware VM from VM Template using API or CLI

Problem: Need to provision VMware VM from Template using the Foreman API or CLI - any advice would be appreciated.

Expected outcome: Complete VM build after hitting the API / executing CLI command/script.

Foreman and Proxy versions: 1.19

Foreman and Proxy plugin versions:

Other relevant data:
VMs are successfully provisioned via the Foreman web interface, need to automate this by calling the API or possibly the CLI if necessary.
TheForeman is running on Ubuntu 16.4 and has DHCP & DNS running locally and managed.
Below is the production.log entry from the VM being successfully created:

2018-12-22T15:49:27 [I|app|17b79] Parameters: {“utf8”=>“✓”, “authenticity_token”=>“hlBreSRRWzOZrzr89knTdcedtJ8awAgUxP02Sf5VWz8eUDgEBtw8g57jwBeYMI7/A5LtnK6HXguu7Qz0sQXa5A==”, “host”=>{“name”=>“k8sworker3”, “hostgroup_id”=>“1”, “puppetclass_ids”=>[""], “managed”=>“true”, “progress_report_id”=>"[FILTERED]", “type”=>“Host::Managed”, “interfaces_attributes”=>{“0”=>{"_destroy"=>“0”, “type”=>“Nic::Managed”, “mac”=>"", “identifier”=>"", “name”=>“k8sworker3”, “domain_id”=>“1”, “subnet_id”=>“1”, “ip”=>“10.10.40.173”, “ip6”=>"", “managed”=>“1”, “primary”=>“1”, “provision”=>“1”, “virtual”=>“0”, “tag”=>"", “attached_to”=>"", “compute_attributes”=>{“type”=>“VirtualE1000”, “network”=>“network-422”}}}, “compute_attributes”=>{“cpus”=>“2”, “corespersocket”=>“1”, “memory_mb”=>“4096”, “firmware”=>“bios”, “cluster”=>“chainsaw.homelab.local”, “resource_pool”=>“Resources”, “path”=>"/Datacenters/HomelabSDDC/vm/Kubernetes-2", “guest_id”=>“otherGuest”, “hardware_version”=>“Default”, “memoryHotAddEnabled”=>“0”, “cpuHotAddEnabled”=>“0”, “add_cdrom”=>“0”, “start”=>“1”, “annotation”=>"", “scsi_controllers”=>"{“scsiControllers”:[{“type”:“VirtualLsiLogicController”,“key”:1000}],“volumes”:[{“thin”:true,“name”:“Hard disk”,“mode”:“persistent”,“controllerKey”:1000,“size”:41943040,“sizeGb”:40,“datastore”:“QAPNAS-VMs”}]}", “image_id”=>“500b1d91-6930-30f5-afc0-888d6a54be31”}, “architecture_id”=>“1”, “operatingsystem_id”=>“2”, “provision_method”=>“image”, “build”=>“1”, “medium_id”=>“7”, “ptable_id”=>"", “pxe_loader”=>"", “disk”=>"", “root_pass”=>"[FILTERED]", “is_owned_by”=>“4-Users”, “enabled”=>“1”, “comment”=>"", “overwrite”=>“false”}}

You can try to create the VM from hammer as following:

hammer host create --name k8sworker3 --hostgroup-id 1 --operatingsystem-id 1 --architecture "x86_64" --domain-id 1 --subnet-id 1 --compute-resource-id 1 --provision-method 'image' --compute-attributes "cpus=2, corespersocket=1, memory_mb=4096, cluster=chainsaw.homelab.local, path=/Datacenters/HomelabSDDC/vm/Kubernetes-2", image_id=500b1d91-6930-30f5-afc0-888d6a54be31" --interface "compute_network=<network_id_from_vmware>, type=VirtualE1000 , managed=1, primary=1, provision=1, type=interface" --ip 10.10.40.173 --volume "datastore=QAPNAS-VMs, size_gb=1, thin=true"

if you have more parameters that you want to add, you can search in ‘hammer host create --help’

Good Luck

2 Likes

Here’s the data structure we POST to foreman’s API. We use compute profiles to set the basic hardware configuration of the VM (cpu, ram, disk, network interface), so those are consolidated into the compute_profile_id. The variable “vmware_network” is the name of the network in vsphere (not in foreman). We have several different VLANs in use, so that part needed to be dynamically selected in our script rather than left to the compute profile.

vm_options = {
    "name": vm_hostname
    "compute_resource_id": resource['id'],
    "compute_profile_id": profile['compute_profile_id'],
    "hostgroup_id": group_id,
    "subnet_id": subnet['id'],
    "location_id": resource['locations'][0]['id'],
    "organization_id": organization['id'],
    "compute_attributes": {"start": "1"}, # this must be the *string* "1" or the vm won't start
    "interfaces_attributes": [{
        "compute_attributes": {"network": vmware_network},
        }],
    "build": True,
    }