Monthly Archives: January 2022

Ansible | How to get the size of a directory from a linux server

To get the filesystem size, used, available, % used, and what it’s mounted on run this simple ansible script to pull the data.

It uses the df -h command of /var/log. You can modify it for any other directory you like

---
- name: get the size of 
  hosts: all
  tasks:
    - name: get disk space using df -h /var/log
      command: df -h /var/log
      register: var_log_space_raw
    - set_fact:
        folder_size:
        #  "{{ var_log_space_raw.stdout.split()[8,11]}}"
          "{{ var_log_space_raw.stdout_lines}}"
        register: folder_size
    - debug:
        msg: "{{ folder_size }}"

The remarked out line gives the fields for Size and Used%