How I can resize ext4
filesystem via Ansible? I know about the filesystem module, but I didn't see a "resize" parameter, or something else about it ...
Use the appropriate resizing methods for the affected block device. When resizing an ext4 file system, the resize2fs utility reads the size in units of file system block size, unless a suffix indicating a specific unit is used. The following suffixes indicate specific units: s — 512 byte sectors.
In that case, you can use the resize2fs utility to increase and decrease a filesystem size. The resize2fs is a command-line utility that allows you to resize ext2, ext3, or ext4 file systems.
The size of an XFS file system can be increased by using the xfs_growfs command when the file system is mounted. Reducing the size of an XFS file system is not possible.
In order to make the task idempotent, add another task to first check for any unexpanded partitions. E.g., if you want the root partition to be at least 10 GB:
- name: Assert root partition is expanded
assert: { that: item.mount != '/' or item.size_total > 10737418240 } # 10 GB
with_items: '{{ ansible_mounts }}'
ignore_errors: yes
register: expanded
NOTE: This task fails if the partition /
is less than 10 GB.
Next, make the expansion task conditional on expanded|failed
:
- name: Expand partition
command: parted /dev/mmcblk0 resizepart 2 15.5GB # NOTE: Assumes 16GB card
when: expanded|failed
notify: Expand filesystem
In my case, I'm expanding partition 2 on the block device /dev/mmcblk0
(for the Raspberry Pi). You should of course replace with the device names on your system.
Finally, notify
triggers filesystem expansion:
handlers:
- name: Expand filesystem
command: resize2fs /dev/mmcblk0p2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With