如何用Ansible实现VMware vCenter虚拟机注销及跨集群注册?
Great question! The short answer is yes—you can fully automate both the unregistration and registration steps you described using Ansible's VMware modules. Let me walk you through how to do this, with practical playbook examples:
Unregistering the VM from the source vCenter
To remove a VM from the source vCenter inventory (without deleting its disk files), you'll use the community.vmware.vmware_guest module. The key here is to pair state: absent with delete_from_inventory: yes—this tells Ansible to only unregister the VM, not wipe its underlying storage.
Here's a sample playbook snippet:
- name: Unregister VM from source vCenter hosts: localhost gather_facts: false vars: source_vcenter_hostname: "source-vcenter.example.com" source_vcenter_username: "admin@vsphere.local" source_vcenter_password: "your-secure-password" vm_name: "my-target-vm" tasks: - name: Remove VM from source vCenter inventory community.vmware.vmware_guest: hostname: "{{ source_vcenter_hostname }}" username: "{{ source_vcenter_username }}" password: "{{ source_vcenter_password }}" validate_certs: false # Disable if using self-signed certs name: "{{ vm_name }}" state: absent delete_from_inventory: true # Critical: This won't delete disk files—only removes the VM from vCenter's inventory
Registering the VM to the target cluster
Next, to register the unregistered VM to your target cluster, you'll use the same vmware_guest module, but this time you need to point it to the VM's .vmx file path on the datastore, and specify your target cluster (or ESXi host).
Here's how that looks:
- name: Register VM to target vCenter cluster hosts: localhost gather_facts: false vars: target_vcenter_hostname: "target-vcenter.example.com" target_vcenter_username: "admin@vsphere.local" target_vcenter_password: "your-secure-password" vm_name: "my-target-vm" vmx_file_path: "[datastore-prod] VM-Folder/my-target-vm/my-target-vm.vmx" target_cluster_name: "prod-cluster-01" target_folder: "/prod-datacenter/vm/Production-VMs" # Optional: Specify target folder tasks: - name: Register VM to target cluster community.vmware.vmware_guest: hostname: "{{ target_vcenter_hostname }}" username: "{{ target_vcenter_username }}" password: "{{ target_vcenter_password }}" validate_certs: false name: "{{ vm_name }}" state: present vm_id: "{{ vmx_file_path }}" # Path to the VM's vmx file on the datastore folder: "{{ target_folder }}" cluster: "{{ target_cluster_name }}" # Use 'esxi_hostname' instead of 'cluster' if you want to register to a specific host
Important Prerequisites & Tips
- First, install the required Ansible collection: Run
ansible-galaxy collection install community.vmwareon your control node. - You'll also need VMware Python libraries: Install them with
pip install pyvmomi vmware-sdk. - To get the exact
.vmxfile path programmatically (instead of hardcoding it), you can use thecommunity.vmware.vmware_guest_infomodule on the source vCenter before unregistering the VM. - If you need to migrate the VM's storage to a new datastore alongside the cluster move, use the
community.vmware.vmware_storage_migrationmodule to handle that step first.
So to confirm: Both the manual unregister/register workflow you described, and the Ansible automation of those steps, are entirely feasible with the right modules and playbook configuration.
内容的提问来源于stack exchange,提问作者Costales

