Finding a VM on VCenters

Hello,

I was curious if anyone has come up with a decent way of accomplishing this.

I have an inventory file with a bunch of VM’s I manage currently.

When we do maintenances we like to take a snapshot just before we perform actions however, I am hitting a hitch of trying to find a way to determine what vCenter the VM is located on. There can be several it could be on, and I really don’t want to statically go and define it for all my VM’s.

Any ideas on the best way to accomplish this? I was thinking I could try vmware_guest_find and iterating through the vCenters but that seems inelegant and sort of slow.

Thanks

I’m not very familiar with vmware, but I’d ask around and see if you have access to the vmware API and see if there is an endpoint on which you could send a query for the current host.
I’d assume that the information provided about the host includes which vcenter the vm is on.

I’ve done something like this with GitHub releases.

Example: (Just Tested)

`

  • name: Get Latest Release Number for App
    hosts: localhost

tasks:

`

You’ll see if you run that the output shows the tag_name object.

I’d bet you could do something similar to get what you need to put the vcenter into a variable so long as this can be pulled from the vmware API.

Not sure how you’re Vcenters are setup, but in my environment our Vm’s are not really assigned to specific Vcenter’s so I use the UUID’s to perform snapshots for each VM in one playbook. I haven’t figured out how to call the UUID so I have it set manually.

All of our vCenters are unlinked, and completely independent making it a little more difficult.

It’s possible we could have some more luck using a dynamic inventory from vCenter. We are looking at Tower, so it could be a possibility.

you might try https://docs.ansible.com/ansible/2.6/modules/vmware_vm_facts_module.html and see what you get back. I don’t have an area to test it though.