ansible - 根据磁盘可用空间获取节点 IP
问题描述
我正在尝试编写一个检查多台服务器上的磁盘空间的 ansible playbook。
到目前为止,这是我的 Ansible 剧本:
---
- hosts: all
become: yes
tasks:
- name: Check / freespace
shell: df -h / | awk '{if($5 > 85)print (IP}'s
基本上我想做的是,只要满足 shell 条件,我就想检索所有超过 85% 的服务器的 IP。
解决方案
我建议使用Ansible fact ansible_mounts
来获取已安装设备的列表及其详细信息。
这个事实会给我们:
- 总空间
size_total
- 中的自由空间
size_available
所以我们可以得到可用空间的百分比:
size_available / size_total x 100 = free space
如果可用空间小于 15%,下面的示例任务将显示设备:
# Use this task if "gather_facts" is disabled
- name: collect ansible_mounts facts
setup:
filter: ansible_mounts
- name: Show Devices having less than 15% free space
debug:
msg: "Device with > 85% use: {{ item.device }}"
when: item.size_available / item.size_total * 100 < 15
loop: "{{ ansible_mounts }}"
loop_control:
label: "{{ item.mount }}"
更新:
如果您想过滤掉本地设备安装,您可以在条件中另外使用regex_search
过滤器。not item.device | regex_search('^/dev')
when
推荐阅读
- vue.js - npm 安装 -g
E401 失败 - c# - 按日期将两个列表合并为一个
- c# - 将数据参数从模型传递到下一页 Prism Xamarin Forms
- python - 如何在气流中创建自定义运算符并在通过云作曲家运行的气流模板中使用它们(在谷歌云平台中)
- angular-material - 如何将 Angular Mat-select 字段中的选定选项输出为 Html 中的文本?
- c# - 使用属性路由找到了与 Web Api 中的请求匹配的多个操作
- python - 从另一个文本文件中存在的文本文件中删除单词 - Python
- ios - 目标 C 深度链接与常规链接
- php - Android:发送问题(在 Kotlin 中使用 Retrofit)或从 php 接收 json 文件
- javascript - 如何在此表示法中指向 2d 数组的特定位置