report for mount size_available

Hi

I would like to create report for free partition size under “ansible_mounts”

“ansible_mounts”: [
{
“block_available”: 3911145,
“block_size”: 4096,
“block_total”: 4452864,
“block_used”: 541719,
“device”: “/dev/mapper/vg_root-lv_root”,
“fstype”: “xfs”,
“inode_available”: 8871855,
“inode_total”: 8910848,
“inode_used”: 38993,
“mount”: “/”,
“options”: “rw,seclabel,relatime,attr2,inode64,noquota”,
“size_available”: 16020049920,
“size_total”: 18238930944,
“uuid”: “e2968252-bcd8-45ac-9604-714aa932deb9”
},
{
“block_available”: 222270,
“block_size”: 4096,
“block_total”: 259584,
“block_used”: 37314,
“device”: “/dev/sda1”,
“fstype”: “xfs”,
“inode_available”: 523988,
“inode_total”: 524288,
“inode_used”: 300,
“mount”: “/boot”,
“options”: “rw,seclabel,relatime,attr2,inode64,noquota”,
“size_available”: 910417920,
“size_total”: 1063256064,
“uuid”: “bcce38b2-0c6c-44aa-99be-20e3553d23ee”

how can I get variables for /, /var /boot and other partitions and create report like this in a file:

root: 10GB free
/boot: 800MB free

/var: 2GB free

Thanks

- copy:
    content: |
      {% for mt in ansible_mounts %}
      {{ mt.mount }}: {{ ( mt.size_available / ( 1024 | pow(3))) | round(2) }} GB free
      {% endfor %}
    dest:
      /tmp/freespace.txt

This creates a report in /tmp/freespace.txt with the GB values with two decimal spaces.
Distinguishing between GB and MB would be a bit more complicated.

Regards
        Racke

Thanks, this works great, can you explain me more what this jinja2 template do especially:

/ ( 1024 | pow(3))) | round(2) }

/ ( 1024 | pow(3)) → is this divide1024 three times? what is pow ?

round(2) → two decimal ?

where can I get more jinja2 examples or tutorial?

Thanks

Thanks, this works great, can you explain me more what this jinja2 template do especially:

/ ( 1024 | pow(3))) | round(2) }

/ ( 1024 | pow(3)) --> is this divide1024 three times? what is pow ?
round(2) --> two decimal ?

where can I get more jinja2 examples or tutorial?

1024 | pow(3) ... that is 1024³ = 1024 * 1024 * 1024 = 1 GB

round ... rounds a number with optional decimals, so 10.445 | round(2) => 10.45.

So we divide the available size by 1 GB and round it up. The parentheses are important
to process in the correct orders.

I maintain a couple of filter examples at
https://wiki.linuxia.de/library/stefan-hornburg-racke-ansible-provisioning-en#text-amuse-label-filters

Regards
        Racke

what will be jinja template if I would like to create report only for / and /var partition? Or haw would I accomplish that?

Thanks

Regards

what will be jinja template if I would like to create report only for / and /var partition? Or haw would I accomplish that?

Thanks

Regards

You have (at least) two options here.

First with a if condition:

{% for mt in ansible_mounts %}
{% if mt.mount in ['/', '/var'] %}
{{ mt.mount }}: {{ ( mt.size_available / ( 1024 | pow(3))) | round(2) }} GB free
{% endif %}
{% endfor %}

Second with a filter on ansible mounts:

{% for mt in (ansible_mounts | selectattr('mount', 'search', '^/(var)*$') | list) %}
{{ mt.mount }}: {{ ( mt.size_available / ( 1024 | pow(3))) | round(2) }} GB free
{% endfor %}

If the partitions to report vary between host / groups use the first solution and replace ['/', '/var']
with a variable, e.g.

report_parts:
  - /
  - /var

Regards
          Racke

Hi

I was trying to make report for free space in vg, but I have a problems, it’s look like jinja doesn’t like me::))

