Skip to content

Commit

Permalink
improve expand_root_partition.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
huaqing1994 committed Dec 31, 2024
1 parent 511af15 commit e654fc8
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions pkg/hostagent/tasks/expand_root_partition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,47 @@
- name: Expand root partition
hosts: all
become: true
gather_facts: false
gather_facts: true
tasks:
- name: Get root path
- name: Set root device
ansible.builtin.set_fact:
rootdevice: >-
{{ ansible_mounts | selectattr('mount', 'equalto', '/') | map(attribute='device') | first }}
- name: Set root partition
ansible.builtin.set_fact:
rootpartition: >-
{{ ansible_devices
| dict2items
| map(attribute='value.partitions')
| map('dict2items')
| flatten(1)
| selectattr('value.holders', 'defined')
| selectattr('value.holders', 'contains', rootdevice.split('/')[-1])
| map(attribute='key')
| first }}
- name: Grow partition
shell: |
. /etc/os-release
rootpath="/dev/mapper/rl-root"
if [[ $ID == 'openEuler' ]]; then
rootpath="/dev/mapper/openeuler-root"
fi
echo $rootpath
register: rootpath
- name: Grow vda2
shell: |
result=$(growpart /dev/vda 2)
if [[ $? == 0 ]]; then
result=$(growpart /dev/{{ rootpartition | regex_replace('[0-9]+$', '') }} {{ rootpartition | regex_search('[0-9]+$') }})
if [ $? -eq 0 ]; then
echo "$result"
elif [[ $result == NOCHANGE* ]]; then
elif echo "$result" | grep -q '^NOCHANGE'; then
echo "$result"
else
echo "$result"
exit 1
fi
- name: Resize vda2
shell: pvresize /dev/vda2
- name: Resize partition
shell: pvresize /dev/{{ rootpartition }}

- name: Extend root
shell: |
result=$(lvextend -r -l+100%FREE -n {{ rootpath.stdout }} 2>&1)
if [[ $? == 0 ]]; then
result=$(lvextend -r -l+100%FREE -n {{ rootdevice }} 2>&1)
if [ $? -eq 0 ]; then
echo "$result"
elif [[ $result == *'matches existing size'* ]]; then
elif echo "$result" | grep -q 'matches existing size'; then
echo "$result"
else
echo "$result"
Expand Down

0 comments on commit e654fc8

Please sign in to comment.