# Ansible Disk Usage and Disk Space Full Alert generation Script.

**URL:** <https://forum.ansible.com/t/ansible-disk-usage-and-disk-space-full-alert-generation-script/26930>\
**Category:** Ansible Project\
**Created:** [March 8, 2018, 4:43am UTC](https://forum.ansible.com/t/ansible-disk-usage-and-disk-space-full-alert-generation-script/26930 "2018-03-08T04:43:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kiran\_Shrimali](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@Kiran\_Shrimali](https://forum.ansible.com/u/Kiran_Shrimali)\
**Post date:** [March 8, 2018, 4:43am UTC](https://forum.ansible.com/t/ansible-disk-usage-and-disk-space-full-alert-generation-script/26930/1 "2018-03-08T04:43:31Z")

</div>

Hi,

I am tryig to write an Ansible script which will Generat Alert on Disk Usage. Once the Disk Usage goes above the Threshold, alert will be generated.

Below is start up code.

---

<div class="post-metadata">

**Author:** ![Thomas\_Hikade](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Thomas\_Hikade](https://forum.ansible.com/u/Thomas_Hikade)\
**Post date:** [March 9, 2018, 1:15pm UTC](https://forum.ansible.com/t/ansible-disk-usage-and-disk-space-full-alert-generation-script/26930/2 "2018-03-09T13:15:56Z")

</div>

Hi Kiran

A few problems:  
First. you register the **results** (plural) of your with\_items loop in the first shell command. That means, _test.results_ is actually a list of result objects!  
Thus you cannot simply compare the value “20” with _test.results_. Instead, you need to loop over _test.results_ again and do the comparison for each result in turn.  
Second, printing out anything using shell cmd is not really working, because you wont actually see stdout! You would need to register the cmd output again and then print that using a debug statement in a loop again.

eg like this:

`

- name: Size is big  
debug:  
msg: "too big size: {{ item}} "  
when: item \> “20”  
with\_items: “{{ test.results|map(attribute=‘stdout’)|list}}”

`

Note: I am extracting stdout attribute like you did, as Ansible will verbosely print out the entire results items structure in debug statement loops, which makes it hard to read.

regards  
Thomas

---

<div class="post-metadata">

**Author:** ![Thomas\_Hikade](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Thomas\_Hikade](https://forum.ansible.com/u/Thomas_Hikade)\
**Post date:** [March 9, 2018, 1:22pm UTC](https://forum.ansible.com/t/ansible-disk-usage-and-disk-space-full-alert-generation-script/26930/3 "2018-03-09T13:22:58Z")

</div>

In addition you might like to print out the name of your FS which is too big. For that you need the names again in the last debug loop.  
You could fix that like this, using advanced looping statement “with\_together” ([Ansible Loops](https://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-parallel-sets-of-data))

`

vars:  
filesystems:

- /
- /tmp

size\_too\_big: “20”

tasks:

…

- name: Size is big v2  
debug:  
msg: "{{ item.1 }} is too big! size={{ item.0 }} "  
when: item.0 \> size\_too\_big  
with\_together:
- “{{ test.results|map(attribute=‘stdout’)|list }}”
- “{{ filesystems }}”

`

This will produce the following output:

`

TASK [Size is big v2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [demobox] =\> (item=[u’37’, u’/‘]) =\> {  
“item”: [  
“37”,  
“/”  
],  
“msg”: "/ is too big! size=37 "  
}  
ok: [demobox] =\> (item=[u’37’, u’/tmp’]) =\> {  
“item”: [  
“37”,  
“/tmp”  
],  
“msg”: "/tmp is too big! size=37 "  
}

`

---

<div class="post-metadata">

**Author:** ![Kiran\_Shrimali](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@Kiran\_Shrimali](https://forum.ansible.com/u/Kiran_Shrimali)\
**Post date:** [March 12, 2018, 6:45am UTC](https://forum.ansible.com/t/ansible-disk-usage-and-disk-space-full-alert-generation-script/26930/4 "2018-03-12T06:45:32Z")

</div>



---

<div class="post-metadata">

**Author:** ![Varun\_Chopra](https://avatars.discourse-cdn.com/v4/letter/v/54ee81/32.png) [@Varun\_Chopra](https://forum.ansible.com/u/Varun_Chopra)\
**Post date:** [March 12, 2018, 8:20am UTC](https://forum.ansible.com/t/ansible-disk-usage-and-disk-space-full-alert-generation-script/26930/5 "2018-03-12T08:20:23Z")

</div>

You can use it as a role and loop over it:

```auto
- name: Disk role.
      include_role:
        name: disk_role
      vars:
        mount_point: "{{ item }}"

```

```auto
      with_items:
        - "/dev"
        - "/backup"

```

---

<div class="post-metadata">

**Author:** ![Varun\_Chopra](https://avatars.discourse-cdn.com/v4/letter/v/54ee81/32.png) [@Varun\_Chopra](https://forum.ansible.com/u/Varun_Chopra)\
**Post date:** [March 12, 2018, 8:21am UTC](https://forum.ansible.com/t/ansible-disk-usage-and-disk-space-full-alert-generation-script/26930/6 "2018-03-12T08:21:16Z")

</div>

Don’t mind the messed up indent…

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [March 12, 2018, 8:56am UTC](https://forum.ansible.com/t/ansible-disk-usage-and-disk-space-full-alert-generation-script/26930/7 "2018-03-12T08:56:37Z")

</div>

Why not using ansible\_mounts fact instead of running df ? Shell task should be avoided each time it is possible ! It already has information about FS Regards, JYL

---

<div class="post-metadata">

**Author:** ![Varun\_Chopra](https://avatars.discourse-cdn.com/v4/letter/v/54ee81/32.png) [@Varun\_Chopra](https://forum.ansible.com/u/Varun_Chopra)\
**Post date:** [March 12, 2018, 9:22am UTC](https://forum.ansible.com/t/ansible-disk-usage-and-disk-space-full-alert-generation-script/26930/8 "2018-03-12T09:22:31Z")

</div>

This thread is filled with red flags, and im not even talking about the fact that he’s using ansible for monitoring…
