# Why is "datacenter" a required parameter for vmware modules?

**URL:** <https://forum.ansible.com/t/why-is-datacenter-a-required-parameter-for-vmware-modules/31135>\
**Category:** Ansible Developer\
**Tags:** vmware\
**Created:** [August 27, 2019, 12:38pm UTC](https://forum.ansible.com/t/why-is-datacenter-a-required-parameter-for-vmware-modules/31135 "2019-08-27T12:38:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MKPhil](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@MKPhil](https://forum.ansible.com/u/MKPhil)\
**Post date:** [August 27, 2019, 12:38pm UTC](https://forum.ansible.com/t/why-is-datacenter-a-required-parameter-for-vmware-modules/31135/1 "2019-08-27T12:38:54Z")

</div>

In many vmware\_\* modules, the datacenter parameter is marked as required - but is it really needed?

I ask because I’ve written a module, based on vmware\_guest\_find, that returns the Datacenter for a given VM and VCenter, which I can then feed in to the other “official” modules. My module uses the built-in function “get\_parent\_datacenter” (defined in [https://github.com/ansible/ansible/blob/devel/lib/ansible/module\_utils/vmware.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/vmware.py)) so I’m not inventing anything particularly new. My point is that if I, an amateur Python coder at best, can do this, why can’t the official modules do it by default? Is there some reason for the current behaviour that I am missing?

I’m not really confident (yet) in editing the official modules and submitting Pull Requests etc. but am happy to share my code with someone who can do this.

---

<div class="post-metadata">

**Author:** ![Christoph\_Wegener](https://avatars.discourse-cdn.com/v4/letter/c/57b2e6/32.png) [@Christoph\_Wegener](https://forum.ansible.com/u/Christoph_Wegener)\
**Post date:** [August 27, 2019, 1:31pm UTC](https://forum.ansible.com/t/why-is-datacenter-a-required-parameter-for-vmware-modules/31135/2 "2019-08-27T13:31:54Z")

</div>

Can you point to any particular module that has 'datacenter' marked as  
a required parameter?

---

<div class="post-metadata">

**Author:** ![MKPhil](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@MKPhil](https://forum.ansible.com/u/MKPhil)\
**Post date:** [August 27, 2019, 1:42pm UTC](https://forum.ansible.com/t/why-is-datacenter-a-required-parameter-for-vmware-modules/31135/3 "2019-08-27T13:42:57Z")

</div>

Here’s some:  
[https://docs.ansible.com/ansible/latest/modules/vmware\_guest\_disk\_module.html](https://docs.ansible.com/ansible/latest/modules/vmware_guest_disk_module.html)

[https://docs.ansible.com/ansible/latest/modules/vmware\_guest\_facts\_module.html](https://docs.ansible.com/ansible/latest/modules/vmware_guest_facts_module.html)  
[https://docs.ansible.com/ansible/latest/modules/vmware\_guest\_snapshot\_module.html](https://docs.ansible.com/ansible/latest/modules/vmware_guest_snapshot_module.html#vmware-guest-snapshot-module)  
[https://docs.ansible.com/ansible/latest/modules/vmware\_guest\_tools\_upgrade\_module.html](https://docs.ansible.com/ansible/latest/modules/vmware_guest_tools_upgrade_module.html)

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 27, 2019, 5:10pm UTC](https://forum.ansible.com/t/why-is-datacenter-a-required-parameter-for-vmware-modules/31135/4 "2019-08-27T17:10:40Z")

</div>

If I have two VM both called hostA, one in datacenter-north and one in datacenter-south, which one do you choose?

---

<div class="post-metadata">

**Author:** ![MKPhil](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@MKPhil](https://forum.ansible.com/u/MKPhil)\
**Post date:** [August 28, 2019, 2:48pm UTC](https://forum.ansible.com/t/why-is-datacenter-a-required-parameter-for-vmware-modules/31135/5 "2019-08-28T14:48:58Z")

</div>

That’s not a datacenter issue - you can have two VMs with the same name in the same datacenter but VMWare does not recommend this - [https://kb.vmware.com/s/article/2108769](https://kb.vmware.com/s/article/2108769)

vmware\_guest\_facts has a “name\_match” parameter “If multiple virtual machines matching the name, use the first or last found.” (see [https://docs.ansible.com/ansible/latest/modules/vmware\_guest\_facts\_module.html#vmware-guest-facts-module](https://docs.ansible.com/ansible/latest/modules/vmware_guest_facts_module.html#vmware-guest-facts-module)) to cater for this but in my opinion (I support over 20,000 vms), you should probably really aim to have unique names.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 29, 2019, 4:22pm UTC](https://forum.ansible.com/t/why-is-datacenter-a-required-parameter-for-vmware-modules/31135/6 "2019-08-29T16:22:07Z")

</div>

> That's not a datacenter issue - you can have two VMs with the same name in  
> the same datacenter but VMWare does not recommend this -  
> [https://kb.vmware.com/s/article/2108769](https://kb.vmware.com/s/article/2108769)

I would say it is.  
A datacenter is a namespace that makes it possible to have the same name for VMs, templates, hosts, clusters, networks and datastores across datacenters. But must of them need to be uniq within the datacenter.

> vmware\_guest\_facts has a "name\_match" parameter "If multiple virtual  
> machines matching the name, use the first or last found." (see  
> [https://docs.ansible.com/ansible/latest/modules/vmware\_guest\_facts\_module.html#vmware-guest-facts-module](https://docs.ansible.com/ansible/latest/modules/vmware_guest_facts_module.html#vmware-guest-facts-module))  
> to cater for this but in my opinion (I support over 20,000 vms), you should  
> probably really aim to have unique names.

IMHO it's missing the option fail.

---

<div class="post-metadata">

**Author:** ![goneri](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/goneri/32/1023_2.png) [@goneri](https://forum.ansible.com/u/goneri)\
**Post date:** [August 29, 2019, 5:01pm UTC](https://forum.ansible.com/t/why-is-datacenter-a-required-parameter-for-vmware-modules/31135/7 "2019-08-29T17:01:04Z")

</div>

Hi,

If no datacenter is set, the default is ‘ha-datacenter’, not ‘all the DC’  
as the users may expect. So unless you are aware of that, the playbook will  
access the wrong datacenter. This was source of confusion for the users.

By enforcing the parameter, we ensure the user select the correct DC.  
This is now listed in the VMware guidlines:  
[https://docs.ansible.com/ansible/devel/dev\_guide/platforms/vmware\_guidelines.html#datacenter-argument-with-esxi](https://docs.ansible.com/ansible/devel/dev_guide/platforms/vmware_guidelines.html#datacenter-argument-with-esxi)
