VMware VM's Configuring Server Names

Hi

I have a playbook that creates multiple VM’s in VMware (see below).

When the VM’s have all been created they have the same server name as the template. So I have 20 new VM’s all with the same OS name.

I want the OS server name to be changed to the same name as the Vm host name that the Ansible playbook gives it when it runs. Is there any way of doing this? It’s obviously pointless creating multiple servers all with the same name.

I can easily do this using VMware PowerCLI and associating a VMware Customization Spec to the VM template:

$esxName = “172.22.22.22”

$template = “WIN2012R2-TEMPLATE”

$datastore = “IE-VMFS5-32”

$newVmList = “DR-EA05”, “DR-EA06”

$custSpec = “Spectest”

$location = “Alphanew”

$taskTab = @{}

Create all the VMs specified in $newVmList

foreach($Name in $newVmList) {

$taskTab[(New-VM -Name $Name -VMHost (Get-VMHost -Name $esxName) -Template $template -Datastore $datastore -OSCustomizationSpec $custSpec -Location $location -RunAsync).Id] = $Name

}

Is there anyway of adding a VMware Customization spec to the Ansible playbook? Or any other way than you can think of that I can use to set the new VM’s server name?

Playbook:

Hi Mark,

There are two as-yet-not-merged Pull Requests that tackle renaming windows hosts:

https://github.com/ansible/ansible-modules-extras/pull/213 and https://github.com/ansible/ansible-modules-extras/pull/33

Would you able to test them out and see what works and make most sense to use?

Changing hostnames obviously has an effect on inventory so, depending on what you need to do to a machine created from a template before you rename it, you may or may not need to have a sort ‘new machines’ inventory so you can run ansible modules against the newly-created-but-not-yet renamed vms.

I would test myself but I am waiting for a vsphere upgrade before I can get as far as you have done.

I don’t know if you are planning on putting your created-from-template VMs onto a domain but I have some prototype code which can use Active Directory as a source of dynamic inventory. If you are interested let me know and I’ll share the code. I haven’t created a PR yet as I haven’t worked out a really good way to secure the credentials needed for querying the domain controller, but that may or may not matter depending on your security requirements.

All the best,

Jon