ansible-collections / community.vmware

Ansible Collection for VMware
GNU General Public License v3.0
346 stars 334 forks source link

vm_info cannot find specific vm #2015

Closed kengsia closed 6 months ago

kengsia commented 6 months ago
SUMMARY

vm_info module cannot find specific vm

below is my playbook:

when I run it , I receive below error :

{ "msg": "Failed to find virtual machine sgpvm0001", "invocation": { "module_args": { "hostname": "sgpvcs01.mycompany.com", "username": "admin\user1", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "vm_name": "sgpvm0001", "validate_certs": false, "port": 443, "vm_type": "all", "show_attribute": false, "show_cluster": true, "show_datacenter": true, "show_datastore": true, "show_folder": true, "show_esxi_hostname": true, "show_mac_address": true, "show_net": true, "show_resource_pool": true, "show_tag": false, "show_allocated": false, "proxy_host": null, "proxy_port": null, "folder": null } }, "_ansible_no_log": null, "changed": false, "_ansible_delegated_vars": { "ansible_host": "localhost", "ansible_port": 22, "ansible_user": "root", "ansible_connection": "local" } }

ISSUE TYPE
COMPONENT NAME
##### ANSIBLE VERSION
COLLECTION VERSION
CONFIGURATION
OS / ENVIRONMENT
STEPS TO REPRODUCE
EXPECTED RESULTS
ACTUAL RESULTS
kengsia commented 6 months ago

ansible version :ansible [core 2.14.9] collection version: 3.11.2

ihumster commented 6 months ago

@kengsia Message "Failed to find virtual machine " returned in process called very reusable common function find_vm_by_name. This function used in many other modules this collection.

The problem is probably in the parameters you pass to the module.

  1. Try copying the VM name from your playbook and searching for the VM in the vCenter UI in its own search bar.
  2. Try explicitly specifying the folder in which the VM is located in the playbook (if the VM is in the root, your path will be /<datacenter name>/vm/).
kengsia commented 6 months ago

hello @ihumster

Thanks for your feedback .

  1. I have the permission to search the VM and get the detailed information of the VM in webUI of vCenter .
  2. After I specified the folder of VM in the playbook , it still told me cannot find the folder :(
kengsia commented 6 months ago

hello @ihumster It's wired I succesfuly use vmware.vmwarer_datacenter_info module to find out the datacenter "Singapore-Cloud" but it failed to find the folder "/Singapore-Cloud/vm"

output of playbook:

{ "name": "Singapore-Cloud", "moid": "datacenter-517", "config_status": "gray", "overall_status": "gray" },

{ "msg": "Failed to find folder specified by /Singapore-Cloud/vm", "invocation": { "module_args": { "hostname": "sgpvcs01.mycompany.com", "username": "admin\user1", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "vm_name": "sgpvm0001", "folder": "/Singapore-Cloud/vm", "validate_certs": false, "port": 443, "vm_type": "all", "show_attribute": false, "show_cluster": true, "show_datacenter": true, "show_datastore": true, "show_folder": true, "show_esxi_hostname": true, "show_mac_address": true, "show_net": true, ...

does it matter we have more than 4000 VMs in our vCenter ?

ihumster commented 6 months ago

Oh. Folder <datacenter>/vm/ is special system folder. Please read carefully this page from vSphere API documentation. https://vdc-download.vmware.com/vmwb-repository/dcr-public/b50dcbbf-051d-4204-a3e7-e1b618c1e384/538cf2ec-b34f-4bae-a332-3820ef9e7773/vim.ServiceInstance.html

And take a look at the picture. Asterisks point to 5 special system folders in vSphere Managed Objects Hierarchy (datacenterFolder, hostFolder, networkFolder, datastoreFolder and vmFolder). And get info about this folders I think not work.

You can also read information about the Folder object. https://vdc-download.vmware.com/vmwb-repository/dcr-public/b50dcbbf-051d-4204-a3e7-e1b618c1e384/538cf2ec-b34f-4bae-a332-3820ef9e7773/vim.Folder.html

kengsia commented 6 months ago

hello @ihumster , Actually my purpose is to develop a playbook to create snapshot for specific VM , the "vmware_guest_snapshot" module needs the folder path if the vm name provided . Then I have to use " vmware_guest_find" module to find out the folder of VM firstly . I also tried below playbook :

but when I run the playbook , it says ""Unable to manage snapshots for non-existing VM sgpvm0001" , it cannot find the VM which is apparently existing in the vCenter . I also tried to use moid instead of vm name but it still cannot find the VM .

Do you have any suggestions how I manage the snapshot for a specific VM ? Thanks.

ihumster commented 6 months ago

@kengsia Open vSphere Client (vCenter UI) and take look on your tree VMs and Folders. You mast view next hierarchy:

<vCenter Name or IP>
|-- <Datacenter Name>
    | -- <VM Folder>
    |    |-- <VM in Folder>
    |    ...
    |-- <VM in root>
    |-- <VM in root>

The folder path is constructed as follows: /<Datacenter Name>/vm/<VM Folder>/<Next Nested VM Folder>/<VM Name>

For VMs in root he being short /<DC Name>/vm/<VM in root>, for VM in some folder path will be longer.

kengsia commented 6 months ago

hello @ihumster since we have multiple vCenters and datacenters which are managed by other team and they may adjust the hierarchy in future , so it will be better for us to use playbook to find the vm folder path automatically instead of inputting static value into playbook .

For example , below is one of our vCenter , we still have one hierarchy before datacenter . so the folder path will be "standard esxi servers/Datacenter_name/vm/" ?

kengsia commented 6 months ago

Sorry I failed to attach the screenshot The hierarchy of my vCenter is like:


<vCenter Name or IP>
|--Folder1 (dedicated ESXi servers)
|-- Folder2 (standard ESXi servers)
     |-- <Datacenter Name>
         |-- <VM in root>
         |-- <VM in root>

Then the folder path should be "Folder2/DC_name/vm/ " ?

ihumster commented 6 months ago

@kengsia If Folder1/Folder2 is type of Datacenter Folder then path is /Folder2/DC_Name/vm/.

kengsia commented 6 months ago

Hi @ihumster yes , Folder1/Folder2 is type of Datacenter Folder , but it still cannot find the specific VM , also not work when providing moid instead of vm name . curious what is the cause for this problem .

{ "msg": "Unable to manage snapshots for non-existing VM sgpvm0001", "invocation": { "module_args": { "datacenter": "Singapore-Cloud", "folder": "Standard ESXI Servers/Singapore-Cloud/vm", "hostname": "sgpvcs01.mycomany.com", "username": "admin\user1", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "name": "sgpvm0001", "state": "present", "snapshot_name": "test", "description": "test", "validate_certs": false, "port": 443, "name_match": "first", "use_instance_uuid": false, "quiesce": false, "memory_dump": false, "remove_children": false, "proxy_host": null, "proxy_port": null, "uuid": null, "moid": null, "snapshot_id": null, "new_snapshot_name": null, "new_description": null } }, "_ansible_no_log": null, "changed": false, "_ansible_delegated_vars": { "ansible_host": "localhost", "ansible_port": 22, "ansible_user": "root", "ansible_connection": "local" } }

ihumster commented 6 months ago

@kengsia You can test work with API through PowerCLI? Use Get-VM cmdLet with your credentials and other used in playbook paramters.

kengsia commented 6 months ago

hello @ihumster , I found the root cause , Ansible is case sensitive, all vms in our environment are in upper case. I updated my playbook "name: "{{ vm_short_name | upper }}"" Thanks for your support and suggestions.