vmware / pyvmomi-community-samples

A place for community contributed samples for the pyVmomi library.
Apache License 2.0
1.01k stars 922 forks source link

Create VM fail optional value is mandatory #714

Open danilo-patrucco-sev1tech opened 1 year ago

danilo-patrucco-sev1tech commented 1 year ago

Describe the bug

This is the command I'm sending to run the create_vm.py function

python3 create_vm.py -s vcenter.address.something -u my_svc_account -p 'my-svc-account-pass' -v test --datacenter-name 'dc-01' --datastore-name 'ds-11' --esx-ip {ip of vcenter}

and this is the error I'm getting in output :

Traceback (most recent call last):
  File "create_vm.py", line 79, in <module>
    main()
  File "create_vm.py", line 74, in main
    create_vm(si, args.vm_name, args.datacenter_name, args.esx_ip, args.datastore_name)
  File "create_vm.py", line 31, in create_vm
    destination_host = pchelper.get_obj(content, [vim.HostSystem], host_ip)
  File "/home/timpo/pyvmomi-community-samples/samples/tools/pchelper.py", line 157, in get_obj
    raise RuntimeError("Managed Object " + name + " not found.")
RuntimeError: Managed Object {ip of vcenter} not found.

The esxi-ip I found using nslookup on the vcenter address. I believe this may be the problem, but from what I can see the esxi ip is not mandatory information, and when I keep it out it returns the same error but there is nothing between "managed object" and "not found".

Reproduction steps

  1. run the clone_vm using a service account with the following rights:
    Alarms
    Acknowledge alarm
    Set alarm status
    Datastore
    Allocate space
    Browse datastore
    Low level file operations
    Remove file
    Update virtual machine files
    Update virtual machine metadata
    External stats provider
    Register
    Unregister
    Update
    Folder
    Create folder
    Delete folder
    Move folder
    Rename folder
    Host
    CIM
    CIM interaction
    Configuration
    System Management
    vSphere Tagging
    Assign or Unassign vSphere Tag on Object
    Network
    Assign network
    Configure
    Performance
    Modify intervals
    Resource
    Assign virtual machine to resource pool
    Sessions
    Validate session
    View and stop sessions
    Profile-driven storage
    Profile-driven storage view
    Storage views
    Configure service
    View
    Tasks
    Create task
    Update task
    Transfer service
    Manage
    Monitor
    vApp
    Import
    Virtual machine
    Change Configuration
    Acquire disk lease
    Add existing disk
    Add new disk
    Add or remove device
    Advanced configuration
    Change CPU count
    Change Memory
    Change Settings
    Change Swapfile placement
    Change resource
    Configure Host USB device
    Configure Raw device
    Configure managedBy
    Display connection settings
    Extend virtual disk
    Modify device settings
    Query Fault Tolerance compatibility
    Query unowned files
    Reload from path
    Remove disk
    Rename
    Reset guest information
    Set annotation
    Toggle disk change tracking
    Toggle fork parent
    Upgrade virtual machine compatibility
    Edit Inventory
    Create from existing
    Create new
    Move
    Register
    Remove
    Unregister
    Guest operations
    Guest operation alias modification
    Guest operation alias query
    Guest operation modifications
    Guest operation program execution
    Guest operation queries
    Interaction
    Answer question
    Backup operation on virtual machine
    Configure CD media
    Configure floppy media
    Connect devices
    Console interaction
    Create screenshot
    Defragment all disks
    Drag and drop
    Guest operating system management by VIX API
    Inject USB HID scan codes
    Install VMware Tools
    Pause or Unpause
    Perform wipe or shrink operations
    Power off
    Power on
    Record session on virtual machine
    Replay session on virtual machine
    Reset
    Resume Fault Tolerance
    Suspend
    Suspend Fault Tolerance
    Test failover
    Test restart Secondary VM
    Provisioning
    Allow disk access
    Allow file access
    Allow read-only disk access
    Allow virtual machine download
    Allow virtual machine files upload
    Clone template
    Clone virtual machine
    Create template from virtual machine
    Customize guest
    Deploy template
    Mark as template
    Mark as virtual machine
    Modify customization specification
    Promote disks
    Read customization specifications
    Service configuration
    Allow notifications
    Allow polling of global event notifications
    Manage service configurations
    Modify service configuration
    Query service configurations
    Read service configuration
    Snapshot management
    Create snapshot
    Remove snapshot
    Rename snapshot
    Revert to snapshot
  2. run the command python3 create_vm.py -s vcenter.address.something -u my_svc_account -p 'my-svc-account-pass' -v test --datacenter-name 'dc-01' --datastore-name 'ds-11' --esx-ip {ip of vcenter}, to find the esx-ip nslookup the URL for center or leave it empty

Expected behavior

Create a VM but it fail to start it because a value is wrong or empty

Additional context

No response

prziborowski commented 1 year ago

I think the --esx-ip parameter is a bit of a misnomer. Based on the fault line:

  File "create_vm.py", line 31, in create_vm
    destination_host = pchelper.get_obj(content, [vim.HostSystem], host_ip)

it is trying to find a host that has the name with the esx-ip. If your hosts are added with the host name, or qualified domain name (e.g. esx1, or esx1.example.com), then you should use that name as the argument.

The sample could have been better/cooler if it leveraged another method:

si.content.searchIndex.FindByIp(dc, hostIp, False)

but alas, it did not and should have been clearer of what the parameter is expecting.

rahulonmars commented 1 year ago

It definitely does not use IP address, as @prziborowski said, it takes the fqdn, I used localhost.localdomain in my case.