I would like to create a playbook that extract the used space for /boot through the shell command df -B MB.
For this example, the playbook will show 358MB for the controller node.

I would like to know how to filter the server_info.stdout_lines in order to get the desired result.
I believe that we can do it with the regex_search filter.

 - hosts: all
      gather_facts: True
      become: True
      tasks:
        - name: show used space on /boot
          shell: "df -B MB"
          register: server_info

        - debug:
            msg: "{{ server_info.stdout_lines }}"
TASK [debug] *****************************************************************************************************************
ok: [control] => {
    "msg": [
        "Filesystem          1MB-blocks   Used Available Use% Mounted on",
        "devtmpfs                1458MB    0MB    1458MB   0% /dev",
        "tmpfs                   1488MB    1MB    1488MB   1% /dev/shm",
        "tmpfs                   1488MB   10MB    1478MB   1% /run",
        "tmpfs                   1488MB    0MB    1488MB   0% /sys/fs/cgroup",
        "/dev/mapper/cs-root    19267MB 6268MB   13000MB  33% /",
        "/dev/sda1               1064MB  358MB     706MB  34% /boot",
        "tmpfs                    298MB    1MB     298MB   1% /run/user/42",
        "tmpfs                    298MB    1MB     298MB   1% /run/user/1000"
    ]
}
ok: [ansible2] => {
    "msg": [
        "Filesystem          1MB-blocks   Used Available Use% Mounted on",
        "devtmpfs                1458MB    0MB    1458MB   0% /dev",
        "tmpfs                   1488MB    0MB    1488MB   0% /dev/shm",
        "tmpfs                   1488MB   10MB    1478MB   1% /run",
        "tmpfs                   1488MB    0MB    1488MB   0% /sys/fs/cgroup",
        "/dev/mapper/cs-root    19267MB 6264MB   13003MB  33% /",
        "/dev/sda1               1064MB  360MB     704MB  34% /boot",
        "tmpfs                    298MB    1MB     298MB   1% /run/user/42",
        "tmpfs                    298MB    1MB     298MB   1% /run/user/1000"
    ]
}

推荐答案

这是一个典型的x/y problem(尽管很容易回答,因为您已经给出了根需求,即在安装点上使用大小).

你不必使用shell来获得这些信息.它在目标机器事实中都可用(只需确保你没有在游戏中禁用gather_fact: no的事实收集功能).

下面的示例适用于我的系统上的/boot/efi.只需将其放回您想要在selectattr过滤器中定位的实际装载点.

---
- hosts: localhost

  vars:
    mount_point: /boot/efi

  tasks:
    - vars:
        device_info: "{{ ansible_mounts | selectattr('mount', '==', mount_point) | first }}"
      debug:
        msg: "Used size on {{ mount_point  }} is {{ (device_info.size_total - device_info.size_available) | human_readable }}"

给予:

PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Used size on /boot/efi is 29.98 MB"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Linux相关问答推荐

jinja2.exceptions.TemplateSyntaxError:预期标记,,得到整数(支持十六进制,八进制和二进制整数文字)

如何从核心转储中获取线程名称?

如何更改文件的上次访问/修改/更改日期?

在 bash 中解析变量

使用 sed 或 awk 在 linux 中将第一行中的一个单词替换为第二行中的另一个单词

ENQCMD 指令的好处和微操作是什么?

你如何在 CentOS9 上使用 C++ fmt?

普通用户 chown(仅更改组)

如何重新安装最新的 cmake 版本?

如何在初始化脚本中以特定用户身份运行命令?

如何在 Linux/Unix 上递归复制以abc开头的目录?

在 C 中检测 64 位编译

在 C 中设置环境变量

我想为 Linux 内核做贡献

SVN 错误:无法将字符串从本机编码转换为UTF-8

如何在shell的递归目录中列出特定类型的文件?

如何将路径名中的..转换为 bash 脚本中的绝对名称?

Android - 找不到命令

如何将 ISO8859-15 转换为 UTF8?

编辑文件时保留 samba 共享的文件权限