Expand a Virtual Disk in VMware vSphere Virtual Machine - Ansible module vmware_guest_disk
How to automate the expansion from 1GB to 2GB size Virtual Disk connected to VMware Virtual Machine guest named “myvm” using Ansible Playbook and vmware_guest_disk module.


How to Expand a Virtual Disk in VMware vSphere Virtual Machine with Ansible?
I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Ansible Expand a Virtual Disk in VMware vSphere Virtual Machine
community.vmware.vmware_guest_disk
- Manage disks related to a virtual machine in a given vCenter infrastructure
Let’s talk about the Ansible module vmware_guest_disk
.
The full name is community.vmware.vmware_guest_disk
, which means that is part of the collection of modules to interact with VMware, community-supported.
It manages disks related to a virtual machine in a given vCenter infrastructure.
Parameters
- hostname string / username string / password string / datacenter string / validate_certs boolean - connection details
- datacenter string - The datacenter name to which the virtual machine belongs to
- scsi_controller / unit_number / scsi_type string - SCSI controller details
- size / size_kb / size_mb / size_gb / size_tb string - Disk storage size
- disk_mode string - persistent / independent_persistent / independent_nonpersistent
The following parameters are useful in order to Expand a Virtual Disk in VMware vSphere Virtual Machine using the module vmware_guest_disk
.
First of all, we need to establish the connection with VMware vSphere or VMware vCenter using a plethora self-explicative parameters: hostname
, username
, password
, datacenter
, and validate_certs
.
Once the connection is successfully established you could specify the desired disk configuration, in this expansion, a disk is connected to a virtual machine.
The mandatory parameters are only datacenter
and unit_number
.
The datacenter
parameter specifies which datacenter name the virtual machine belongs to, for resources allocations.
The disk must be connected to a SCSI controller inside the virtual machine, so you should specify all the small details like scsi_controller
, unit_number
, and scsi_type
.
You might be interested in deep-diving into some performance analysis to properly adjust these parameters.
You could specify the disk size via various parameters according to the needed size unit: kb, MB, GB, TB, etc.
One most important parameter is the disk_mode
, default to persistent
mode, other options are independent_persistent
and independent_nonpersistent
.
Links
The Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
How to Expand a Virtual Disk in VMware vSphere Virtual Machine with Ansible. I’m going to show you how to expand the size of an additional disk connected to a Virtual Machine named “myvm” using Ansible Playbook. The disk is connected to a SCSI controller number 1 and has unit number 1. The beginning of the demo has a size of 1 GB and we would like to expand it to 2 GB.
code
- vm_disk_expand.yml
---
- name: vm disk demo
hosts: localhost
become: false
gather_facts: false
collections:
- community.vmware
pre_tasks:
- include_vars: vars.yml
tasks:
- name: expand disk in vm
vmware_guest_disk:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: "{{ vcenter_validate_certs }}"
datacenter: "{{ vcenter_datacenter }}"
name: "{{ vm_name }}"
disk:
- size_gb: "{{ vm_disk_gb }}"
type: "{{ vm_disk_type }}"
datastore: "{{ vm_disk_datastore }}"
state: present
scsi_controller: "{{ vm_disk_scsi_controller }}"
unit_number: "{{ vm_disk_unit }}"
scsi_type: "{{ vm_disk_scsi_type }}"
disk_mode: "{{ vm_disk_mode }}"
- vars.yml
---
vcenter_hostname: "vmware.example.com"
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "[email protected]"
vcenter_password: "MySecretPassword123"
vm_name: "myvm"
vm_disk_gb: 2
vm_disk_type: "thin"
vm_disk_datastore: "datastore"
vm_disk_scsi_controller: 1
vm_disk_scsi_unit: 1
vm_disk_scsi_type: 'paravirtual'
vm_disk_mode: 'persistent'
- inventory
localhost
execution
$ ansible-playbook vm_disk_expand.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [vm disk demo] *******************************************************************************
TASK [include_vars] *******************************************************************************
ok: [localhost]
TASK [expand disk in vm] **************************************************************************
changed: [localhost]
PLAY RECAP ****************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
idempotency
$ ansible-playbook vm_disk_expand.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [vm disk demo] *******************************************************************************
TASK [include_vars] *******************************************************************************
ok: [localhost]
TASK [expand disk in vm] **************************************************************************
ok: [localhost]
PLAY RECAP ****************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
before execution
after execution
Recap
Now you know how to Expand a Virtual Disk in VMware vSphere Virtual Machine with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate