This guide will show you how to utilize Ansible to rename an existing virtual machine.
Software
Pyvmomi
must be installed on the Ansible control node (or Target host if not executing against localhost).pip install Pyvmomi
(as the OS packages are usually out of date and incompatible).Hardware
Access / Credentials
validate_certs
option, as this version is capable of changing the SSL verification behaviours.With the following Ansible playbook you can rename an existing virtual machine by changing the UUID.
--- - name: Rename virtual machine from old name to new name using UUID gather_facts: no vars_files: - vcenter_vars.yml vars: ansible_python_interpreter: "/usr/bin/env python3" hosts: localhost tasks: - set_fact: vm_name: "old_vm_name" new_vm_name: "new_vm_name" datacenter: "DC1" cluster_name: "DC1_C1" - name: Get VM "{{ vm_name }}" uuid vmware_guest_facts: hostname: "{{ vcenter_server }}" username: "{{ vcenter_user }}" password: "{{ vcenter_pass }}" validate_certs: False datacenter: "{{ datacenter }}" folder: "/{{datacenter}}/vm" name: "{{ vm_name }}" register: vm_facts - name: Rename "{{ vm_name }}" to "{{ new_vm_name }}" vmware_guest: hostname: "{{ vcenter_server }}" username: "{{ vcenter_user }}" password: "{{ vcenter_pass }}" validate_certs: False cluster: "{{ cluster_name }}" uuid: "{{ vm_facts.instance.hw_product_uuid }}" name: "{{ new_vm_name }}"
Since Ansible utilizes the VMware API to perform actions, in this use case it will be connecting directly to the API from localhost.
This means that playbooks will not be running from the vCenter or ESXi Server.
Note that this play disables the gather_facts
parameter, since you don’t want to collect facts about localhost.
You can run these modules against another server that would then connect to the API if localhost does not have access to vCenter. If so, the required Python modules will need to be installed on that target server. We recommend installing the latest version with pip: pip install Pyvmomi
(as the OS packages are usually out of date and incompatible).
Before you begin, make sure you have:
For now, you will be entering these directly, but in a more advanced playbook this can be abstracted out and stored in a more secure fashion using ansible-vault or using Ansible Tower credentials.
If your vCenter or ESXi server is not setup with proper CA certificates that can be verified from the Ansible server, then it is necessary to disable validation of these certificates by using the validate_certs
parameter. To do this you need to set validate_certs=False
in your playbook.
Now you need to supply the information about the existing virtual machine which will be renamed. For renaming virtual machine, vmware_guest
module uses VMware UUID, which is unique across vCenter environment. This value is autogenerated and can not be changed. You will use vmware_guest_facts
module to find virtual machine and get information about VMware UUID of the virtual machine.
This value will be used input for vmware_guest
module. Specify new name to virtual machine which conforms to all VMware requirements for naming conventions as name
parameter. Also, provide uuid
as the value of VMware UUID.
Running this playbook can take some time, depending on your environment and network connectivity. When the run is complete you will see
{ "changed": true, "instance": { "annotation": "", "current_snapshot": null, "customvalues": {}, "guest_consolidation_needed": false, "guest_question": null, "guest_tools_status": "guestToolsNotRunning", "guest_tools_version": "10247", "hw_cores_per_socket": 1, "hw_datastores": ["ds_204_2"], "hw_esxi_host": "10.x.x.x", "hw_eth0": { "addresstype": "assigned", "ipaddresses": [], "label": "Network adapter 1", "macaddress": "00:50:56:8c:b8:42", "macaddress_dash": "00-50-56-8c-b8-42", "portgroup_key": "dvportgroup-31", "portgroup_portkey": "15", "summary": "DVSwitch: 50 0c 3a 69 df 78 2c 7b-6e 08 0a 89 e3 a6 31 17" }, "hw_files": ["[ds_204_2] old_vm_name/old_vm_name.vmx", "[ds_204_2] old_vm_name/old_vm_name.nvram", "[ds_204_2] old_vm_name/old_vm_name.vmsd", "[ds_204_2] old_vm_name/vmware.log", "[ds_204_2] old_vm_name/old_vm_name.vmdk"], "hw_folder": "/DC1/vm", "hw_guest_full_name": null, "hw_guest_ha_state": null, "hw_guest_id": null, "hw_interfaces": ["eth0"], "hw_is_template": false, "hw_memtotal_mb": 1024, "hw_name": "new_vm_name", "hw_power_status": "poweredOff", "hw_processor_count": 1, "hw_product_uuid": "420cbebb-835b-980b-7050-8aea9b7b0a6d", "hw_version": "vmx-13", "instance_uuid": "500c60a6-b7b4-8ae5-970f-054905246a6f", "ipv4": null, "ipv6": null, "module_hw": true, "snapshots": [] } }
confirming that you’ve renamed the virtual machine.
If your playbook fails:
© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.10/scenario_guides/vmware_scenarios/scenario_rename_vm.html