ansible_lvm

},
“vgs”: {
“vg_root”: {
“free_g”: “0”,
“num_lvs”: “2”,
“num_pvs”: “1”,
“size_g”: “19.00”
},
“vgswap”: {
“free_g”: “1.50”,
“num_lvs”: “1”,
“num_pvs”: “1”,
“size_g”: “1.75”

},
“vgdata”: {
“free_g”: “1.80”,
“num_lvs”: “1”,
“num_pvs”: “1”,
“size_g”: “1.75”

  • name: vg free
    copy:
    content: |
    {% for vg in ansible_lvm %}
    {{ vg.vgs }} {{ vg.free_g }} GB
    {% endfor %}
    dest: /tmp/vg-free-vgs

but it keep telling me that I didn’t defined variable vgs: (The task includes an option with an undefined variable. The error was: ‘str object’ has no attribute ‘vgs’) .

Can you please help me once again with this? Thanks

Hi

I was trying to make report for free space in vg, but I have a problems, it's look like jinja doesn't like me::))
ansible_lvm
},
"vgs": {
"vg_root": {
"free_g": "0",
"num_lvs": "2",
"num_pvs": "1",
"size_g": "19.00"
},
"vgswap": {
"free_g": "1.50",
"num_lvs": "1",
"num_pvs": "1",
"size_g": "1.75"
},
"vgdata": {
"free_g": "1.80",
"num_lvs": "1",
"num_pvs": "1",
"size_g": "1.75"

-name:vg free
copy:
content:|
{%for vg in ansible_lvm %}
{{vg.vgs }}{{vg.free_g }}GB
{%endfor %}
dest:/tmp/vg-free-vgs

but it keep telling me that I didn't defined variable vgs: (The task includes an option with an undefined variable. The
error was: 'str object' has no attribute 'vgs') .

ansible_lvm is a dictionary and not a list. So looping as above doesn't work.

Please try the following loop:

     {% for vg in ansible_lvm.vgs | dict2items %}
     {{ vg.key }} {{ vg.value.free_g }} GB
     {% endfor %}

Regards
         Racke

Thanks it’s working.

The same is this:

  • name: vg space
    lineinfile:
    path: /tmp/vg_free
    line: “VG {{ item.key }} free {{ item.value.free_g }} GB”
    loop: “{{ ansible_lvm.vgs| dict2items }}”

Regards

Thanks it's working.

The same is this:

-name: vg space
lineinfile:
path:/tmp/vg_free
line:"VG {{ item.key }} free {{ item.value.free_g }} GB"
loop:"{{ ansible_lvm.vgs| dict2items }}"

I would prefer the "copy" approach:

* more efficient, writes the file once
* your lineinfile example doesn't erase old entries with different free space value

Regards
       Racke

Hi

copy doesn’t work for me. I have 2 VG and with the copy module it looks like it rewrites the first occurrence of VG in loop, linenifile works as it should.

  • name: vg free
    copy:
    dest: /tmp/file-copy
    content: “VG: {{ item.key }} free {{ item.value.free_g }} GB”
    loop: “{{ ansible_lvm.vgs|dict2items }}”

TASK [crate /tmp/file-copy] *************************************************************************************************************
changed: [servera]

TASK [vg free] **************************************************************************************************************************
changed: [servera] => (item={‘key’: ‘apache-vg’, ‘value’: {‘size_g’: ‘0.49’, ‘free_g’: ‘0.12’, ‘num_lvs’: ‘2’, ‘num_pvs’: ‘2’}})
changed: [servera] => (item={‘key’: ‘vg_root’, ‘value’: {‘size_g’: ‘19.00’, ‘free_g’: ‘0’, ‘num_lvs’: ‘2’, ‘num_pvs’: ‘1’}})

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

[student@ansiblem ]$ ansible -m shell -a “cat /tmp/file-copy” servera
servera | CHANGED | rc=0 >>
VG: vg_root free 0 GB

lineinfile:

  • name: vg free
    lineinfile:
    path: /tmp/file-lineinfile
    line: “VG: {{ item.key }} free {{ item.value.free_g }} GB”
    loop: “{{ ansible_lvm.vgs|dict2items }}”

ansible -m shell -a “cat /tmp/file-lineinfile” servera
servera | CHANGED | rc=0 >>
VG: apache-vg free 0.12 GB
VG: vg_root free 0 GB

I should probably use jinja2 and template module?

Regards

Hi

copy doesn't work for me. I have 2 VG and with the copy module it looks like it rewrites the first occurrence of VG in
loop, linenifile works as it should.

- name: vg free
copy:
dest: /tmp/file-copy
content: "VG: {{ item.key }} free {{ item.value.free_g }} GB"
loop: "{{ ansible_lvm.vgs|dict2items }}"

You need to put the loop into the content.

Regards
        Racke

How can I put the loop into the content? Can you please write?

Thanks

How can I put the loop into the content? Can you please write?

Thanks

Sure, here we go:

- name: vg free
  copy:
    dest: /tmp/file-copy
    content: |
      {% for vg in ansible_lvm.vgs | dict2items %}
      VG {{ vg.key }} {{ vg.value.free_g }} GB
      {% endfor %}

Regards
        Racke

Thanks

I just add endfor ::))

copy:
dest: /tmp/file-copy2
content: |
{% for vg in ansible_lvm.vgs | dict2items %}
VG {{ vg.key }} {{ vg.value.free_g }} GB
{% endfor %}