Reputation: 3048
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 ...
Upvotes: 7
Views: 17977
Reputation: 21888
Don't know if it could help. But, since Ansible 2.0
there is resizefs
option in the filesystem
module that is false
by default and that supports ext4
.
If yes, if the block device and filesystem size differ, grow the filesystem into the space. Supported for ext2, ext3, ext4, ext4dev, f2fs, lvm, xfs, vfat, swap filesystems.
https://docs.ansible.com/ansible/latest/modules/filesystem_module.html
- name: 'Extend the FS'
filesystem:
fstype: 'ext4'
dev: '/dev/sda1'
resizefs: yes
Upvotes: 10
Reputation: 5927
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
Upvotes: 12
Reputation:
If you look at the source of the filesystem module as it currently stands, you can see that there doesn't seem to be anything you could use to resize a filesystem.
Thankfully, you have options: the command module and the shell module.
The command module is preferred over the shell module because it's not affected by the user environment, so I'll show you how to do it using the approach:
- name: "resize my ext4 filesystem, please"
command: resize2fs /dev/sda1
sudo: True
where sda1
is your filesystem. If you need to enlarge your filesystem first, use the same approach, but make a call to fdisk
(with the correct command line switches) instead.
Check man resize2fs
for specific options for that command and the ansible documentation for more information, including parameterizing your command.
Upvotes: 10