keepcalculator

06 Numbers and Powers

Get Disks with Ansible: A Practical Guide

Card No. 06·50638 · Updated · Standard formula, shown below

Yes, you can get disk information with Ansible, and the most direct way is to use the built-in ansible_facts or the community.general.parted and community.general.filesystem modules. This guide explains the different methods, how to interpret the results, and common pitfalls, so you can reliably retrieve disk details across your servers.

Background: Why Disk Info Matters in Automation

Ansible is an automation tool that manages servers by connecting over SSH and executing tasks. Disk information is one of the most common pieces of system data you need when provisioning, monitoring, or troubleshooting. You might need to check free space before deploying an application, verify that a new disk is attached, or ensure a filesystem is mounted correctly. Ansible provides several built-in ways to collect this data without writing custom scripts.

How to Get Disk Facts with Ansible

The simplest method is to use the ansible_facts that Ansible gathers automatically when you run a playbook. These facts include ansible_mounts, which is a list of mounted filesystems with details like device, mount point, size, and usage. To see them, run:

ansible all -m setup -a 'filter=ansible_mounts'

This returns a JSON structure for each host. For a single host, you can also use the debug module in a playbook:

- hosts: all
  tasks:
    - debug:
        var: ansible_mounts

The output shows entries like:

{
    "device": "/dev/sda1",
    "mount": "/",
    "size_total": 1000000000,
    "size_available": 500000000
}

Here, size_total and size_available are in bytes. To get human-readable sizes, you can compute them in your playbook using filters like human_readable.

Using Ansible Modules for Detailed Disk Info

For more detailed information about physical disks and partitions, you need modules from the community.general collection. The community.general.parted module can list partition tables, and community.general.filesystem can check filesystem types. For example, to list partitions on a device:

- hosts: all
  tasks:
    - community.general.parted:
        device: /dev/sda
        state: info
      register: parted_info
    - debug:
        var: parted_info

This returns the partition table type (e.g., gpt or msdos) and a list of partitions with their numbers, start/end sectors, and sizes. For filesystem details, you can use the community.general.filesystem module with fstype: info:

- community.general.filesystem:
    fstype: info
    dev: /dev/sda1
  register: fs_info

This gives you the filesystem type (ext4, xfs, etc.) and block size.

Choosing the Right Method for Your Task

Deciding which method to use depends on what you need:

  • Quick free-space check: Use ansible_mounts facts. It’s built-in and requires no extra collections.
  • Partition layout: Use community.general.parted. It shows the partition table and each partition’s size.
  • Filesystem type: Use community.general.filesystem with fstype: info.
  • Raw device list: Use the ansible_devices fact, which lists all disks and their model, size, and other attributes. To see it, filter ansible_devices.

For most automation tasks, ansible_mounts is sufficient because it shows mounted filesystems, which are what your applications actually use. If you need to manage disks (create partitions, format filesystems), you’ll use the modules to change state, not just read info.

Common Problems and How to Avoid Them

Several issues can trip you up when getting disk info:

  • Facts not gathered: If you set gather_facts: no in your playbook, ansible_mounts won’t exist. You can force gathering with gather_facts: yes or use the setup module.
  • Missing collection: The community.general modules require the collection to be installed. Run ansible-galaxy collection install community.general if you get a “collection not found” error.
  • Permissions: Reading some disk info may require root privileges. If you get permission denied, ensure your Ansible user has sudo access and use become: yes.
  • Units confusion: Sizes are in bytes, not megabytes. Convert using size_total / 1024 / 1024 for MB, or use the human_readable filter.
  • Virtual disks: On cloud servers, device names like /dev/sda may be virtual. The facts still work, but the names may differ from physical hardware.

Comparing Ansible to Other Tools

Ansible is not the only way to get disk info. You could use SSH commands like df -h or lsblk directly, but Ansible provides structured data that is easier to use in automation. Compared to tools like SaltStack or Puppet, Ansible is agentless and uses SSH, which is simpler to set up. For ad-hoc queries, you can run ansible all -m command -a 'df -h', but you get plain text output that is harder to parse. The setup module gives you JSON, which is machine-readable and can be fed into other tasks.

Costs and Safety Considerations

Ansible is open-source and free to use. There is no licensing cost for the core engine. The community.general collection is also free. There are no safety risks in just reading disk info—it doesn’t modify anything. However, if you use modules like parted to change partitions or filesystem to format, you must be extremely careful. Formatting a disk erases all data. Always test on a non-production system first, and use when conditions to limit changes to specific hosts. Also, remember that disk operations can be destructive, so always back up critical data before running any playbook that modifies disks.

Frequently Asked Questions

How do I get disk usage per mount point?

Use ansible_mounts facts. Each entry has mount, size_total, and size_available. To get used space, subtract available from total. You can loop over the list and display the results with debug.

Can I get disk info for a specific device like /dev/sdb?

Yes. Use community.general.parted with device: /dev/sdb and state: info. It returns partition info for that device. For filesystem type, use community.general.filesystem with dev: /dev/sdb1 and fstype: info.

What is the difference between ansible_mounts and ansible_devices?

ansible_mounts lists mounted filesystems with sizes and usage. ansible_devices lists physical disks (like /dev/sda) with model, size, and other hardware attributes. They serve different purposes: mounts for space, devices for hardware inventory.

Why do I get “ansible_mounts is undefined”?

This happens when facts are not gathered. Ensure your playbook has gather_facts: yes (the default) or explicitly run the setup module. If you set gather_facts: no for speed, you must manually gather facts when needed.

Can I use Ansible to get disk info on Windows hosts?

Yes, but the facts are different. Windows hosts have ansible_facts['disks'] and ansible_facts['volumes']. The community.general.parted module is Linux-only, so use the win_disk_facts module for Windows.

Is there a way to get disk info without installing the community collection?

Yes, you can rely on the built-in ansible_mounts and ansible_devices facts. They are part of the core Ansible and require no extra collections. For partition tables, you would need the collection, but for most space checks, the built-in facts are enough.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →