首页 > 解决方案 > 根据磁盘可用空间获取节点 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

解决方案


我建议使用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


推荐阅读