# Finding a VM on VCenters

**URL:** <https://forum.ansible.com/t/finding-a-vm-on-vcenters/28398>\
**Category:** Ansible Project\
**Created:** [August 22, 2018, 4:03pm UTC](https://forum.ansible.com/t/finding-a-vm-on-vcenters/28398 "2018-08-22T16:03:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carter\_Stephen](https://avatars.discourse-cdn.com/v4/letter/c/f9ae1b/32.png) [@Carter\_Stephen](https://forum.ansible.com/u/Carter_Stephen)\
**Post date:** [August 22, 2018, 4:03pm UTC](https://forum.ansible.com/t/finding-a-vm-on-vcenters/28398/1 "2018-08-22T16:03:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Arden\_Shackelford](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@Arden\_Shackelford](https://forum.ansible.com/u/Arden_Shackelford)\
**Post date:** [August 22, 2018, 6:28pm UTC](https://forum.ansible.com/t/finding-a-vm-on-vcenters/28398/2 "2018-08-22T18:28:49Z")

</div>

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:

- name: Get Release Number  
uri:  
url: [https://api.github.com/repos/BookStackApp/BookStack/releases/latest](https://api.github.com/repos/BookStackApp/BookStack/releases/latest)  
method: GET  
return\_content: yes  
body\_format: json  
register: latest\_release

- debug:  
var: latest\_release.json.tag\_name

`

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.

---

<div class="post-metadata">

**Author:** ![Izzy](https://avatars.discourse-cdn.com/v4/letter/i/c2a13f/32.png) [@Izzy](https://forum.ansible.com/u/Izzy)\
**Post date:** [August 22, 2018, 6:35pm UTC](https://forum.ansible.com/t/finding-a-vm-on-vcenters/28398/3 "2018-08-22T18:35:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Carter\_Stephen](https://avatars.discourse-cdn.com/v4/letter/c/f9ae1b/32.png) [@Carter\_Stephen](https://forum.ansible.com/u/Carter_Stephen)\
**Post date:** [August 22, 2018, 7:37pm UTC](https://forum.ansible.com/t/finding-a-vm-on-vcenters/28398/4 "2018-08-22T19:37:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Lozada\_De\_L](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@Jonathan\_Lozada\_De\_L](https://forum.ansible.com/u/Jonathan_Lozada_De_L)\
**Post date:** [August 22, 2018, 8:23pm UTC](https://forum.ansible.com/t/finding-a-vm-on-vcenters/28398/5 "2018-08-22T20:23:03Z")

</div>

you might try [https://docs.ansible.com/ansible/2.6/modules/vmware\_vm\_facts\_module.html](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.
