vCenter - Linux Templates
To deploy multiple VMs with different hostnames and IP addresses while utilizing the customization capabilities provided by the vmware_guest
module in Ansible, you can use VMware's customization specifications. This approach allows for more advanced customization options, such as setting the domain, hostname, and network settings directly within the playbook. Below is an example of how to modify the playbook to use VMware's customization feature for deploying 3 VMs with distinct configurations:
Step 1: Update the Playbook with Customization Specifications
You'll need to define a customization spec for each VM, which can be included directly in the loop. Here's how the updated playbook could look:
---
- name: Deploy multiple Linux VMs from a template with customization on vCenter
hosts: localhost
gather_facts: no
tasks:
- name: Clone VMs from template with customization
community.vmware.vmware_guest:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_user }}'
password: '{{ vcenter_pass }}'
validate_certs: no
datacenter: '{{ datacenter_name }}'
folder: '{{ vm_folder }}'
name: "{{ item.vm_name }}"
template: '{{ vm_template }}'
state: poweredon
disk:
- size_gb: '{{ item.disk_size_gb | default(disk_size_gb) }}'
type: thin
datastore: '{{ datastore_name }}'
hardware:
memory_mb: '{{ item.vm_memory_mb | default(vm_memory_mb) }}'
num_cpus: '{{ item.vm_cpus | default(vm_cpus) }}'
networks:
- name: '{{ network_name }}'
device_type: vmxnet3
customization:
hostname: "{{ item.vm_name }}"
domain: '{{ vm_domain }}'
ipsettings:
ip: "{{ item.vm_ip }}"
subnet_mask: '{{ vm_netmask }}'
gateway: '{{ vm_gateway }}'
dns_servers: "{{ item.vm_dns_servers | default(vm_dns_servers) }}"
wait_for_customization: yes
delegate_to: localhost
loop: "{{ vms }}"
vars:
vcenter_hostname: 'vcenter.example.com'
vcenter_user: 'your-username'
vcenter_pass: 'your-password'
datacenter_name: 'your-datacenter'
vm_folder: '/your-vm-folder'
vm_template: 'your-linux-template'
disk_size_gb: 20 # Default disk size
datastore_name: 'your-datastore'
vm_memory_mb: 2048 # Default memory
vm_cpus: 2 # Default CPU count
network_name: 'VM Network'
vm_domain: 'example.com'
vm_netmask: '255.255.255.0'
vm_gateway: '192.168.1.1'
vm_dns_servers:
- '8.8.8.8'
- '8.8.4.4'
vms:
- vm_name: 'linux-vm-01'
vm_ip: '192.168.1.101'
- vm_name: 'linux-vm-02'
vm_ip: '192.168.1.102'
- vm_name: 'linux-vm-03'
vm_ip: '192.168.1.103'
In this playbook, each VM's customization is defined within the customization
parameter of the vmware_guest
module. This includes settings for hostname, domain, IP, subnet mask, gateway, and DNS servers. The wait_for_customization: yes
option ensures Ansible waits until VMware's customization process completes before moving on.
Step 2: Running the Playbook
To run the updated playbook, use the same command as before:
ansible-playbook -i hosts.ini deploy_vm.yml
This command deploys 3 VMs, each with its customized hostname and network configuration as defined in the vms
list.
Notes:
- Template Requirements: The template you use must be prepared for customization. For Linux VMs, ensure VMware Tools is installed, and the Perl scripting language is available for the customization scripts to run.
- Customization Script: VMware's customization mechanism uses a script that runs on the first boot. If the customization does not apply, troubleshooting may involve checking that VMware Tools is correctly installed and that the template is properly prepared for cloning and customization.
- Ansible and VMware Versions: Ensure you are using recent versions of Ansible and the VMware modules, as improvements and bug fixes are regularly added.
This method leverages VMware's powerful customization engine, allowing for a wide range of customization options beyond what was demonstrated here.