Home > database >  Extract specific data from an output of shell command in Ansible
Extract specific data from an output of shell command in Ansible

Time:07-12

i would create a playbook that extract the used space for the /boot through the shell command df -B MB. for this example the playbook will show 358MB for the controller node as an example. I would 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

 - 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"
    ]
}

CodePudding user response:

This is a typical x/y problem (although it is easy to answer since you have given your root requirement i.e. get available size on a mount point).

You don't have to use shell to get that information. It's all available in the target machine facts (just make sure you did not disable fact gathering with gather_fact: no on your play).

The example below is for /boot/efi on my system. Just put it back on the actual mount point you want to target in the selectattr filter.

---
- 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 }}"

gives:

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
  • Related