BacklogZero®

Ansible in a Minute · Ansible Core ·

Use human_to_bytes to parse a size string

Short answer

Pipe a size string through human_to_bytes so a task gets a raw byte integer such as 10240.

Ansible in a Minute — human_to_bytes

Subscribe for updates

Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.

By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.

Request

print the string '10.00 KB' with human_to_bytes so the message is 10240

Example

A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.

YAML
---
# Prompt: print the string '10.00 KB' with human_to_bytes so the message is 10240
- name: Print the string '10.00 KB' with human_to_bytes
  hosts: all
  gather_facts: false
  tasks:
    - name: Convert '10.00 KB' to bytes
      ansible.builtin.set_fact:
        bytes_value: "{{ '10.00 KB' | ansible.builtin.human_to_bytes }}"

    - name: Print the converted bytes value
      ansible.builtin.debug:
        msg: "{{ bytes_value }}"

How it works

set_fact stores the string 10.00 KB piped through ansible.builtin.human_to_bytes. debug prints that integer. The filter turns a size string into a byte count. 10.00 KB becomes 10240. Use it when a module argument must be an integer.

When to use it

  • Converting a quota string before a disk or file task
  • Passing a byte size to a module that does not accept KB

Requirements

  • Ansible: >=2.14

Validation

  • Syntax validated
  • Not execution-tested
  • Last verified 2026-10-20

Notes

Pass isbits=True when the string uses bit units such as Kb. Values between 0 and 1 round to the nearest byte. Use human_readable when people must read the size.

Reference

Built for every Ansible use case in every organization

BacklogZero can generate this kind of automation in your environment, just like it did here.

Get BacklogZero today →

Related guides

← Back to Ansible in a Minute

Use human_to_bytes to parse a size string · Level Up Labs