Ansible 和硬件检查 [英] Ansible and hardware checks
问题描述
我必须使用 ansible 检查 Linux 机器上的不同硬件和配置元素,但我完全不知道该怎么做(RAM、磁盘空间、DNS、CPU...),我知道我几乎可以在 ansible 事实中找到我想要的所有内容,但我不知道如何使用它.
I have to check different hardware and configurations elements on Linux machines using ansible, and I am not sure at all about how to do it (RAM, disk space, DNS, CPU...), I've understood that I can find nearly all I want in the ansible facts, but I do not understand how I can use it.
例如,我必须检查 RAM 量是否至少为 4GB,否则会发出警报,因此我尝试了很多方法,但...没有任何效果.
For example, I have to check if the RAM amount is at least of 4GB and have an alarm if not, so I tried many things, and... nothing works.
这是我尝试过的示例.
- hosts: client
remote_user: user
tasks:
- debug: var=ansible_memory_mb
- debug: msg="total RAM is {{ ansible_memory_mb.real.total }}"
- fail: msg="not enough RAM"t
- when: {{ ansible_memory_mb.real.total }} < 4096
你能告诉我它是如何工作的吗?也许有更好的方法可以使用 Ansible 做我想做的事?
Could you tell me how it works ? and maybe there is a better way to do what I want using Ansible ?
感谢您的回答.
推荐答案
您发布的代码段存在一些问题.
There are a few things wrong with the snippet you posted.
您的缩进已关闭.
tasks
需要与hosts
处于相同的缩进级别.
Your indentation is off.
tasks
needs to be at the same indentation level ashosts
.
when
条件需要成为 fail
任务块的一部分,而不是单独的列表项.
The when
condition needs to be part of the fail
task block, not a separate list item.
一般情况下,when
条件中不需要使用{{ ... }}
,整个表达式将被视为Jinja模板.
In general, you do not need to use {{ ... }}
in a when
condition, the entire expression will be treated as a Jinja template.
试试这个:
- hosts: client
remote_user: user
tasks:
- debug: var=ansible_memory_mb
- debug: msg="total RAM is {{ ansible_memory_mb.real.total }}"
- fail: msg="not enough RAM"
when: ansible_memory_mb.real.total < 4096
您也可以使用 assert
模块来检查一个条件或条件列表.
You can also use the assert
module to check a condition or list of conditions.
- assert:
that:
- ansible_memory_mb.real.total >= 4096
- some other condition
- ...
这篇关于Ansible 和硬件检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!