Resize Windows Disk On VMWare Via Ansible Automation Platform
I really like having little bite sized demos, though some argue telling a big story works best…I suppose a mix of the two is likely the real solution.
This demo shows the Ansible Automation Platform(AAP) connecting to VMware Vcenter to expand a disk on a windows host. AAP then connects to the disk and expands the disk in windows itself.
Video Demo
Playbooks
You can find the playbook in question here.
I’ll breakdown some of its parts here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | tasks: - name: Connect to VMWare and shutdown guest community.vmware.vmware_guest: hostname: "{{ vcenter_hostname }}" username: "{{ vcenter_username }}" password: "{{ vcenter_password }}" datacenter: "{{ datacenter_name }}" validate_certs: no name: "{{ inventory_hostname }}" state: shutdownguest delegate_to: localhost failed_when: vm_shutdown.instance.hw_power_status != 'poweredOff' and vm_shutdown.changed == false register: vm_shutdown - name: Wait 15 seconds ansible.builtin.pause: seconds: 15 - name: Connect to VMWare and resize disk on VM community.vmware.vmware_guest_disk: hostname: "{{ vcenter_hostname }}" username: "{{ vcenter_username }}" password: "{{ vcenter_password }}" datacenter: "{{ datacenter_name }}" validate_certs: no name: "{{ inventory_hostname }}" disk: - size_mb: 2000 state: present unit_number: 1 scsi_controller: 0 scsi_type: 'lsilogicsas' delegate_to: localhost register: disk_facts - name: Connect to VMWare and boot up guest community.vmware.vmware_guest: hostname: "{{ vcenter_hostname }}" username: "{{ vcenter_username }}" password: "{{ vcenter_password }}" datacenter: "{{ datacenter_name }}" validate_certs: no name: "{{ inventory_hostname }}" state: poweredon delegate_to: localhost |
Most of the work in the above is done via the vmware_guest modules from the vmware collection.
I first shutdown the guest.
I then do the resize on the disk in question. Something I learned is that, while optional, the scsi_type parameter needs to be specified. It will assume a default(likely other than is already set), and then your VM guest likely won’t boot!
After the resize is done, I boot the guest back up.
1 2 3 4 5 6 7 8 9 10 11 12 13 | - name: Wait 300 seconds, but only start checking after 15 seconds wait_for_connection: delay: 15 timeout: 300 - name: Connect to windows host and expand the drive ansible.windows.win_powershell: script: | # Variable specifying the drive you want to extend $drive_letter = "E" # Script to get the partition sizes and then resize the volume $size = (Get-PartitionSupportedSize -DriveLetter $drive_letter) Resize-Partition -DriveLetter $drive_letter -Size $size.SizeMax |
The last little bit is pretty straight forward.
I first use the wait for connection module to repeatedly test until the guest is booted back up and ready to be connected to.
I next use the win_powershell module to tell the drive to expand using the newly adjusted vmware disk size. Note that I could have used an ansible module, but at the time of creation I was trying to demonstrate how to use powershell scripts directly.
Conclusion
While this isn’t revolutionary, it is very convenient. I can imagine monitoring my server infrastructure for disk utilization. When I violate a certain threshold my monitoring system will call the AAP API and pass over the name of the host, and the offending drive. AAP can then automatically connect to the server and resize the disk, then create a ticket or send an email. This feels pretty powerful to me!
As always, if you have any questions or comments I’d love to hear them.
Thanks and happy automating!