Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible to check diskspace for mounts mentioned as variable

Tags:

ansible

I am new to ansible and currently working on a play which will see if disk space of remote machines has reached 70% threshold. If they have reached it should throw error.

i found a good example at : Using ansible to manage disk space

but at this example the mount names are hard coded. And my requirement is to pass them dynamically. So i wrote below code which seems to not work:

    name: test for available disk space
    assert:
    that: 
    - not {{ item.mount == '{{mountname}}' and ( item.size_available < 
    item.size_total - ( item.size_total|float * 0.7 ) ) }}
    with_items: '{{ansible_mounts}}'
    ignore_errors: yes
    register: disk_free

    name: Fail the play
    fail: msg="disk space has reached 70% threshold"
    when: disk_free|failed

This play works when i use:

item.mount == '/var/app'

Is there any way to enter mountname dynamically ? and can i enter multiple mount names ??

I am using ansible 2.3 on rhel

Thanks in advance :)

like image 266
Krishna Sharma Avatar asked May 19 '17 19:05

Krishna Sharma


1 Answers

Try this:

name: Ensure that free space on {{ mountname }} is grater than 30%
assert:
  that: mount.size_available > mount.size_total|float * 0.3
  msg: disk space has reached 70% threshold
vars:
  mount: "{{ ansible_mounts | selectattr('mount','equalto',mountname) | list | first }}"
  1. that is a raw Jinja2 expression, don't use curly brackets in it.

  2. why do you use separate fail task, if assert can fail with a message?

like image 148
Konstantin Suvorov Avatar answered Sep 22 '22 20:09

Konstantin Suvorov