0

Here are my example codes, questions at bottom

hw.j2:

System Total Memory     : {{ ansible_memtotal_mb }}
Free Memory             : {{ ansible_memfree_mb }}
System Total CPU        : {{ ansible_processor_cores }}
System Virtual CPU      : {{ ansible_processor_vcpus }}

Disks capacities
        /dev/sda        : {{ ansible_devices.sda.size }}
        /dev/sdb        : {{ ansible_devices.sdb.size }}
        /dev/sdc        : {{ ansible_devices.sdc.size }}

collecthw.yml:

---
- name: Collect information
  become: yes
  hosts: clients

  tasks:
  - name: use template from labs/hw.j2
    template:
      src: hw.j2
      dest: hw.{{ ansible_facts.hostname }}.txt
      owner: corona
      group: corona
      mode: '0600'

It works okay but ..

Now my questions are:

  1. How to loop across all /dev/sd* in managed nodes and collect their respective disk size?
  2. This is the disk size, how about unused disk space in the disk?

Thank you in advance for those that able to spend time and effort in getting the answer or perhaps other advice. Newbie here xD

0

For example, select all devices except loop* and display size

    - set_fact:
        my_devices: "{{ ansible_devices.keys()|
                        reject('match', '^loop(.*)$')|
                        list }}"
    - debug:
        msg: "{{ item }} {{ ansible_devices[item].size }}"
      loop: "{{ my_devices }}"

give

  msg: nvme0n1 238.47 GB

Q1: "How to loop across all /dev/sd* in managed nodes and collect their respective disk size?"

A1: In the code above, change the reject filter to select

                        select('match', '^sd(.*)$')|

and use the template below

{% for dev in my_devices %}
{{ dev }}: {{ ansible_devices[dev].size }}
{% endfor %}

Q2: "How about unused disk space in the disk?"

A2: Use the variable ansible_mounts to display unsused space. For example

    - set_fact:
        my_devs: "{{ my_devs|default([]) +
                     [ansible_mounts|
                      selectattr('device', 'match', my_regex)|
                      list|
                      json_query(my_query)]|first }}"
      loop: "{{ my_devices }}"
      vars:
        my_regex: '^(.*){{ item }}(.*)$'
        my_query: '[].[device, size_total, size_available]'

    - debug:
        msg: "dev: {{ item.0 }}
              size[MB]: {{ (item.1|int / 1000000)|int }}
              free[MB]: {{ (item.2|int / 1000000)|int }}"
      loop: "{{ my_devs }}"

give

  msg: 'dev: /dev/nvme0n1p6 size[MB]: 41015 free[MB]: 12677'
  msg: 'dev: /dev/nvme0n1p7 size[MB]: 109899 free[MB]: 39896'
  msg: 'dev: /dev/nvme0n1p2 size[MB]: 100 free[MB]: 45'

Use the template below to store unused space in Bytes

{% for dev in my_devs %}
{{ dev[0] }}: {{ dev[2] }}
{% endfor %}
3
  • I am amazed at your epic code. Even I have years of scripting background. That is godlevel I tell you. haha
    – Jason
    Jul 15 '20 at 11:07
  • Yet it doesnt solve my playbook intention. I had been having trouble to solve this from the question from EX294 exam.
    – Jason
    Jul 17 '20 at 1:33
  • I've updated the answer and added the templates. In Q1 simply change the filter to select and fit the regex. No changes are needed in Q2. Jul 17 '20 at 6:00
0

Ok gys I finally found out the answer . Its default value with jinja template.
Example code below:
vi hw.j2
Disks capacities
/dev/sda : {{ ansible_devices.sda.size|default("NONE", true) }}
/dev/sdb : {{ ansible_devices.sdb.size|default("NONE", true) }}
/dev/sdc : {{ ansible_devices.sdc.size|default("NONE", true) }}
/dev/sdz : {{ ansible_devices.sdz.size|default("NONE", true) }

client@ cat list-hw.r8s2.txt
Disks capacities
/dev/sda : 20.00 GB
/dev/sdb : 20.00 GB
/dev/sdc : 20.00 GB
/dev/sdz : NONE

2
  • to loop across all /dev/sd* How do you know a,b,c,z are all devices? Jul 17 '20 at 6:02
  • got some typo, i didnt realize that. <br> I use ansible_devices. dict. it works for me <br> /dev/sda , /dev/sdb and /dev/sdc is in the system, thats why it show 20GB , but when it comes to /dev/sdz, this disk is not in the system hence it show NONE
    – Jason
    Jul 17 '20 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.