Обновить ansible/expand_ubuntu_disk.yml

This commit is contained in:
2026-05-06 19:51:51 +03:00
parent 982b79f32e
commit 76cc62c5f3
+34 -80
View File
@@ -1,15 +1,19 @@
--- ---
- name: Expand Ubuntu disk in vCenter and inside guest - name: Expand Ubuntu root disk in vCenter and inside guest
hosts: all hosts: all
gather_facts: yes gather_facts: yes
become: yes become: yes
vars: vars:
disk_unit_number: 0
disk_controller_number: 0 disk_controller_number: 0
disk_unit_number: 0
root_disk: /dev/sda
root_partition: /dev/sda3
root_partition_number: 3
root_lv: /dev/mapper/ubuntu--vg-ubuntu--lv
tasks: tasks:
- name: Gather VM info from vCenter - name: Get VM info from vCenter
community.vmware.vmware_guest_info: community.vmware.vmware_guest_info:
hostname: "{{ vcenter_hostname }}" hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}" username: "{{ vcenter_username }}"
@@ -33,13 +37,13 @@
) | int // 1024 // 1024 ) | int // 1024 // 1024
}} }}
- name: Calculate target disk size in GB - name: Calculate target disk size
ansible.builtin.set_fact: ansible.builtin.set_fact:
target_disk_gb: "{{ current_disk_gb + (increase_gb | int) }}" target_disk_gb: "{{ current_disk_gb + (increase_gb | int) }}"
- name: Show resize plan - name: Show resize plan
ansible.builtin.debug: ansible.builtin.debug:
msg: "Disk will be expanded from {{ current_disk_gb }} GB to {{ target_disk_gb }} GB" msg: "Expanding {{ vm_name }} from {{ current_disk_gb }} GB to {{ target_disk_gb }} GB"
- name: Expand disk in vCenter - name: Expand disk in vCenter
community.vmware.vmware_guest_disk: community.vmware.vmware_guest_disk:
@@ -65,97 +69,47 @@
executable: /bin/bash executable: /bin/bash
changed_when: true changed_when: true
- name: Wait a moment after rescan - name: Wait after rescan
ansible.builtin.pause: ansible.builtin.pause:
seconds: 5 seconds: 5
- name: Detect root source device
ansible.builtin.command: findmnt -n -o SOURCE /
register: root_source
changed_when: false
- name: Detect base disk and partition number
ansible.builtin.shell: |
set -e
ROOT_SRC="$(findmnt -n -o SOURCE /)"
PKNAME="$(lsblk -no PKNAME "$ROOT_SRC" | head -n1)"
PARTNUM="$(lsblk -no PARTN "$ROOT_SRC" | head -n1)"
if [ -z "$PKNAME" ]; then
PKNAME="$(basename "$(readlink -f "$ROOT_SRC")" | sed 's/[0-9]*$//')"
PARTNUM="$(basename "$(readlink -f "$ROOT_SRC")" | grep -o '[0-9]*$')"
fi
echo "DISK=/dev/${PKNAME}"
echo "PARTNUM=${PARTNUM}"
args:
executable: /bin/bash
register: root_layout
changed_when: false
- name: Parse disk layout
ansible.builtin.set_fact:
root_disk: "{{ (root_layout.stdout_lines | select('match', '^DISK=') | first).split('=')[1] }}"
root_partnum: "{{ (root_layout.stdout_lines | select('match', '^PARTNUM=') | first).split('=')[1] }}"
- name: Ensure growpart is installed - name: Ensure growpart is installed
ansible.builtin.apt: ansible.builtin.apt:
name: cloud-guest-utils name: cloud-guest-utils
state: present state: present
update_cache: yes update_cache: yes
- name: Expand root partition - name: Expand partition if needed
ansible.builtin.command: "growpart {{ root_disk }} {{ root_partnum }}" ansible.builtin.command: "growpart {{ root_disk }} {{ root_partition_number }}"
register: growpart_result register: growpart_result
changed_when: "'NOCHANGE' not in growpart_result.stdout" changed_when: "'CHANGED:' in growpart_result.stdout"
failed_when: false failed_when: >
growpart_result.rc != 0 and
- name: Detect filesystem source after partition grow 'NOCHANGE:' not in growpart_result.stdout
ansible.builtin.command: findmnt -n -o SOURCE /
register: root_source_after
changed_when: false
- name: Detect filesystem type
ansible.builtin.command: findmnt -n -o FSTYPE /
register: root_fstype
changed_when: false
- name: Check if root is on LVM
ansible.builtin.shell: |
lsblk -no TYPE "$(readlink -f {{ root_source_after.stdout | quote }})" | head -n1
args:
executable: /bin/bash
register: root_dev_type
changed_when: false
- name: Resize LVM physical volume - name: Resize LVM physical volume
ansible.builtin.command: "pvresize {{ root_disk }}{{ root_partnum }}" ansible.builtin.command: "pvresize {{ root_partition }}"
when: root_dev_type.stdout == "lvm" register: pvresize_result
changed_when: "'changed' in (pvresize_result.stdout | lower) or 'resized' in (pvresize_result.stdout | lower)"
failed_when: false failed_when: false
changed_when: true
- name: Extend root logical volume and filesystem - name: Extend root logical volume and filesystem
ansible.builtin.command: "lvextend -r -l +100%FREE {{ root_source_after.stdout }}" ansible.builtin.command: "lvextend -r -l +100%FREE {{ root_lv }}"
when: root_dev_type.stdout == "lvm" register: lvextend_result
changed_when: true changed_when: "'Size of logical volume' in lvextend_result.stdout or 'Logical volume' in lvextend_result.stdout"
failed_when: false
- name: Resize ext4 filesystem on non-LVM root - name: Show volume state
ansible.builtin.command: "resize2fs {{ root_source_after.stdout }}" ansible.builtin.shell: |
when: pvs
- root_dev_type.stdout != "lvm" vgs
- root_fstype.stdout in ["ext4", "ext3", "ext2"] lvs
changed_when: true df -h /
args:
- name: Resize xfs filesystem on non-LVM root executable: /bin/bash
ansible.builtin.command: "xfs_growfs /" register: final_state
when:
- root_dev_type.stdout != "lvm"
- root_fstype.stdout == "xfs"
changed_when: true
- name: Show final disk usage
ansible.builtin.command: df -h /
register: df_root
changed_when: false changed_when: false
- name: Print final disk usage - name: Print final state
ansible.builtin.debug: ansible.builtin.debug:
var: df_root.stdout_lines var: final_state.stdout_lines