Turn the page, wash your Large files

- 4 mins

Creating Large Files on Linux with fallocate for Testing Ansible Playbooks and Storage Systems

When working with automation tools like Ansible or managing storage systems, it’s common to test scenarios involving large files — whether you’re verifying delete operations, assessing disk space thresholds, or benchmarking storage performance. Instead of copying actual large datasets, you can simulate them by using the fallocate command in Linux.

In this blog post, we’ll walk through:

Why Create Large Files for Testing?

Here are a few practical scenarios:

Using the fallocate command

The fallocate command is the fastest way to create large files in Linux. It allocates space at the filesystem level without actually writing data, making it nearly instant.

Command example:

fallocate -l <size> <filename>
Example: Create a 3GB File
fallocate -l 3G /tmp/testfile.bin

This creates a 3GB file at /tmp/testfile.bin in a fraction of a second. It appears fully allocated on disk but is not filled with data like zeroes or random bytes. This helps trigger low-disk-space alerts or test how your application reacts when the disk is nearly full.

Note: Some filesystems (e.g., XFS, ext4, Btrfs) support fallocate, but others (like FAT32 or some network filesystems) might not.

 ## Use Case: Testing Ansible File Deletion Playbook

Suppose you’re writing an Ansible playbook to delete files larger than 3B in /var/log/test:

Step 1: Create Large Files with fallocate
mkdir -p /var/log/test
fallocate -l 3G /var/log/test/testfile1.bin
fallocate -l 1G /var/log/test/testfile2.bin
Step 2: Create Ansible playbook to Remove Large Files
   - name: Find files greater than or equal to 3GB in /var/log/test 
     find:
       paths: "/var/log/test"
       recurse: yes
       size: "2g"
       file_type: file
     register: event_large_files

   - name: Display files that will be deleted (for logging visibility)
     debug:
       msg: ""
     loop: ""
     when: event_large_files.matched > 0

   - name: Delete files greater than or equal to 2GB
     file:
       path: ""
       state: absent
     loop: ""
     when: event_large_files.matched > 0
Step 3: Run the Playbook
ansible-playbook cleanup_large_files.yml

You’ve now safely tested your logic against dummy data without risking production logs or valuable files being tested or removed accidentally.

Conclusion and TLDR

Creating large files with fallocate is a fast, efficient, and safe way to simulate data for testing Ansible playbooks, storage thresholds, and system performance. Whether you’re a systems engineer, SRE, or DevOps practitioner, mastering this tool can help you create realistic test environments without waiting hours to copy or generate large datasets.

Scenario:

Storage space is running out on a linux server. The files taking up space are iologs and logs larger than 3GB. The team has developed an ansible playbook to remove logs larger than 3GB in specific dirs but the storage issues have been resolved on the servers that were reporting space issues. How can we reproduce large files on a development server to test the ansible playbooks and confirm it will pick up large files and remove them accordingly? 

Solution:

The fallocate command is a fast and efficient way to create large files for testing and development purposes on Linux systems. It allocates disk space to a file without actually writing data to it, making it ideal for simulating large files quickly. This is particularly useful for testing scripts or systems that handle file size thresholds, such as removing files larger than 3GB or validating storage limits.

To create a 3GB file using fallocate, you can run the following command:

fallocate -l 3G /tmp/dummy-file.bin