How to check disk space usage through ansible playbook

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

Some Ansible related post , which will help in setting Ansible Navigator  and some helpfull content is below.

Install Ansible Navigator And Execution Environment

Register Variable

In the register variable, the keyword we will use is the register, to capture the output. below the playbook, the 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 playbook with the register command and 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

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

Playbook

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 register 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 ansible ad-hoc command for like temporary purposes through playbook we can do through different modes according to your need.

About Sachin G

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. Required fields are marked *