kodo651
Contributor
Contributor

How to pass assigned VM name to cloud-init during provisioning

THE SHORT VERSION

When provisioning a new VM through the vCenter API, we need to pass the VM's given name (the name given in the vCenter UI, not the hostname) to the OS during the initial boot/provisioning phase. I'm wondering if there's any way to "interrogate" the hypervisor to give me that name from the OS level? If possible I may use that name and issue a hostnamectl command to set the static name, and, thus, being handed a correct IP from the DHCP server (sent hostname based IP allocation). Does VMware provide any API support for this kind of look up?

THE LONG VERSION Smiley Happy

We're using Rancher for provisioning k8s cluster nodes using their vSphere integration which works great. The nodes are customized by using cloud-init through cloud-config directives and everything works as advertized EXCEPT one small but, for us, very important part.

The base configuration is based on a CentOS7 7.7.1908 VM adapted for our needs, which has been converted into a template. In Rancher we have created something they call a "node template" which describes the various settings that should be assigned to the provisioned node(s). Here we also provide the cloud-config mentioned earlier which is mounted as an .iso file during provisioning.

Now, what I'd like to accomplish is to be able to pass the provisioned VM name (as seen in vCenter for example) to cloud-init so where able to set the static hostname. For us this is important because we request a specific IP based on the sent hostname to the DHCP server. Although the hostname is correctly set by the "Rancher Agent" later in the provisioning phase, we have already acquired an IP based on the VM template's hostname (which might be template-centos-7-7-1908)!

I've found a workaround which schedules (using "at") "dhclient -r; systemctl restart network ..." five minutes after the cloud-init process finishes. It works but with a major drawback - we end up with "ghost IP allocations" dangling from the initial hostname which will, eventually, vanish but we might end up in a bad situation if we ever need to re-provision a number of nodes!

So what I would like to be able to do is to inject the given VM's name into the cloud-init process so we may issue, say, "hostnamectl set-hostname $VM_NAME" where $VM_NAME is the injected variable. Is there any VMware tools/cloud-init thingy or the like invented somewhere which would help me accomplish this?

MANY thanks in advance!!!!!!!

UPDATE

The procedure mentioned above does solve the issue with worker nodes but not for the etcd/control plane nodes. They're VERY picky with changing IP:s from my experience but I might have missed something.

0 Kudos
0 Replies