代码之家  ›  专栏  ›  技术社区  ›  Petr

如何使用Ansible检查特定文件系统的使用情况

  •  0
  • Petr  · 技术社区  · 6 年前

    我需要创建一张与此处所述支票非常相似的支票: Ansible to check diskspace for mounts mentioned as variable

    {{ ansible_mounts }} 是一个字典数组,每个字典都包含变量 mount {{ansible_mounts}

    我想用伪代码实现的示例:

    foreach (mountpoint in ansible_mounts)
    {
        if (mountpoint["mount"] == "/var" || mountpoint["mount"] == "/opt")
        {
            // do the check
        }
    }
    

    我怎么能在Jinja/Ansible这样做?此代码检查每个项目。我只需要为显式指定的路径筛选它:

    - name: Ensure that free space on the tested volume is greater than 15%
      assert:
        that:
          - mount.size_available > mount.size_total|float * 0.15
        msg: Disk space has reached 85% threshold
      vars:
        mount: "{{ ansible_mounts | selectattr('mount','equalto',item.mount) | list | first }}"
      with_items:
        - "{{ ansible_mounts }}"
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   user2599522    6 年前

    你需要添加一个 when 条件,例如

    vars:
      my_mounts:
        - '/var/log'
        - '/var/logs/foo'
    
    tasks:
    - name: do the check
      when: item.mount in my_mounts
      with_items: '{{ ansible_mounts }}'