{"id":7282,"date":"2022-06-30T12:14:42","date_gmt":"2022-06-30T18:14:42","guid":{"rendered":"https:\/\/gregsowell.com\/?p=7282"},"modified":"2022-06-30T12:14:42","modified_gmt":"2022-06-30T18:14:42","slug":"resize-windows-disk-on-vmware-via-ansible-automation-platform","status":"publish","type":"post","link":"https:\/\/gregsowell.com\/?p=7282","title":{"rendered":"Resize Windows Disk On VMWare Via Ansible Automation Platform"},"content":{"rendered":"<p><a href=\"https:\/\/gregsowell.com\/wp-content\/uploads\/2022\/06\/Untitled-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/gregsowell.com\/wp-content\/uploads\/2022\/06\/Untitled-1-300x169.jpg\" alt=\"\" width=\"300\" height=\"169\" class=\"aligncenter size-medium wp-image-7285\" srcset=\"https:\/\/gregsowell.com\/wp-content\/uploads\/2022\/06\/Untitled-1-300x169.jpg 300w, https:\/\/gregsowell.com\/wp-content\/uploads\/2022\/06\/Untitled-1-768x432.jpg 768w, https:\/\/gregsowell.com\/wp-content\/uploads\/2022\/06\/Untitled-1-1024x576.jpg 1024w, https:\/\/gregsowell.com\/wp-content\/uploads\/2022\/06\/Untitled-1.jpg 1280w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>I really like having little bite sized demos, though some argue telling a big story works best&#8230;I suppose a mix of the two is likely the real solution.<br \/>\nThis 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.<\/p>\n<h2>Video Demo<\/h2>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/7-iq1aVeg6I\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<h2>Playbooks <\/h2>\n<p>You can find <a href=\"https:\/\/github.com\/gregsowell\/ansible-windows\/blob\/main\/vmware-win-vmdisk-extend.yml\">the playbook in question here<\/a>.<\/p>\n<p>I&#8217;ll breakdown some of its parts here:<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">  tasks:\r\n  - name: Connect to VMWare and shutdown guest\r\n    community.vmware.vmware_guest:\r\n      hostname: &quot;{{ vcenter_hostname }}&quot;\r\n      username: &quot;{{ vcenter_username }}&quot;\r\n      password: &quot;{{ vcenter_password }}&quot;\r\n      datacenter: &quot;{{ datacenter_name }}&quot;\r\n      validate_certs: no\r\n      name: &quot;{{ inventory_hostname }}&quot;\r\n      state: shutdownguest\r\n    delegate_to: localhost\r\n    failed_when: vm_shutdown.instance.hw_power_status != &#039;poweredOff&#039; and vm_shutdown.changed == false\r\n    register: vm_shutdown\r\n\r\n  - name: Wait 15 seconds\r\n    ansible.builtin.pause:\r\n      seconds: 15\r\n\r\n  - name: Connect to VMWare and resize disk on VM\r\n    community.vmware.vmware_guest_disk:\r\n      hostname: &quot;{{ vcenter_hostname }}&quot;\r\n      username: &quot;{{ vcenter_username }}&quot;\r\n      password: &quot;{{ vcenter_password }}&quot;\r\n      datacenter: &quot;{{ datacenter_name }}&quot;\r\n      validate_certs: no\r\n      name: &quot;{{ inventory_hostname }}&quot;\r\n      disk:\r\n        - size_mb: 2000\r\n          state: present\r\n          unit_number: 1\r\n          scsi_controller: 0\r\n          scsi_type: &#039;lsilogicsas&#039;\r\n    delegate_to: localhost\r\n    register: disk_facts\r\n\r\n  - name: Connect to VMWare and boot up guest\r\n    community.vmware.vmware_guest:\r\n      hostname: &quot;{{ vcenter_hostname }}&quot;\r\n      username: &quot;{{ vcenter_username }}&quot;\r\n      password: &quot;{{ vcenter_password }}&quot;\r\n      datacenter: &quot;{{ datacenter_name }}&quot;\r\n      validate_certs: no\r\n      name: &quot;{{ inventory_hostname }}&quot;\r\n      state: poweredon\r\n    delegate_to: localhost<\/code><\/pre>\n<p>Most of the work in the above is done via the vmware_guest modules from the <a href=\"https:\/\/galaxy.ansible.com\/community\/vmware\">vmware collection<\/a>.<br \/>\nI first shutdown the guest.<br \/>\nI 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&#8217;t boot!<br \/>\nAfter the resize is done, I boot the guest back up.<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">  - name: Wait 300 seconds, but only start checking after 15 seconds\r\n    wait_for_connection:\r\n      delay: 15\r\n      timeout: 300\r\n\r\n  - name: Connect to windows host and expand the drive\r\n    ansible.windows.win_powershell:\r\n      script: |\r\n        # Variable specifying the drive you want to extend\r\n        $drive_letter = &quot;E&quot;\r\n        # Script to get the partition sizes and then resize the volume\r\n        $size = (Get-PartitionSupportedSize -DriveLetter $drive_letter)\r\n        Resize-Partition -DriveLetter $drive_letter -Size $size.SizeMax<\/code><\/pre>\n<p>The last little bit is pretty straight forward.<br \/>\nI first use the wait for connection module to repeatedly test until the guest is booted back up and ready to be connected to.<br \/>\nI 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.<\/p>\n<h2>Conclusion<\/h2>\n<p>While this isn&#8217;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!<\/p>\n<p>As always, if you have any questions or comments I&#8217;d love to hear them.<br \/>\nThanks and happy automating!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I really like having little bite sized demos, though some argue telling a big story works best&#8230;I suppose a mix of the two is\u2026<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49,13,40,15],"tags":[],"class_list":["post-7282","post","type-post","status-publish","format-standard","hentry","category-ansible","category-server","category-vmware","category-windows"],"_links":{"self":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/7282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7282"}],"version-history":[{"count":3,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/7282\/revisions"}],"predecessor-version":[{"id":7286,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/7282\/revisions\/7286"}],"wp:attachment":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}