Impact-Site-Verification: d8d8859d-7548-47cd-9205-db15dd001e65

Last Updated on 10 months by Sachin G

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. Ansible playbooks are YAML-based configuration files that define a set of tasks to be executed on remote hosts. These tasks can range from simple shell commands to complex configuration management actions. Playbooks are at the heart of Ansible’s automation capabilities, enabling administrators to define infrastructure as code and maintain consistency across their environments.

This playbook should work on RPM-based Linux operating systems like Rocky, Alma Oracle Linux, etc. In our playbook, I’ll utilize a register variable to store the output. Then, I’ll employ the debug module to display the content stored within that variable.

Some Ansible-related posts will help in setting up Ansible Navigator, and some helpful content is below.

Install Ansible Navigator And Execution Environment

Register Variable

In the register variable, we’ll employ the keyword “register” to capture the output of a task. Below the playbook, the code illustrates the structure of the register variable used within 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. According to an accessible version or platform, you can run your playbook.

ansible-navigator run -m stdout playbook_name.yml

OR

ansible-playbook playbook_name.yml 

Make sure you have SSH access to the target Linux systems and that Ansible is installed on the system from where you run the playbook. Additionally, ensure that your SSH keys are properly set up for passwordless authentication, or you may need to provide the SSH password through Ansible’s --ask-pass option.

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

Check Disk Space Usage 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
            ansible.builtin.command: df -h
            register: space
          - ansible.builtin.debug:
                  var: space

The playbook’s output will encompass the entirety of the captured output stored in the register variable. Specifically, it will include both the stdout and stdout_lines components.

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
            ansible.builtin.command: df -h
            register: space
          - ansible.builtin.debug:
                  var: space.stdout_lines

Output :

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