How to check disk space usage through ansible playbook

This post will show you how we can view disk space through an -playbook. Here I am using Linux-based identical machines and the Operating system is CentOS. In our playbook, I am going to use the variable to capture the output and debug module to show the output of the variable.

Variable

In the variable the keyword we will use the register, to capture the output. below playbook, code shows the structure of the register variable from a task.

debug module

the debug module to print the standard output of the task commands.

Below is the full with the command the debug module. One thing to notice is that I am using stdout_lines and not stdout. The reason is that I like the output to be in lines

Below i am providing two  only two minor differences . You can use any one which you feel better to use . 

Create a playbook inside your project directory and verify every managed host are running and working .
---
- name: Check Disk Space Usage
  hosts: srv1.example.com
  tasks:
          - name: Disk usage from command module
            command: df -h
            register: space
          - debug:
                  var: space

The output of this playbook will consist of the whole output, which is captured in the variable with stdout and stdout_lines.

The below playbook snippet will show only variable space.stdout_lines, which is automatically arranged in lines but you can also show complete information with stdout.

---
- name: Check Disk Space Usage
  hosts: srv1.example.com
  tasks:
          - name: Disk usage from command module
            command: df -h
            register: space
          - debug:
                  var: space.stdout_lines

Output :

I have done the above task through the playbook but we can do this task through ad-hoc command for like temporary purposes through playbook we can do through different modes according to your need.

About Sachin Gupta

I am a professional freelance contributor and founder of tech transit. Love to write and lover of education, culture, and community. I have been using it, setting, supporting, and maintaining it since 2009.Linux rocks!

Have any Question or Comment?

Leave a Reply

Your email address will not be published.