Ansible和硬件检查 [英] Ansible and hardware checks

查看:185
本文介绍了Ansible和硬件检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用Ansible检查Linux机器上的不同硬件和配置元素,而且我不确定如何做到(RAM,磁盘空间,DNS,CPU ...),我了解我可以在这个不切实际的事实中找到几乎所有我想要的东西,但是我不知道如何使用它.

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.

这是我尝试过的一个例子.

Here is an example of what I tried.

 - 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 as hosts.

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屋!

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