无法检查磁盘空间是否有挂载为变量 [英] Ansible to check diskspace for mounts mentioned as variable
问题描述
我是ansible的新手,目前正在制作一个剧本,它将查看远程机器的磁盘空间是否已达到70%阈值.如果他们已经达到它应该抛出错误.
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.
我在以下位置找到了一个很好的例子:使用ansible来管理磁盘空间
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'
有什么办法可以动态输入mountname?我可以输入多个安装名称吗?
Is there any way to enter mountname dynamically ? and can i enter multiple mount names ??
我在rhel上使用ansible 2.3
I am using ansible 2.3 on rhel
先谢谢您了:)
推荐答案
尝试一下:
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 }}"
-
that
是原始的Jinja2表达式,请不要在其中使用大括号.
that
is a raw Jinja2 expression, don't use curly brackets in it.
如果assert
可能失败并显示一条消息,为什么还要使用单独的fail
任务?
why do you use separate fail
task, if assert
can fail with a message?
这篇关于无法检查磁盘空间是否有挂载为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!