community.general.filesize – Create a file with a given size, or resize it if it exists
Note
This plugin is part of the community.general collection (version 3.8.3).
You might already have this collection installed if you are using the ansible
package.
It is not included in ansible-core
.
To check whether it is installed, run ansible-galaxy collection list
.
To install it, use: ansible-galaxy collection install community.general
.
To use it in a playbook, specify: community.general.filesize
.
New in version 3.0.0: of community.general
Synopsis
This module is a simple wrapper around
dd
to create, extend or truncate a file, given its size. It can be used to manage swap files (that require contiguous blocks) or alternatively, huge sparse files.
Requirements
The below requirements are needed on the host that executes this module.
dd (Data Duplicator) in PATH
Parameters
Parameter |
Comments |
---|---|
The attributes the resulting file or directory should have. To get supported flags look at the man page for chattr on the target system. This string should contain the attributes in the same order as the one displayed by lsattr. The |
|
Size of blocks, in bytes if not followed by a multiplicative suffix. The numeric value (before the unit) If not set, the size of blocks is guessed from the OS and commonly results in |
|
Whether or not to overwrite the file if it exists, in other words, to truncate it from 0. When force=true and sparse=true are mutually exclusive. Choices:
|
|
Name of the group that should own the file/directory, as would be fed to chown. |
|
The permissions the resulting file or directory should have. For those used to /usr/bin/chmod remember that modes are actually octal numbers. You must either add a leading zero so that Ansible’s YAML parser knows it is an octal number (like Giving Ansible a number without following one of these rules will end up with a decimal number which will have unexpected results. As of Ansible 1.8, the mode may be specified as a symbolic mode (for example, If If Specifying |
|
Name of the user that should own the file/directory, as would be fed to chown. |
|
Path of the regular file to create or resize. |
|
The level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the When set to |
|
The role part of the SELinux file context. When set to |
|
The type part of the SELinux file context. When set to |
|
The user part of the SELinux file context. By default it uses the When set to |
|
Requested size of the file. The value is a number (either If the multiplicative suffix is not provided, the value is treated as an integer number of blocks of blocksize bytes each (float values are rounded to the closest integer). When the size value is equal to the current file size, does nothing. When the size value is bigger than the current file size, bytes from source (if sparse is not When the size value is smaller than the current file size, it is truncated to the requested value without modifying bytes before this value. That means that a file of any arbitrary size can be grown to any other arbitrary size, and then resized down to its initial size without modifying its initial content. |
|
Device or file that provides input data to provision the file. This parameter is ignored when sparse=true. Default: “/dev/zero” |
|
Whether or not the file to create should be a sparse file. This option is effective only on newly created files, or when growing a file, only for the bytes to append. This option is not supported on OpenBSD, Solaris and AIX. force=true and sparse=true are mutually exclusive. Choices:
|
|
This option is silently ignored. This module always modifies file size in-place. Choices:
|
See Also
See also
- dd(1) manpage for Linux
Manual page of the GNU/Linux’s dd implementation (from GNU coreutils).
- dd(1) manpage for IBM AIX
Manual page of the IBM AIX’s dd implementation.
- dd(1) manpage for Mac OSX
Manual page of the Mac OSX’s dd implementation.
- dd(1M) manpage for Solaris
Manual page of the Oracle Solaris’s dd implementation.
- dd(1) manpage for FreeBSD
Manual page of the FreeBSD’s dd implementation.
- dd(1) manpage for OpenBSD
Manual page of the OpenBSD’s dd implementation.
- dd(1) manpage for NetBSD
Manual page of the NetBSD’s dd implementation.
Examples
- name: Create a file of 1G filled with null bytes
community.general.filesize:
path: /var/bigfile
size: 1G
- name: Extend the file to 2G (2*1024^3)
community.general.filesize:
path: /var/bigfile
size: 2G
- name: Reduce the file to 2GB (2*1000^3)
community.general.filesize:
path: /var/bigfile
size: 2GB
- name: Fill a file with random bytes for backing a LUKS device
community.general.filesize:
path: ~/diskimage.luks
size: 512.0 MiB
source: /dev/urandom
- name: Take a backup of MBR boot code into a file, overwriting it if it exists
community.general.filesize:
path: /media/sdb1/mbr.bin
size: 440B
source: /dev/sda
force: true
- name: Create/resize a sparse file of/to 8TB
community.general.filesize:
path: /var/local/sparsefile
size: 8TB
sparse: true
- name: Create a file with specific size and attributes, to be used as swap space
community.general.filesize:
path: /var/swapfile
size: 2G
blocksize: 512B
mode: u=rw,go=
owner: root
group: root
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
---|---|
Command executed to create or resize the file. Returned: when changed or failed Sample: “/usr/bin/dd if=/dev/zero of=/var/swapfile bs=1048576 seek=3072 count=1024” |
|
Dictionary of sizes related to the file. Returned: always |
|
Number of blocks in the file. Returned: success Sample: 500 |
|
Size of the blocks in bytes. Returned: success Sample: 1024 |
|
Size of the file, in bytes, as the product of Returned: success Sample: 512000 |
|
Size of the file, in human-readable format, following IEC standard. Returned: success Sample: “500.0 KiB” |
|
Size of the file, in human-readable format, following SI standard. Returned: success Sample: “512.0 kB” |
|
Realpath of the file if it is a symlink, otherwise the same than module’s param. Returned: always Sample: “/var/swap0” |
|
Difference (positive or negative) between old size and new size, in bytes. Returned: always Sample: -1234567890 |
Authors
quidame (@quidame)