I would like to automate the installation of the right version of vmware tools according the vSphere version of ESXi host.
There is no "right version" and no need to keep host and VMware tools version in sync. It's fully supported to run newer tools versions in VMs running on older hosts.
In fact, it's even supported to run the latest 5.1 U1 Tools on a completely unpatched ESX 4.0 host and any ESX/ESXi version in between. This also saves you tedious tools upgrades on later migrations.
See the official VMware Product Interoperability Matrixes and compare VMware Tools vs. ESX(i) host versions.
So my recommendation is to just go with the latest tools available regardless of ESX(i) version (as long as it's supported according to the matrix).