Skip to Content

2 ways to get disks info with Ansible

This article is part of the following series.


Get all the disks with ansible_facts in Ansible

You can use the ansible_facts module in Ansible to gather information about disks on remote hosts. The ansible_facts module automatically collects facts about the system where Ansible is running, including disk information. Here’s an example playbook that lists all the disks on remote hosts:

- name: Gather Disk Information
  hosts: your_hosts
  gather_facts: yes
    - name: Display Disk Information
        var: ansible_facts.devices
    - name:
        var: ansible_facts.devices.keys()

In this playbook, replace your_hosts with the target hosts where you want to gather disk information. The gather_facts: yes option tells Ansible to collect facts about the remote hosts before executing tasks.

“ansible_facts” is a predefined variable that contains facts or information about the remote system being managed. The “ansible_facts.devices” dictionary is a sub-dictionary within “ansible_facts” that contains information about the devices or hardware components on the remote system.

You can use the debug module to display the contents of this variable, which will list all the disks along with their attributes, such as size, partitions, file system, etc.

The “ansible_facts.devices.keys()” expression refers to the keys or names of the dictionary items in the “ansible_facts.devices” dictionary. It returns a list of all the disks.

You can run this playbook using the ansible-playbook command, specifying the path to the playbook file. Once the playbook runs, you’ll see the disk information displayed in the output, which will list all the disks on the remote hosts.

If you want to list the specific disk info, you can use the following code.

- name: Gather Disk Information
  hosts: your_hosts
  gather_facts: yes
    - name: Display Disk Information
        var: ansible_facts.devices.diskname


- name: Display Disk Information
    var: item.value
  with_dict: "{{ ansible_facts.devices }}"
  when: "'diskname' in item.key"

Get all the disks with lsblk shell command in Ansible

You can use the “lsblk” command via the “” module. Here’s an example playbook that demonstrates how to do this:

- name: List all disks
  hosts: your_host
    - name: Get disk information lsblk
      register: disk_info

    - name: Print disk information
        var: disk_info.stdout_lines

In this example, the “lsblk” command is executed on the remote host and the output is stored in the “disk_info” variable. The “debug” module is then used to print out the output of the “lsblk” command.

Note that you’ll need to replace “your_host” with the name or IP address of the host(s) you want to run the playbook against. Additionally, make sure that you have the necessary permissions to execute the “lsblk” command on the remote host.

    "disk_info.stdout_lines": [
        "NAME                     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT",
        "sda                        8:0    0   140G  0 disk ",
        "├─sda1                     8:1    0     1G  0 part /boot",
        "└─sda2                     8:2    0   139G  0 part ",
        "  ├─wbx_vg-root          253:0    0 106.5G  0 lvm  /",
        "  ├─wbx_vg-swap          253:1    0     3G  0 lvm  ",
        "  ├─wbx_vg-var_log_audit 253:2    0   512M  0 lvm  /var/log/audit",
        "  ├─wbx_vg-var_log       253:3    0     6G  0 lvm  /var/log",
        "  ├─wbx_vg-var           253:4    0     9G  0 lvm  /var",
        "  ├─wbx_vg-tmp           253:5    0    10G  0 lvm  /var/tmp",
        "  └─wbx_vg-home          253:6    0     4G  0 lvm  /home",
        "sdb                        8:16   0     2G  0 disk [SWAP]",
        "sdc                        8:32   0     1G  0 disk ",
        "sdd                        8:48   0     1G  0 disk ",
        "sde                        8:64   0     1G  0 disk ",
        "sdf                        8:80   0     1G  0 disk ",
        "sdg                        8:96   0     1G  0 disk ",
        "sr0                       11:0    1  1024M  0 rom  "

Get disk name and disk info with Ansible

The ansible_facts.devices dictionary typically contains keys representing the names of the devices, and the values are sub-dictionaries that contain various details about each device.

You can access the information in ansible_facts.devices using standard Jinja2 template filters, such as {{ ansible_facts.devices.sda.model }} to access the model of the ‘sda’ disk device.

We can also define a new dict which contains the info we are interested.

- name: Display disks and their details
    diskinfo: "{{ diskinfo | default({}) | combine({ item.key: { 'size': item.value.size, 'vendor':item.value.vendor }  }) }}"
  with_dict: "{{ ansible_facts.devices }}"
- name:
    var: diskinfo

TASK [debug] ***********************************************************************************************************************************************************************************************************
    "diskinfo": {
        "dm-0": {
            "size": "106.50 GB",
            "vendor": "VMware"
        "dm-1": {
            "size": "3.00 GB",
            "vendor": "VMware"

Filter disk name based on disk info with Ansible

We can filter disk names based on disk information with Ansible.

Let’s see one example.

- name: Display Disk Information
    msg: "{{item.key}}"
  with_dict: "{{ ansible_facts.devices }}"
  when: "item.value.partitions|length > 1

This Ansible task you provided is using the debug module to display the key (name) of each item in the ansible_facts.devices dictionary, but only for those items where the partitions key of the corresponding value (sub-dictionary) has a length greater than 1.