Ansible Disk Usage and Disk Space Full Alert generation Script.

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.

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

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)

`

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 "
}

`

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

- name: Disk role.
      include_role:
        name: disk_role
      vars:
        mount_point: "{{ item }}"
      with_items:
        - "/dev"
        - "/backup"

Don’t mind the messed up indent…

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

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