Ansible 可以检查磁盘空间是否存在作为变量提及的挂载 [英] Ansible to check diskspace for mounts mentioned as variable

查看:30
本文介绍了Ansible 可以检查磁盘空间是否存在作为变量提及的挂载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  1. that 是一个原始的 Jinja2 表达式,不要在其中使用大括号.

  1. that is a raw Jinja2 expression, don't use curly brackets in it.

为什么要使用单独的 fail 任务,如果 assert 会失败并显示消息?

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

这篇关于Ansible 可以检查磁盘空间是否存在作为变量提及的挂载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