Skip to Content

how to get file size with Ansible

In Ansible, you can use the stat module to get the size of a file on a remote host. The stat module retrieves information about a file, including its size, permissions, and other attributes.

Get file size with stat module with Ansible

Here’s an example of how you can use the stat module to get the file size:

#https://www.howtouselinux.com/post/how-to-get-file-size-with-ansible
- name: Get file size
  hosts: your_host
  tasks:
    - name: Check file size
      stat:
        path: /path/to/your/file
      register: file_info
      
    - name: Display file size
      debug:
        var: file_info.stat.size

In this example, the stat module is used to retrieve information about the file located at /path/to/your/file on the remote host.

The register parameter is used to store the result of the stat module in a variable called file_info.

Then, the debug module is used to display the size attribute from the file_info.stat dictionary, which represents the size of the file in bytes.

Here’s an example that demonstrates how to use a variable for the file path in Ansible:

- name: Get file size with variable
  hosts: your_host
  vars:
    file_path: /path/to/your/file
  tasks:
    - name: Check file size
      stat:
        path: "{{ file_path }}"
      register: file_info
      
    - name: Display file size
      debug:
        var: file_info.stat.size

In this example, a variable named file_path is defined with the value of /path/to/your/file. The stat module is then used to retrieve information about the file specified by the file_path variable using the {{ file_path }} syntax to reference the variable.

The result is stored in the file_info variable using the register parameter. Finally, the debug module is used to display the size attribute from the file_info.stat dictionary, which represents the size of the file in bytes.

You can update the value of the file_path variable to dynamically specify the file path based on your requirements, such as reading it from an inventory file, a playbook variable, or any other external source.

Get the total file size under a directory with Ansible

Here’s an example that demonstrates how to use the find module in combination with the stat module in Ansible to get the size of all the files under a directory whose name is specified as a variable:

- name: Get total file size
  hosts: your_host
  vars:
    directory_path: /path/to/your/directory  # Update the directory path
  tasks:
    - name: Find files
      find:
        paths: "{{ directory_path }}"
        file_type: file
        recurse: yes
      register: found_files

    - name: Calculate total file size
      set_fact:
        total_size: "{{ found_files.files | map(attribute='size') | map('int') | sum }}"
        
    - name: Display total file size
      debug:
        var: total_size

In this example, the find module is used to search for files under the directory specified by the directory_path variable. The file_type parameter is set to file to only consider regular files, and the recurse parameter is set to yes to search for files recursively. The result of the find module is stored in a variable called found_files using the register parameter.

Next, the set_fact module is used to calculate the total size of all the found files. The map filter is used to extract the size attribute from each file in the found_files.files list, and then the int filter is used to convert the size values from strings to integers. Finally, the sum filter is used to calculate the sum of all the file sizes, which is stored in a variable called total_size.

Finally, the debug module is used to display the value of the total_size variable, which represents the total size of all the files under the directory specified by the directory_path variable.

Get the file name and file size under a directory with Ansible

Here’s an example that uses the find module in Ansible to get the list of all files and their sizes under a directory:

- name: Get file list and sizes
  hosts: your_host
  vars:
    directory_path: /path/to/your/directory  # Update the directory path
  tasks:
    - name: Find files
      find:
        paths: "{{ directory_path }}"
        file_type: file
        recurse: yes
      register: found_files

    - name: Display file list and sizes
      debug:
        var: found_files.files |map(attribute='path')| zip(found_files.files | map(attribute='size')) | map('list') | list

In this example, the find module is used to search for files under the directory specified by the directory_path variable. The file_type parameter is set to file to only consider regular files, and the recurse parameter is set to yes to search for files recursively. The result of the find module is stored in a variable called found_files using the register parameter.

The debug module is then used to display the files attribute from the found_files variable, which represents the list of all files found under the directory, along with their attributes, including their sizes. The result will show the file list and their sizes in bytes, which you can further process or format as needed.