In modern IT environments, automation has become a cornerstone of efficiency and scalability. Ansible, a popular automation tool, allows system administrators to automate repetitive tasks like software provisioning, configuration management, and application deployment. When it comes to VMware infrastructure, Ansible can be particularly useful for managing virtualized environments, simplifying workflows, and improving consistency.
In this article, we will walk you through how to create and use Ansible playbooks for VMware infrastructure, helping you automate VMware tasks and streamline management processes. We will cover essential concepts, key components, and best practices for building robust playbooks that manage your VMware virtual machines (VMs), networks, and datastores.
Prerequisites for Using Ansible with VMware
Before we dive into the details of Ansible playbooks for VMware, it’s important to make sure your environment is set up properly:
Ansible Installation: First, you need to install Ansible on a control machine. You can install it using the package manager of your distribution. For instance, on a Red Hat-based system:
sudo yum install ansible
Or on a Debian-based system:
sudo apt-get install ansible
Python and PyVmomi: Ansible uses PyVmomi, a Python library to interact with VMware vSphere environments. Install it on your control machine:
pip install pyvmomi
Installed community.vmware ansible collection. Official documentation for the community.vmware collection can be found on the documentation link. The installation of the collection is performed with:
ansible-galaxy collection install community.vmware
Access to VMware vSphere or vCenter: Ensure that you have administrator access to your VMware infrastructure, including vSphere or vCenter credentials, as they will be required to manage virtual machines and datastores.
Creating an Ansible Playbook for VMware Infrastructure
1. Set Up Inventory and Authentication
An Ansible playbook requires an inventory file, where you define the hosts and their properties. For VMware automation, the hosts will be your VMware vSphere or vCenter instances. In addition to the inventory file, you’ll need to define your authentication parameters, such as vCenter or ESXi login credentials.
Here’s an example of an Ansible inventory file (inventory.ini
):
[vmware]
vcenter.example.com
[vmware:vars]
vcenter_server: "your_vcenter_server" # VCenter hostname or IP address
vcenter_user: "your_vcenter_user" # VCenter username
vcenter_password: "your_vcenter_password" # VCenter password
3. Basic VMware VM Management Playbook
To start automating VMware infrastructure, let’s create a basic Ansible playbook to manage virtual machines (VMs) within the VMware environment. Below is an example of an Ansible playbook that creates a new VM in vCenter.
Create a YAML file (vm_creation.yml
):
---
- name: Create a new VM in VMware vSphere
hosts: vmware
gather_facts: no
vars:
cluster_name: "your_cluster_name" # Cluster where the VM will be deployed
datastore_name: "your_datastore_name" # Datastore to store the VM
network_name: "your_network_name" # Network for the VM (e.g., "VM Network")
vm_name: "NewVM" # The name of the VM to be created
vm_template: "your_template_name" # Template to clone the VM from (optional)
vm_cpu: 2 # Number of CPUs for the VM
vm_memory: 4096 # Amount of RAM for the VM in MB
vm_disk_size: 20 # Disk size in GB
vm_guest_id: "otherGuest64" # Guest OS ID (adjust as per your OS)
vm_ip: "your_ip_address" # Optional static IP address (if required)
tasks:
- name: Create a new VM from a template in VMware vSphere
community.vmware.vm_vm_vmware:
hostname: "{{ vcenter_server }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_password }}"
cluster: "{{ cluster_name }}"
datastore: "{{ datastore_name }}"
name: "{{ vm_name }}"
template: "{{ vm_template }}"
guest_id: "{{ vm_guest_id }}"
num_cpus: "{{ vm_cpu }}"
memory_mb: "{{ vm_memory }}"
disk:
- size_gb: "{{ vm_disk_size }}"
type: thin # Or "thick" for thick provisioning
networks:
- name: "{{ network_name }}"
ip: "{{ vm_ip }}" # Optional, can be omitted if DHCP is used
validate_certs: no
state: powered_on # Optionally use "powered_off" if you don't want it powered on immediately
delegate_to: localhost
register: vm_creation_result
- name: Show VM creation result
debug:
msg: "VM '{{ vm_name }}' created successfully with the IP '{{ vm_ip }}'."
when: vm_creation_result.changed
Explanation:
Variables:
vcenter_server
,vcenter_user
,vcenter_password
: Credentials for accessing the vSphere server.cluster_name
,datastore_name
,network_name
: Specify the vSphere environment’s cluster, datastore, and network for the new VM.vm_name
: The name of the new VM you want to create.vm_template
: (Optional) The template to use for cloning the VM. If you don’t have a template, you can create a VM without this and configure it manually.vm_cpu
,vm_memory
,vm_disk_size
: These are for VM specifications like CPU count, RAM (in MB), and disk size (in GB).vm_guest_id
: The guest operating system type (adjust it according to the OS type).
Tasks:
- The task uses the
community.vmware.vm_vm_vmware
module to create a new VM using the specified settings. - The
validate_certs: no
option is used to bypass SSL certificate validation (for self-signed certs). You can set this toyes
if your vCenter uses valid certificates. - The
state: powered_on
ensures the VM is powered on after creation. You can set it topowered_off
if you don’t want the VM to be powered on immediately. - The second task is just to print out a debug message showing the result.
- The task uses the
Running the Playbook:
To run the playbook, save it to a file (e.g., vm_creation.yml) and execute it using the following command:
ansible-playbook -i inventory.ini vm_creation.yml
This playbook will automatically create a new VM in your VMware vSphere environment using the settings provided. Adjust the variables as necessary for your environment.