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

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) 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.

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

Here’s some:
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_snapshot_module.html
https://docs.ansible.com/ansible/latest/modules/vmware_guest_tools_upgrade_module.html

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

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

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) to cater for this but in my opinion (I support over 20,000 vms), you should probably really aim to have unique names.

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

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)
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.

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