135 lines
3.9 KiB
YAML
135 lines
3.9 KiB
YAML
- name: Stage 1 - Resize disk in vCenter with Terraform
|
|
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
|
|
groups: ubuntu_resize_group
|
|
ansible_host: "{{ vm_ip }}"
|
|
ansible_user: "{{ ansible_user }}"
|
|
ansible_password: "{{ ansible_password }}"
|
|
ansible_become: true
|
|
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
|
|
hosts: ubuntu_resize_group
|
|
gather_facts: false
|
|
become: true
|
|
|
|
vars:
|
|
root_disk: /dev/sda
|
|
root_partition_number: 3
|
|
root_partition: /dev/sda3
|
|
root_lv: /dev/mapper/ubuntu--vg-ubuntu--lv
|
|
|
|
tasks:
|
|
- name: Rescan SCSI bus
|
|
ansible.builtin.shell: |
|
|
for host in /sys/class/scsi_host/host*; do
|
|
echo "- - -" > "$host/scan"
|
|
done
|
|
args:
|
|
executable: /bin/bash
|
|
changed_when: true
|
|
|
|
- name: Wait after rescan
|
|
ansible.builtin.pause:
|
|
seconds: 5
|
|
|
|
- name: Ensure growpart is installed
|
|
ansible.builtin.apt:
|
|
name: cloud-guest-utils
|
|
state: present
|
|
update_cache: yes
|
|
|
|
- name: Expand partition if needed
|
|
ansible.builtin.command: "growpart {{ root_disk }} {{ root_partition_number }}"
|
|
register: growpart_result
|
|
changed_when: "'CHANGED:' in growpart_result.stdout"
|
|
failed_when: >
|
|
growpart_result.rc != 0 and
|
|
'NOCHANGE:' not in growpart_result.stdout
|
|
|
|
- name: Resize LVM physical volume
|
|
ansible.builtin.command: "pvresize {{ root_partition }}"
|
|
register: pvresize_result
|
|
failed_when: false
|
|
changed_when: true
|
|
|
|
- name: Extend root logical volume and filesystem
|
|
ansible.builtin.command: "lvextend -r -l +100%FREE {{ root_lv }}"
|
|
register: lvextend_result
|
|
failed_when: false
|
|
changed_when: true
|
|
|
|
- name: Show final disk state
|
|
ansible.builtin.shell: |
|
|
pvs
|
|
vgs
|
|
lvs
|
|
df -h /
|
|
args:
|
|
executable: /bin/bash
|
|
register: final_state
|
|
changed_when: false
|
|
|
|
- name: Print final disk state
|
|
ansible.builtin.debug:
|
|
var: final_state.stdout_lines
|