diff --git a/ansible/expand_ubuntu_disk_workflow.yml b/ansible/expand_ubuntu_disk_workflow.yml index 50eef39..56b09d7 100644 --- a/ansible/expand_ubuntu_disk_workflow.yml +++ b/ansible/expand_ubuntu_disk_workflow.yml @@ -1,52 +1,11 @@ -- name: Stage 1 - Resize disk in vCenter with Terraform +--- +- name: Stage 1 - Add Ubuntu VM to in-memory inventory hosts: localhost connection: local gather_facts: false become: false - vars: - tf_dir: "{{ playbook_dir }}/../terraform/resize-vm-disk" - tasks: - - name: Create terraform CLI config - ansible.builtin.copy: - dest: "/tmp/.terraformrc" - content: | - provider_installation { - direct {} - } - - - name: Cleanup old terraform files - ansible.builtin.shell: rm -rf .terraform .terraform.lock.hcl - args: - chdir: "{{ tf_dir }}" - - - name: Terraform init - ansible.builtin.shell: terraform init -reconfigure -no-color - args: - chdir: "{{ tf_dir }}" - environment: - TF_CLI_CONFIG_FILE: "/tmp/.terraformrc" - TF_VAR_vsphere_server: "{{ vsphere_server }}" - TF_VAR_vsphere_datacenter: "{{ vsphere_datacenter }}" - TF_VAR_vsphere_user: "{{ vsphere_user }}" - TF_VAR_vsphere_password: "{{ vsphere_password }}" - TF_VAR_vm_name: "{{ vm_name }}" - TF_VAR_increase_gb: "{{ increase_gb }}" - - - name: Terraform apply - ansible.builtin.shell: terraform apply -auto-approve -no-color -lock=false - args: - chdir: "{{ tf_dir }}" - environment: - TF_CLI_CONFIG_FILE: "/tmp/.terraformrc" - TF_VAR_vsphere_server: "{{ vsphere_server }}" - TF_VAR_vsphere_datacenter: "{{ vsphere_datacenter }}" - TF_VAR_vsphere_user: "{{ vsphere_user }}" - TF_VAR_vsphere_password: "{{ vsphere_password }}" - TF_VAR_vm_name: "{{ vm_name }}" - TF_VAR_increase_gb: "{{ increase_gb }}" - - name: Add Ubuntu VM to in-memory inventory ansible.builtin.add_host: name: ubuntu-resize-target @@ -58,16 +17,65 @@ ansible_become_password: "{{ ansible_become_password }}" ansible_ssh_extra_args: "-o StrictHostKeyChecking=no" - - name: Stage 2 - Wait for SSH hosts: ubuntu_resize_group gather_facts: false + tasks: - name: Wait for SSH connection ansible.builtin.wait_for_connection: timeout: 300 -- name: Stage 3 - Expand partition, LVM and filesystem +- name: Stage 3 - Read current guest disk size + hosts: ubuntu_resize_group + gather_facts: false + become: true + + tasks: + - name: Get current size of /dev/sda in bytes + ansible.builtin.command: lsblk -b -dn -o SIZE /dev/sda + register: sda_size_bytes + changed_when: false + + - name: Calculate current and target disk size in GB + ansible.builtin.set_fact: + current_disk_gb: "{{ (sda_size_bytes.stdout | int) // 1024 // 1024 // 1024 }}" + target_disk_gb: "{{ ((sda_size_bytes.stdout | int) // 1024 // 1024 // 1024) + (increase_gb | int) }}" + +- name: Stage 4 - Expand disk in vCenter + hosts: localhost + connection: local + gather_facts: false + become: false + + vars: + ansible_python_interpreter: /opt/semaphore/apps/ansible/11.1.0/venv/bin/python3 + + tasks: + - name: Show resize plan + ansible.builtin.debug: + msg: + - "VM: {{ vm_name }}" + - "VM IP: {{ vm_ip }}" + - "Current disk: {{ hostvars['ubuntu-resize-target']['current_disk_gb'] }} GB" + - "Target disk: {{ hostvars['ubuntu-resize-target']['target_disk_gb'] }} GB" + + - name: Expand VMware disk + community.vmware.vmware_guest_disk: + hostname: "{{ vsphere_server }}" + username: "{{ vsphere_user }}" + password: "{{ vsphere_password }}" + validate_certs: false + datacenter: "{{ vsphere_datacenter }}" + name: "{{ vm_name }}" + disk: + - state: present + unit_number: 0 + controller_number: 0 + size_gb: "{{ hostvars['ubuntu-resize-target']['target_disk_gb'] }}" + delegate_to: localhost + +- name: Stage 5 - Expand partition, LVM and filesystem inside Ubuntu hosts: ubuntu_resize_group gather_facts: false become: true