Cloning an existing VMware VM fails in Foreman 3.15: undefined method 'include?' for nil:NilClass

I’m currently using Foreman 3.15 with a VMware vCenter compute resource.
I’m trying to provision a new host by cloning an existing VMware VM (not a template) using the “Image” provisioning method.

However, the provisioning fails with the following error:

undefined method `include?' for nil:NilClass

This appears to originate from the fog-vsphere plugin, specifically in the method get_vm_by_name in:

/usr/share/gems/gems/fog-vsphere-3.7.1/lib/fog/vsphere/requests/compute/get_virtual_machine.rb:40

The issue occurs because name is nil, and name.include?('/') is called without a check, causing a crash.


Expected behavior:

I expect Foreman to allow me to clone an existing VM (not a template) by specifying its name, and to handle this process gracefully without crashing if parameters are missing or misconfigured.


Steps to reproduce:

  1. In Foreman 3.15, go to Hosts > Create Host
  2. Use provision method = Image
  3. Select a compute profile and a VMware compute resource
  4. Fill in the VM settings (CPU, memory, disks, etc.)
  5. Attempt to submit the form without explicitly setting the “VM to clone”

Result:
The orchestration fails with:

025-08-06T18:41:07 [I|app|92eedfa1] Backtrace for 'Failed to create a compute VCenter xxxxxx (VMware) instance mysrv: undefined method `include?' for nil:NilClass
 92eedfa1 |  ' error (NoMethodError): undefined method `include?' for nil:NilClass
 92eedfa1 | /usr/share/gems/gems/fog-vsphere-3.7.1/lib/fog/vsphere/requests/compute/get_virtual_machine.rb:40:in `get_vm_by_name'
 92eedfa1 | /usr/share/gems/gems/fog-vsphere-3.7.1/lib/fog/vsphere/requests/compute/get_virtual_machine.rb:23:in `get_vm_ref'
 92eedfa1 | /usr/share/gems/gems/fog-vsphere-3.7.1/lib/fog/vsphere/requests/compute/get_virtual_machine.rb:7:in `get_virtual_machine'
 92eedfa1 | /usr/share/gems/gems/fog-vsphere-3.7.1/lib/fog/vsphere/requests/compute/vm_clone.rb:27:in `vm_clone_check_options'
 92eedfa1 | /usr/share/gems/gems/fog-vsphere-3.7.1/lib/fog/vsphere/requests/compute/vm_clone.rb:111:in `vm_clone'
 92eedfa1 | /usr/share/foreman/app/models/compute_resources/foreman/model/vmware.rb:608:in `clone_vm'
 92eedfa1 | /usr/share/foreman/app/models/compute_resources/foreman/model/vmware.rb:516:in `create_vm'
 92eedfa1 | /usr/share/foreman/app/models/concerns/orchestration/compute.rb:108:in `setCompute'
 92eedfa1 | /usr/share/foreman/app/models/concerns/orchestration.rb:220:in `execute'
 92eedfa1 | /usr/share/foreman/app/models/concerns/orchestration.rb:149:in `block in process'
 92eedfa1 | /usr/share/foreman/app/models/concerns/orchestration.rb:141:in `each'
 92eedfa1 | /usr/share/foreman/app/models/concerns/orchestration.rb:141:in `process'
 92eedfa1 | /usr/share/foreman/app/models/concerns/orchestration.rb:41:in `around_save_orchestration'
 92eedfa1 | /usr/share/gems/gems/activesupport-7.0.8.7/lib/active_support/callbacks.rb:127:in `block in run_callbacks'
 92eedfa1 | /usr/share/gems/gems/activesupport-7.0.8.7/lib/active_support/callbacks.rb:138:in `run_callbacks'
 92eedfa1 | /usr/share/gems/gems/activesupport-7.0.8.7/lib/active_support/callbacks.rb:929:in `_run_save_callbacks'
 92eedfa1 | /usr/share/gems/gems/activerecord-7.0.8.7/lib/active_record/callbacks.rb:455:in `create_or_update'
 92eedfa1 | /usr/share/gems/gems/activerecord-7.0.8.7/lib/active_record/timestamp.rb:126:in `create_or_update'
 92eedfa1 | /usr/share/gems/gems/activerecord-7.0.8.7/lib/active_record/persistence.rb:615:in `save'
 92eedfa1 | /usr/share/gems/gems/activerecord-7.0.8.7/lib/active_record/validations.rb:47:in `save'
 92eedfa1 | /usr/share/gems/gems/activerecord-7.0.8.7/lib/active_record/transactions.rb:298:in `block in save'
 92eedfa1 | /usr/share/gems/gems/activerecord-7.0.8.7/lib/active_record/transactions.rb:354:in `block in with_transaction_returning_status'
 92eedfa1 | /usr/share/gems/gems/activerecord-7.0.8.7/lib/active_record/connection_adapters/abstract/transaction.rb:319:in `block in within_new_transaction'

@lstejska do you have some free cycles to look at this one?

https://issues.redhat.com/browse/SAT-36506 added to our backlog

1 Like

Hello,

Which release is the fix planned for?

Hi, I will take a look at it this week, once I’ll have some info I will let you know

thx !

I’ve been trying to replicate the issue, but without any success.

What I tried:

  • Foreman 3.15 & latest develop
  • Creating a host
  • Cloning a host (from the host above)
  • With host group and compute profile
  • With disabled cache.
  • Mix of NVME controllers and others.

No success, it works for me.

Could you please share some details with me so I can investigate deeper?
Of course, without any kind of sensitive data.

Information about the resources:

hammer compute-resource info
hammer compute-profile info
hammer os info
hammer hostgroup info
hammer hostg info

I would like to see more logs, especially those including steps prior to host creation or cloning, to understand what happened in the app.

If you don’t feel like sharing the data publicly, feel free to send it via PM.

hammer compute-resource info

Id:                   1
Name:                 VMware Vcenter Prd
Provider:             VMware
Description:
User:                 xxxxx@vsphere.prod
Locations:
    Default Location
Organizations:
    Default Organization
Created at:           2024/03/15 13:46:05
Updated at:           2024/12/18 09:29:07
Url:                 xxxx.domain.com
Datacenter:           MYDC
Server:               myserver
Console password set: yes
Caching enabled:      yes

hammer compute-profile info

Id:                 4
Name:               alix-vm
Created at:         2024/03/15 13:58:16
Updated at:         2024/03/15 13:58:16
Compute attributes:
 1) Id:               1
    Name:             2 CPUs and 4096 MB memory
    Compute Resource: VMware Vcenter Prd
    VM attributes:    {"cpus"=>"2", "corespersocket"=>"1", "memory_mb"=>"4096", "firmware"=>"automatic", "cluster"=>"CL-FTV-02-PT-PROD", "resource_pool"=>"Resources", "path"=>"/Datacenters/MYDC/vm", "guest_id"=>"rhel9_64Guest", "hardware_version"=>"vmx-19", "memoryHotAddEnabled"=>"1", "cpuHotAddEnabled"=>"1", "add_cdrom"=>"0", "boot_order"=>["network", "disk"], "annotation"=>"", "image_id"=>"502be0db-b914-41bf-b6a2-a3f506e9d9fd", "virtual_tpm"=>"0", "interfaces_attributes"=>{"0"=>{"type"=>"VirtualVmxnet3", "network"=>"dvportgroup-230350"}}, "volumes_attributes"=>{"0"=>{"thin"=>true, "name"=>"Hard disk", "mode"=>"persistent", "controller_key"=>1000, "size_gb"=>10, "datastore"=>"vsan-02", "storage_pod"=>nil}, "1"=>{"thin"=>true, "name"=>"Hard disk", "mode"=>"persistent", "size_gb"=>10, "datastore"=>"vsan-02", "storage_pod"=>nil, "eager_zero"=>false, "controller_key"=>1000}}, "nvme_controllers"=>[], "scsi_controllers"=>[{"type"=>"ParaVirtualSCSIController", "key"=>1000}]}

hammer os info

hammer hostgroup info

Id:                    1
Name:                  alix default
Title:                 alix default
Description:
  Base host groupe for all machines
Compute Resource:      Vcenter Prod FTV
Network:

Operating system:

Parameters:

Locations:
    Default Location
Organizations:
    Default Organization
Content View:
    Id:   9
    Name: rhel9
Lifecycle Environment:
    Id:   1
    Name: Library
Content Source:
    Id:   1
    Name: mysrv
Kickstart Repository:
    Id:
1 Like

I’m trying to understand the relationship between templates, images, and provisioning methods when using Foreman with a VMware compute resource.

My current use case:

  • I need to clone an existing VM in vCenter (not a template) using Image-based provisioning.
  • I noticed that in the Host creation form there is no “Clone from VM” field in the VMware tab.
  • The only place where I can reference the source is under Compute Resource → Images, where I can create an “Image” pointing to either a template or a VM.

Questions I’d like to clarify:

  1. What is the intended difference between a template and an image in Foreman’s VMware integration?
  2. Is creating an Image always required—even when cloning an existing VM?
  3. If I select a VM (not a vSphere template) as the Image source, are there any limitations or best practices?
  4. Is there any plan to make the UI clearer (for example, exposing a “VM to clone” fied ?

I don’t see anything explicitly referring to SecureBoot, but could this issue already be resolved in Fixes #38381 - fix image-based provisioning on VMware by nofaralfasi · Pull Request #10524 · theforeman/foreman · GitHub? It adds support for SecureBoot and VirtualTPM in image-based provisioning on VMware.
It might be worth checking whether your version of Foreman includes that commit and whether your fog-vsphere dependency is new enough to include the needed changes.

That might be the issue, in Foreman, images are VMware’s templates. From the documentation:

VMware vSphere uses templates as images for creating new virtual machines. If using image-based provisioning to create new hosts, you need to add VMware template details to your Foreman server. This includes access details and the template name.

What is the intended difference between a template and an image in Foreman’s VMware integration?

Images are Foreman’s mapping to VMware’s templates. doc

Is creating an Image always required—even when cloning an existing VM?

No, one image can be used for multiple hosts in Foreman.

If I select a VM (not a vSphere template) as the Image source, are there any limitations or best practices?

To the best of my knowledge, we haven’t tested it, so I would say it’s not supported.

Is there any plan to make the UI clearer (for example, exposing a “VM to clone” fied ?

Not at the moment, but all contributions are welcome.


I’m talking about cloning a VM from the “clone” button, this opens a new page with the characteristics of the VM you want to clone, you just have to change the name + ip. When I say clone, it’s in the vmware sense, I don’t know if I’m clear.

Yes you are clear, but you also mentioned:

[quote=“orelops, post:10, topic:44043”]
The only place where I can reference the source is under Compute Resource → Images, where I can create an “Image” pointing to either a template or a VM.
[/quote]

To make sure, you should not point the (Foreman) image to the (VMware) virtual machine, but only to the template.

yes, absolutely.

I think I understand what the issue is, but let me first do some sanity checks that I understand your setup correctly.

  • You have a system in Foreman (let’s call it server.example.com), that is a VM on VMware and you want to clone it.
  • The clone should be (besides name/ip/mac address) identical to server.example.com, especially it should also have a copy of the disks server.example.com had at the time of cloning
  • The OS of server.example.com has no matching “image” known to Foreman
  • Images are Foremans representation of a VMware template with some additional data (like, which OS it belongs to, what the user is for the login, etc)

To check the image definition, you can list them with hammer:

# hammer compute-resource image list --compute-resource-id 1
---|------|------------------|----------|------|----------
ID | NAME | OPERATING SYSTEM | USERNAME | UUID | USER DATA
---|------|------------------|----------|------|----------

(The OS you show in your screenshot is called “RHEL 9”, so you’d want to look out for “RHEL 9” in the OS column here).

Now, when you go to the server.example.com host in Foreman and click on “Clone”, you get to a “Create host” form, that has most data pre-filled from server.example.com, and you need to enter a new name and expect a new copy of that host to be deployed. Instead, you get an error.

The problem (well, one problem) is that Foreman uses server.example.com as an input for the form data, but tries to deploy the new system from the image of the OS that server.example.com has, not with the disks of server.example.com has. But the OS has no image defined, and that’s where it explodes with undefined method include?.

You should be able to reproduce this also by deploying a fully fresh system with the same OS, as the “image” field remains empty: